📖 Scenario: You are managing infrastructure configurations using Terraform. You need to organize and analyze lists and maps of server names and their attributes.
🎯 Goal: Build a Terraform configuration that uses collection functions length, flatten, and merge to work with lists and maps.
📋 What You'll Learn
Create a variable with a nested list of server names
Create a variable with two maps of server attributes
Use length to find the number of servers in the flattened list
Use flatten to convert the nested list into a single list
Use merge to combine the two maps into one
💡 Why This Matters
🌍 Real World
Managing infrastructure often requires organizing lists of servers and their properties. Terraform collection functions help simplify this management.
💼 Career
Understanding how to manipulate collections in Terraform is essential for infrastructure as code roles, enabling efficient and scalable configuration management.
Progress0 / 4 steps
1
Create a nested list of server names
Create a variable called server_names with the nested list value [["web1", "web2"], ["db1", "db2"]].
Terraform
Hint
Use variable "server_names" { default = ... } to define the nested list.
2
Create two maps of server attributes
Create two variables called server_attrs1 and server_attrs2 with these exact maps: { web1 = "active", db1 = "active" } and { web2 = "inactive", db2 = "active" } respectively.
Terraform
Hint
Define each variable with variable "name" { default = { ... } } syntax.
3
Use length and flatten functions
Create a local variable called flat_servers that uses flatten(var.server_names). Then create another local variable called server_count that uses length(local.flat_servers).
Terraform
Hint
Use locals { flat_servers = flatten(var.server_names) } and then server_count = length(local.flat_servers).
4
Merge the two server attribute maps
Add a local variable called all_server_attrs that merges var.server_attrs1 and var.server_attrs2 using the merge function.
Terraform
Hint
Use all_server_attrs = merge(var.server_attrs1, var.server_attrs2) inside the locals block.
Practice
(1/5)
1. What does the Terraform function length() do when applied to a list?
easy
A. Returns the number of items in the list
B. Combines all items into a single string
C. Flattens nested lists into one list
D. Merges the list with another list
Solution
Step 1: Understand the purpose of length()
The length() function counts how many elements are in a list, map, or string.
Step 2: Apply to a list context
When used on a list, it returns the total number of items inside that list.
Final Answer:
Returns the number of items in the list -> Option A
Quick Check:
length(list) = count of items [OK]
Hint: Count items with length() to get list size fast [OK]
Common Mistakes:
Thinking length() merges lists
Confusing length() with flatten()
Assuming length() returns the list itself
2. Which of the following is the correct syntax to merge two maps map1 and map2 in Terraform?
easy
A. length(map1, map2)
B. flatten(map1, map2)
C. map1 + map2
D. merge(map1, map2)
Solution
Step 1: Identify the function for combining maps
The merge() function is used to combine two or more maps into one.
Step 2: Check syntax correctness
The correct syntax is merge(map1, map2). Other options either use wrong functions or invalid operators.
Final Answer:
merge(map1, map2) -> Option D
Quick Check:
merge() combines maps correctly [OK]
Hint: Use merge() with parentheses and commas for maps [OK]
Common Mistakes:
Using + operator to merge maps
Confusing flatten() with merge()
Passing maps without commas
3. What is the output of this Terraform expression?
flatten([["a", "b"], ["c"], ["d", "e"]])
medium
A. [["a", "b"], ["c"], ["d", "e"]]
B. ["a", "b", "c", "d", "e"]
C. ["a", "b", ["c"], "d", "e"]
D. Error: flatten() only works on maps
Solution
Step 1: Understand flatten() function
The flatten() function takes a list of lists and combines all inner lists into one single list.
Step 2: Apply flatten() to the given nested list
Input is [["a", "b"], ["c"], ["d", "e"]]. Flattening merges all inner lists into ["a", "b", "c", "d", "e"].
Final Answer:
["a", "b", "c", "d", "e"] -> Option B
Quick Check:
flatten(nested list) = single list [OK]
Hint: flatten() removes inner list brackets to one list [OK]
Common Mistakes:
Thinking flatten() returns original nested list
Expecting flatten() to work on maps
Confusing flatten() with merge()
4. Given the code snippet:
locals {
map1 = { a = 1, b = 2 }
map2 = { b = 3, c = 4 }
result = merge(map1, map2)
}
What is the value of local.result?
medium
A. { a = 1, b = 3, c = 4 }
B. { a = 1, b = 2, c = 4 }
C. { a = 1, b = 5, c = 4 }
D. Error: duplicate keys not allowed in merge
Solution
Step 1: Understand merge() behavior with duplicate keys
When keys overlap, the value from the last map argument overrides earlier ones.
Step 2: Apply merge to map1 and map2
map1 has b=2, map2 has b=3. The merged map uses b=3 from map2, so result is { a=1, b=3, c=4 }.
Final Answer:
{ a = 1, b = 3, c = 4 } -> Option A
Quick Check:
merge() overrides duplicate keys with last map [OK]
Hint: Last map's keys override earlier ones in merge() [OK]
Common Mistakes:
Assuming values add up for duplicate keys
Expecting error on duplicate keys
Confusing merge() with flatten()
5. You have a list of maps:
list_of_maps = [
{ name = "Alice", age = 30 },
{ name = "Bob", age = 25 },
{ name = "Carol", age = 27 }
]
Which Terraform expression correctly merges all maps into one map with all keys and values combined?
hard
A. length(list_of_maps)
B. flatten(list_of_maps)
C. merge(list_of_maps...)
D. merge(list_of_maps)
Solution
Step 1: Understand how to merge multiple maps in a list
To merge all maps inside a list, you use the splat operator ... to unpack the list as arguments to merge().
Step 2: Check each option
merge(list_of_maps...) uses merge(list_of_maps...) which correctly merges all maps. flatten(list_of_maps) tries flatten on maps (invalid), C returns count, D passes list directly (invalid).
Final Answer:
merge(list_of_maps...) -> Option C
Quick Check:
Use merge() with ... to combine list of maps [OK]
Hint: Use ... to unpack list when merging maps [OK]
Common Mistakes:
Passing list directly to merge() without ...
Using flatten() on maps
Confusing length() with merge()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.