Function in terraform help you transform data, build dynamic values, and write smarter infrastructure code.
Terraform supports only built in function. Here are some details -
π String Functions:
β
lower() - lower("HELLO") β "hello"
β
upper() - upper("hello") β "HELLO"
β
replace() - replace("dev-app", "dev", "prod") β "prod-app"
β
substr()
β
trim() - trim(" hello ", " ") β "hello"
β
split() - split(",", "a,b,c") β ["a", "b", "c"]
β
join() - join(",", ["a", "b", "c"]) β "a,b,c"
β
chomp() - chomp("hello\n") β "hello" hashtag#cuts off the last newline character from a string.
π Numeric Functions
β
abs() - abs(-3) β 3 hashtag#Absolute value
β
max() - max(10, 5, 8) β 10
β
min() - min(10, 5, 8) β 5
β
ceil() - ceil(4.2) β 5 hashtag#Round UP a number
β
floor() - floor(4.8) β 4 hashtag#Round DOWN a number
β
sum() - sum([1, 2, 3]) β 6
π Collection Functions
β
length() - length([1,2,3]) β 3
β
concat() - concat([1,2], [3,4]) β [1,2,3,4]
β
merge() - merge({a=1}, {b=2}) β {a=1, b=2}
β
reverse() - reverse([1, 2, 3, 4]) β [4, 3, 2, 1]
β
toset() - toset(["a", "b", "a"]) β ["a", "b"]
- Converts a list into a set.
- A set has unique values (no duplicates) and order does not matter β tolist() - tolist(toset(["a", "b", "a"])) β ["a", "b"]
- Converts something (like a set or tuple) into a list.
- A list keeps order and allows duplicates
π Type Conversion
β
tonumber() - tonumber("10") β 10
β
tostring() - tostring(100) β "100"
β
tobool() -
- tobool("true") β true
- tobool(1) β true
- tobool(0) β false
π File Functions
β
file() - file("message.txt") β "Hello Nandan"
- Reads a file from your system and returns the text inside it.
- Use this when loading user-data scripts or config files. β fileexists() - fileexists("config.yaml") β true/false β dirname() - get folder / directory path β basename() - get the filename inside the directory
π Date/Time Functions
β
timestamp() - current date and time
β
formatdate() - formatdate("YYYY-MM-DD", timestamp()) hashtag#changes a date/time into a different format.
β
timeadd() - adds time (like days, hours, minutes) to a timestamp.
π Validation Functions
β can() - can(tolist("hello")) β false hashtag#cannot convert string "hello" to list
- It tells you if an expression will run without error.
- Use it to safely test something without breaking Terraform. β regex() - regex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", "nandan@gmail.com") hashtag#Check if a value looks like an email
- Checks if a string matches a regular expression pattern. β contains() - contains(["dev", "prod"], "dev") β true
- Returns true if an item exists inside a list. β startswith() - startswith("dev-app", "dev") β true hashtag#Returns true if the string starts with the given text. β endswith() - endswith("app-prod", "dev") β false hashtag#Returns true if the string ends with the given text.
π Lookup Functions
β lookup() - lookup({env = "dev", region = "us-east-1"}, "env", "default") β "dev"
- It returns a value from a map (keyβvalue pair).
- If the key does NOT exist β it returns a default value.
β
element() - element(["a", "b", "c"], 1) β "b" hashtag#returns the item at a specific index from a list.
β
index() - index(["dev", "stage", "prod"], "stage") β 1 hashtag#Find the Position of a Value in a List.
Refer: https://lnkd.in/gNHnBNDe
hashtag#Devops hashtag#Terraform hashtag#AWS
Thanks to Piyush sachdeva The CloudOps Community
Top comments (0)