Skip to content

Terraform Functions and Conditional Expressions

Links: 113 Terraform Index


  • Some of the common functions are
    • file: read data from a file
    • length: determine the number of elements in a list or map
    • toset: convert a list to a set
  • We can test functions using an interactive console.
    • We can use the terraform console using terraform console
    • Terraform console loads the state associated to the configuration directory by default.
    • It also loads variables in the configuration files.
    • attachments/Pasted image 20230114192353.jpg
  • Numeric functions
    • Expansion is needed when using variables.
    • attachments/Pasted image 20230114192605.jpg
  • String functions
    • attachments/Pasted image 20230114192840.jpg
    • split is used to split a string based on the separator. It returns a list.
    • join is used to convert a list to a string.
    • attachments/Pasted image 20230114192916.jpg
  • List functions
    • attachments/Pasted image 20230114193039.jpg
    • contains is case sensitive
  • Map functions
    • attachments/Pasted image 20230114193129.jpg
    • We can provide a default argument to the lookup function
    • attachments/Pasted image 20230114193207.jpg

Conditional Expressions

  • Assigning value to a variable based on condition
    • Generating a random password of atleast 8 characters where length of password comes from the user.
    • attachments/Pasted image 20230114193611.jpg

Last updated: 2023-01-14