0
0
Terraformcloud~20 mins

For expressions for transformation in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use an output block with value = local.prefixed_servers.