0
0
Terraformcloud~10 mins

Collection functions (length, flatten, merge) in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Collection functions (length, flatten, merge)
Start with collections
Apply length()
Apply flatten()
Apply merge()
Get final result
Start with collections, then use length() to count items, flatten() to make nested lists flat, and merge() to combine maps.
Execution Sample
Terraform
locals {
  list_nested = [[1, 2], [3, 4]]
  map1 = {a = 1, b = 2}
  map2 = {b = 3, c = 4}
  len_list = length(local.list_nested)
  flat_list = flatten(local.list_nested)
  merged_map = merge(local.map1, local.map2)
}
This code counts items in a nested list, flattens it, and merges two maps.
Process Table
StepFunctionInputOutputExplanation
1length[[1, 2], [3, 4]]2Count top-level elements in the nested list
2flatten[[1, 2], [3, 4]][1, 2, 3, 4]Flatten nested list into single list
3merge{a=1,b=2}, {b=3,c=4}{a=1,b=3,c=4}Merge maps; later keys overwrite earlier ones
💡 All functions applied; final outputs obtained.
Status Tracker
VariableStartAfter lengthAfter flattenAfter merge
list_nested[[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]]
len_listundefined222
flat_listundefinedundefined[1, 2, 3, 4][1, 2, 3, 4]
merged_mapundefinedundefinedundefined{a=1,b=3,c=4}
Key Moments - 3 Insights
Why does length([[1, 2], [3, 4]]) return 2, not 4?
length() counts only the top-level elements. Here, the list has two elements: [1, 2] and [3, 4]. See execution_table step 1.
How does flatten() change the nested list?
flatten() removes one level of nesting, combining inner lists into a single list. See execution_table step 2.
When merging maps with overlapping keys, which value is kept?
The value from the later map overwrites the earlier one. In step 3, key 'b' changes from 2 to 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of length() at step 1?
A1
B2
C4
D0
💡 Hint
Check the 'Output' column in execution_table row with Step 1.
At which step does the list become flat?
AStep 2
BStep 1
CStep 3
DNo flattening occurs
💡 Hint
Look at the 'Function' and 'Output' columns in execution_table for flatten().
If map2 was {b=5, c=6} instead, what would merged_map's 'b' value be?
A2
B3
C5
DUndefined
💡 Hint
Refer to execution_table step 3 where later map keys overwrite earlier ones.
Concept Snapshot
length(collection) returns number of top-level items.
flatten(list_of_lists) merges nested lists into one list.
merge(map1, map2, ...) combines maps; later keys overwrite earlier.
Use these to count, simplify, and combine collections easily.
Full Transcript
This visual trace shows how Terraform collection functions work step-by-step. We start with a nested list and two maps. length() counts the top-level items in the list, returning 2 because there are two inner lists. flatten() then merges these inner lists into one flat list with four numbers. Finally, merge() combines two maps; when keys overlap, the value from the later map replaces the earlier one. This helps beginners see exactly how these functions change data collections in Terraform.