0
0
Terraformcloud~30 mins

Why complex types matter in Terraform - See It in Action

Choose your learning style9 modes available
Why complex types matter
📖 Scenario: You are setting up a cloud infrastructure using Terraform. You need to organize your resources using complex data types to keep your configuration clean and manageable.
🎯 Goal: Build a Terraform configuration that uses complex types like maps and lists to define multiple server instances with their properties.
📋 What You'll Learn
Create a map variable with server names as keys and their IP addresses as values
Add a list variable to hold server roles
Use a for expression to create a list of server descriptions combining name, IP, and roles
Output the final list of server descriptions
💡 Why This Matters
🌍 Real World
Cloud engineers use complex types in Terraform to manage multiple resources and their properties cleanly, avoiding repetitive code.
💼 Career
Understanding complex types and for expressions is essential for writing scalable and maintainable infrastructure as code in professional cloud roles.
Progress0 / 4 steps
1
Create a map variable for servers
Create a Terraform variable called servers of type map with these exact entries: "web1" = "10.0.0.1", "db1" = "10.0.0.2", and "cache1" = "10.0.0.3".
Terraform
Need a hint?

Use variable block with type = map(string) and set default to the given map.

2
Add a list variable for server roles
Add a Terraform variable called roles of type list with these exact values: "frontend", "backend", and "cache".
Terraform
Need a hint?

Define a variable block with type = list(string) and set default to the given list.

3
Create a list of server descriptions
Create a local variable called server_descriptions that uses a for expression to combine each server name and IP from var.servers with the corresponding role from var.roles by matching the index. Format each description as "Name: , IP: , Role: ".
Terraform
Need a hint?

Use a for expression with idx, name over sort(keys(var.servers)) to build the list.

4
Output the server descriptions
Add an output block called server_descriptions that outputs the local variable local.server_descriptions.
Terraform
Need a hint?

Use an output block with value = local.server_descriptions.