3.5. Types / Functions
Preparation
Create a new directory for this exercise:
mkdir -p $LAB_ROOT/basics/types
cd $LAB_ROOT/basics/types
Documentation for the built-in functions can be found at: https://www.terraform.io/docs/language/functions/index.html
Step 3.5.1: String interpolation
Create a new file named strings.tf and add the following content:
locals {
counter = 5
}
output "counter" {
value = "Counter is ${local.counter}"
}
Run init and apply:
terraform init
terraform apply
Step 3.5.2: Working with lists
Create a new file named lists.tf and add the following content:
locals {
fibonacci = [0,1,1,2,3,5,8,13]
}
output "element_5" {
value = local.fibonacci.5 // or local.fibonacci[5]
}
output "fibonacci" {
value = join("/", local.fibonacci)
}
Run apply:
terraform apply
Step 3.5.3: Working with maps
Create a new file named maps.tf and add the following content:
locals {
tags = {
env = "prod"
app = "nginx"
}
extra_tags = {
platform = "azure"
}
}
output "tag_list" {
value = keys(local.tags)
}
output "full_tags" {
value = merge(local.tags, local.extra_tags)
}
Run apply:
terraform apply
Step 3.5.4: Working with external YAML/JSON files
Terraform provides built-in functions to access external YAML and JSON files.
Create a new file named project.yaml and add the following content:
components:
- name: "project-name"
metadata:
annotations:
app: "example"
Create a new file named yaml.tf and add the following content:
locals {
yaml_file = yamldecode(file("project.yaml"))
}
output "app" {
value = local.yaml_file.components.0.metadata.annotations.app
}
The example above could also be shortened using output chaining to the following snippet but readability suffers:
output "app2" {
value = yamldecode(file("project.yaml")).components.0.metadata.annotations.app
}
Run apply:
terraform apply
Explanation
The statement
locals {
yaml_file = yamldecode(file("project.yaml"))
}
loads the file project.yaml and assigns it to the local variable yaml_file.
The statement
output "app" {
value = local.yaml_file.components.0.metadata.annotations.app
}
creates an output variable, whereas the part components.0.metadata.annotations.app refers to the YAML structure
components:
- name: "project-name"
metadata:
annotations:
app: "example"
The components is a list of which we take the first (0th) element and access sub-attributes.