0
0
Terraformcloud~5 mins

For expressions for transformation in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to change a list or map into a new list or map with different values. For expressions in Terraform help you do this easily by looping over each item and making a new one.
When you have a list of server names and want to add a prefix to each name.
When you want to convert a list of numbers into strings for display.
When you need to create a map from a list by using each item as a key.
When you want to filter and change values in a list before using them.
When you want to build a new list of resources based on existing data.
Config File - main.tf
main.tf
variable "server_names" {
  type    = list(string)
  default = ["app1", "app2", "app3"]
}

output "prefixed_servers" {
  value = [for name in var.server_names : "prod-${name}"]
}

output "server_name_lengths" {
  value = { for name in var.server_names : name => length(name) }
}

This Terraform file defines a variable server_names as a list of strings.

The first output prefixed_servers uses a for expression to add the prefix "prod-" to each server name, creating a new list.

The second output server_name_lengths uses a for expression to create a map where each server name is a key and its length is the value.

Commands
This command initializes the Terraform working directory. It downloads the necessary provider plugins and prepares Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/random... - Installing hashicorp/random v3.4.3... - Installed hashicorp/random v3.4.3 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command applies the Terraform configuration to create or update infrastructure. The -auto-approve flag skips the confirmation prompt.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create No changes. Your infrastructure matches the configuration. Outputs: prefixed_servers = [ "prod-app1", "prod-app2", "prod-app3", ] server_name_lengths = { "app1" = 4 "app2" = 4 "app3" = 4 } Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the value of the output named prefixed_servers, which is the list of server names with the prefix added.
Terminal
terraform output prefixed_servers
Expected OutputExpected
[ "prod-app1", "prod-app2", "prod-app3" ]
This command shows the value of the output named server_name_lengths, which is a map of server names to their lengths.
Terminal
terraform output server_name_lengths
Expected OutputExpected
{ "app1" = 4 "app2" = 4 "app3" = 4 }
Key Concept

If you remember nothing else from this pattern, remember: for expressions let you create new lists or maps by transforming each item in an existing list or map.

Common Mistakes
Using for expressions without specifying the output structure correctly
Terraform expects a list or map output, so missing the colon or wrong syntax causes errors.
Always use the syntax [for item in list : expression] for lists or {for key, value in map : key => value} for maps.
Trying to use for expressions outside of Terraform expressions blocks
For expressions only work inside Terraform expressions like variables, outputs, or resource arguments.
Use for expressions only inside Terraform configuration expressions, not as standalone commands.
Summary
Use for expressions to transform each item in a list or map into a new list or map.
Apply the Terraform configuration with terraform apply to see the transformed outputs.
Use terraform output to view the results of your for expressions.