Transforming a List of Server Names with Terraform For Expressions
📖 Scenario: You are managing cloud servers and have a list of server names. You want to create a new list where each server name is transformed to include a prefix that indicates the environment.
🎯 Goal: Build a Terraform configuration that uses a for expression to transform a list of server names by adding the prefix "prod-" to each name.
📋 What You'll Learn
Create a variable called server_names with the exact list: ["app1", "db1", "cache1"]
Create a variable called env_prefix with the exact value "prod-"
Use a for expression to create a local variable called prefixed_servers that adds env_prefix to each server name in server_names
Output the prefixed_servers list
💡 Why This Matters
🌍 Real World
Cloud engineers often need to manage lists of resources and apply consistent naming conventions automatically. Using for expressions in Terraform helps transform data efficiently.
💼 Career
Understanding for expressions in Terraform is essential for infrastructure as code roles, enabling automation and reducing manual errors in cloud resource management.
Progress0 / 4 steps
1
Create the server_names variable
Create a Terraform variable called server_names with the exact list ["app1", "db1", "cache1"].
Terraform
Hint
Use variable "server_names" block with default set to the list.
2
Create the env_prefix variable
Create a Terraform variable called env_prefix with the exact string value "prod-".
Terraform
Hint
Use a variable block with type = string and default = "prod-".
3
Create the prefixed_servers local variable using a for expression
Create a local variable called prefixed_servers that uses a for expression to add the env_prefix to each server name in var.server_names.
Terraform
Hint
Use locals block with a for expression inside square brackets to transform the list.
4
Output the prefixed_servers list
Create an output called prefixed_servers that outputs the local variable prefixed_servers.
Terraform
Hint
Use an output block with value = local.prefixed_servers.
Practice
(1/5)
1. What does a for expression in Terraform primarily do?
easy
A. Creates a new resource in the cloud
B. Transforms each item in a list or map into a new list or map
C. Deletes resources based on a condition
D. Runs a shell command on the local machine
Solution
Step 1: Understand the purpose of for expressions
For expressions in Terraform are used to transform collections like lists or maps into new collections by applying a transformation to each item.
Step 2: Differentiate from other Terraform actions
Creating or deleting resources and running shell commands are not done by for expressions; they are handled by resource blocks or provisioners.
Final Answer:
Transforms each item in a list or map into a new list or map -> Option B
Quick Check:
For expressions = transform collections [OK]
Hint: For expressions change each item in a list/map to a new form [OK]
Common Mistakes:
Thinking for expressions create or delete resources
Confusing for expressions with provisioners
Assuming for expressions run commands
2. Which of the following is the correct syntax for a simple for expression transforming a list [1, 2, 3] to their squares in Terraform?
easy
A. [for n in [1, 2, 3] : n * n]
B. [for n : n * n in [1, 2, 3]]
C. [for n in [1, 2, 3] n * n]
D. [for n in [1, 2, 3] => n * n]
Solution
Step 1: Recall Terraform for expression syntax
The correct syntax is: [for <variable> in <list> : <expression>]. [for n in [1, 2, 3] : n * n] matches this exactly.
Step 2: Check other options for syntax errors
[for n : n * n in [1, 2, 3]] has wrong order, [for n in [1, 2, 3] n * n] misses colon, [for n in [1, 2, 3] => n * n] uses => which is invalid in Terraform for expressions.
Final Answer:
[for n in [1, 2, 3] : n * n] -> Option A
Quick Check:
Correct syntax = [for var in list : expr] [OK]
Hint: Remember syntax: for var in list : expression [OK]
Common Mistakes:
Omitting the colon ':' after the list variable
Using arrow '=>' instead of colon
Placing expression before 'in' keyword
3. Given the Terraform expression:
[for s in ["apple", "banana", "cherry"] : upper(s) if length(s) > 5]
What is the resulting list?
medium
A. ["banana", "cherry"]
B. ["APPLE", "BANANA", "CHERRY"]
C. ["BANANA", "CHERRY"]
D. ["APPLE"]
Solution
Step 1: Identify filtering condition
The expression filters strings with length greater than 5. "banana" (6) and "cherry" (6) qualify; "apple" (5) does not.
Step 2: Apply transformation function
For qualifying strings, upper(s) converts them to uppercase: "BANANA", "CHERRY".