Funciones de terraform







En esta publicación, discutiremos las características de Terraform. El lenguaje Terraform incluye una serie de funciones integradas que puede llamar desde expresiones para transformar y combinar valores. La sintaxis general para las llamadas a funciones es el nombre de la función, seguido de los argumentos, separados por comas, entre paréntesis.







<function_name>(<argument1>, <argument2>, …<argumentN>)
      
      





A continuación, explicaremos las características de Terraform con ejemplos. Usaremos la consola terraform para esta demostración.







Características de Terraform:







Abra la consola terraform

terraform console









Funciones numéricas







abs(number): .

abs(-19.86)



# 19.86









ceil(number): ,

ceil(7.1)



# 8









floor(number): ,

floor(7.1)



# 7









log(number, base): log .

log(16,2)



# 4









max(N1,N2,..Nn): .

max(3,2,6,8.8,7)



# 8.8









min(N1,N2,..Nn): .

min(3,2,6,8.8,7)



# 2









pow(number,power): , .

pow(8,2)



# 64









signum(number): , -1 1 .

signum(-4)



# -1





signum(4)



# 1















chomp("string"): .

chomp("cloudaffaire\n")



# cloudaffaire



(** \ n )







format(spec, values...):

format("Welcome, to %s", "CloudAffaire")



# Welcome, to CloudAffaire





format("The year is %d", 2019)



# The year is 2019

format("%4.4f+", 3.86)



# 3.8600+











formatlist(spec, values...): formatlist

. , .

formatlist("www.%s.com",list("azure","aws","google"))



# [www.azure.com,www.aws.com,www.google.com]









indent(num_spaces, string): , , .

indent(8,"hi, \n welcome \n to \n cloudaffaire")



#1st line hi has no indentaton







join(separator, list): , .

join(".",list("www","google","com"))



# www.google.com









lower(string): .

lower("CLOUDAFFAIRE")



# cloudaffaire









upper(string): .

upper("cloudaffaire")



# CLOUDAFFAIRE









replace(string, substring, replacement): ,

.

replace("www.google.com","google","cloudaffaire")



# www.cloudaffaire.com









split(separator, string): , .

split(".","www.google.com")



# ["www","google","com"]









strrev(string): . , Unicode.

strrev("google")



#: elgoog



( terraform 0.12 )







substr(string, offset, length): .

substr("www.google.com",4,6)



# google









title(string): .

title("welcome to cloudaffaire")



# Welcome To Cloudaffaire









trimspace(string): .

trimspace(" hello, all ")



# "hello, all



"













chunklist(list, chunk_size): , .

chunklist(list("a","b","c","d","e","f"),3)



# [["a","b","c"],["d","e","f"]]









coalesce(strings\numbers): , .

coalesce("",1,"a")



# 1









coalescelist(list1, list2,… listn): .

coalescelist(list(),list("a","b","c"),list("d","e"))



# ["a","b","c",]









compact(list(string)): .

compact(list("a","","c","","d"))



# ["a","c","d"]









concat(list1, list2,… listn): .

concat(list("a","b"),list("c","d"),list("e","f"))



# ["a","b","c","d","e","f"]









contains(list, value): , .

contains(list("a","b","c"),"a")



# true





contains(list("a","b","c"),"d")



# false









distinct(list): .

distinct(list("a","b","b","c"))



# ["a","b","c",]









element(list, index): .

element(list("a","b","c"),2)



# c #index start from 0







index(list, value): .

index(list("a","b","c"),"b")



# 1









flatten(list(list1,list2,..,listn)): , .

flatten(list(list("a","b"),list("c"),list(),list("d","e")))



# ["a","b","c","d","e",]









keys(map): , .

keys(map("name","debjeet","sex","male"))



# ["name","sex",]









length(list\map\string): , .

length(list("a","b"))



# 2





length("debjeet")



# 7





length(map("name","debjeet","sex","male"))



# 2









list(): , .

list("a","b","c")



# ["a","b","c",]









lookup(map, key, default): , . , .







lookup(map("name","debjeet","sex","male"),"sex","not found!")



# male





lookup(map("name","debjeet","","male"),"gender","not found!")



# not found!









map("key1","value1","key2","value2",...,"keyn","valuen"): ,

.

map("name","debjeet","sex","male")



# {"name" = "debjeet" "sex" = "male"}









matchkeys(valueslist, keyslist, searchset): , ,

.

matchkeys(list("a","b","c"),list("one","two","three"),list("two"))



# b





matchkeys(list("a","b","c"),list("one","two","three"),list("one"))



# a





matchkeys(list("a","b","c"),list("one","two","three"),list("three"))



# c









