Terraform functions







In this post, we will discuss Terraform features. The Terraform language includes a number of built-in functions that you can call from expressions to transform and combine values. The general syntax for function calls is the name of the function, followed by the arguments, separated by commas, in parentheses.







<function_name>(<argument1>, <argument2>, โ€ฆ<argumentN>)
      
      





Next, we're going to explain Terraform's features with examples. We will be using the terraform console for this demo.







Terraform features:







Open the terraform console

terraform console









Numeric functions







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









Exit terraform console

exit









For a complete list of terraform functions, see the terraform documentation below.








All Articles