DEV Community

Nandan K
Nandan K

Posted on

Terraform Functions

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"]

  1. Converts a list into a set.
  2. A set has unique values (no duplicates) and order does not matter βœ… tolist() - tolist(toset(["a", "b", "a"])) β†’ ["a", "b"]
  3. Converts something (like a set or tuple) into a list.
  4. A list keeps order and allows duplicates

πŸ“Œ Type Conversion
βœ… tonumber() - tonumber("10") β†’ 10
βœ… tostring() - tostring(100) β†’ "100"
βœ… tobool() -

  1. tobool("true") β†’ true
  2. tobool(1) β†’ true
  3. tobool(0) β†’ false

πŸ“Œ File Functions
βœ… file() - file("message.txt") β†’ "Hello Nandan"

  1. Reads a file from your system and returns the text inside it.
  2. 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

  1. It tells you if an expression will run without error.
  2. 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
  3. Checks if a string matches a regular expression pattern. βœ… contains() - contains(["dev", "prod"], "dev") β†’ true
  4. 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"

  1. It returns a value from a map (key–value pair).
  2. 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)