merge(map1,map2,..,mapn):

.

merge(map("a","one"),map("b","two"),map("c","three"))



# {"a" = "one" "b" = "two" "c" = "three"}









reverse(list): .

, , .







reverse(list("a","b","c"))



#: ["c","b","a",]



( terraform 0.12 )







setintersection(sets...): , , .

setintersection(list("a","b"),list("b","c"),list("b","d"))



# ["b",]



( terraform 0.12 )







setproduct(sets...): , .

setproduct(list("a","b"),list("c","d"))



# [["a","c"],["a","d"],["b","c"],["b","d"],]



( terraform 0.12 )







setunion(sets...): , .

, .







setunion(list("a","b"),list("c","d"))



# ["a","b","c","d",]



( terraform 0.12 )







slice(list, startindex, endindex):

slice(list("zero","one","two","three"),1,3)



# ["one","two"]









sort(list): , .

sort(list("d","c","a","b"))



# ["a","b","c","d",]









transpose(): , .

transpose(map("a",list("one","two"),"b",list("three","four")))



# {"four"=["b",] "one"=["a",] "three"=["b",] "two"=["a",]}









values(map): , .

values(map("name","debjeet","sex","male"))



# ["debjeet","male",]









zipmap(keyslist, valueslist): .

zipmap(list("name","sex"),list("debjeet","male"))



# {"name" = "debjeet" "sex" = "male"}















base64encode(string): Base64 .

base64encode("cloudaffaire")



# Y2xvdWRhZmZhaXJl









base64gzip(string): gzip, Base64.

base64gzip("cloudaffaire")



# H4sIAAAAAAAA/0rOyS9NSUxLS8wsSgUAAAD//wEAAP//38z9sQwAAAA=









base64decode(string): , Base64, .

base64decode("Y2xvdWRhZmZhaXJl")



# cloudaffaire









csvdecode(string): , CSV, , .

csvdecode("a,b,c\n1,2,3\n")



# [{"a"="1" "b"="2" "c"="3"},]



( terraform 0.12 )







jsonencode(): , JSON.

jsonencode(map("name","debjeet"))



# {"name":"debjeet"}









jsondecode(): JSON, .

jsondecode("{\"name\":\"debjeet\"}")



# {"name" = "debjeet"} ( terraform 0.12 )







urlencode(): URL .

urlencode("https://cloudaffaire.com/?s=terraform")



# https%3A%2F%2Fcloudaffaire.com%2F%3Fs%3Dterraform















dirname(string): , , .

dirname("/home/ec2-user/terraform/main.tf")



# /home/ec2-user/terraform









pathexpand(): , ~,

, .

pathexpand("~/.ssh/id_rsa")



# /home/ec2-user/.ssh/id_rsa









basename(string): , , , .

basename("/home/ec2-user/terraform/main.tf")



# main.tf









file(path):

file("/home/ec2-user/terraform/main.tf")



# content of main.tf









fileexists(path): , .

fileexists("/home/ec2-user/terraform/main.tf")



# true if main.tf exist



( terraform 0.12 )







filebase64(path): base64.

filebase64("/home/ec2-user/terraform/main.tf")



# main.tf base64. ( terraform 0.12 )







templatefile(path, vars): , .













formatdate(spec, timestamp): .

formatdate("MMM DD, YYYY", "2018-01-02T23:12:01Z")



# Jan 02, 2018



( terraform 0.12 ). https://www.terraform.io/docs/configuration/functions/formatdate.html







timeadd(timestamp, duration): , .

— , , «1,5 » «1 30 ».

: "ns", "us" (or "µs"), "ms", "s", "m", and "h".

, , "-2h5m".

timeadd("2019-05-10T00:00:00Z", "10m")



# 2019-05-10T00:10:00Z









timestamp():

timestamp()









IP







cidrhost(prefix, hostnum): IP- IP- .

cidrhost("10.0.0.0/16", 4)



# 10.0.0.4





cidrhost("10.0.0.0/16", -4)`` #



10.0.255.252`







cidrnetmask(prefix): IPv4-, CIDR, .

cidrnetmask("10.0.0.0/16")



# 255.255.0.0





cidrnetmask("10.0.0.0/24")



# 255.255.255.0









cidrsubnet(prefix, newbits, netnum): cidrsubnet IP- .

CIDR

newbits



— , .

netnum



— , , newbits

cidrsubnet("10.0.0.0/16",8,2)



# 10.0.2.0/24





cidrsubnet("10.0.0.0/24",8,1)



# 10.0.0.1/32









Salir de la consola terraform

exit









Para obtener una lista completa de las funciones de terraform, consulte la documentación de terraform a continuación.








All Articles