0
0
Terraformcloud~10 mins

Iterator variable in Terraform - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an iterator variable in a Terraform for expression.

Terraform
locals {
  names = ["app", "db", "cache"]
  upper_names = [for [1] in local.names : upper([1])]
}
Drag options to blanks, or click blank then click option'
Aitem
Bvalue
Cname
Delement
Attempts:
3 left
💡 Hint
Common Mistakes
Using reserved keywords like 'value' or 'element' as iterator variable names.
Not using the same iterator variable name consistently inside the for expression.
2fill in blank
medium

Complete the code to create a map using an iterator variable in Terraform.

Terraform
locals {
  ports = [80, 443, 8080]
  port_map = { for [1] in local.ports : [1] => "open" }
}
Drag options to blanks, or click blank then click option'
Aitem
Bport
Cvalue
Dp
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic name like 'value' which can be confusing.
Using different variable names in the key and value parts.
3fill in blank
hard

Fix the error in the iterator variable usage in this Terraform for expression.

Terraform
locals {
  servers = ["web1", "web2"]
  server_ids = [for [1] in local.servers : "id_${server}" ]
}
Drag options to blanks, or click blank then click option'
Ainstance
Bsrv
Cserver
Dservers
Attempts:
3 left
💡 Hint
Common Mistakes
Using the plural form 'servers' as the iterator variable but referencing 'server' inside the expression.
Using a different variable name inside the expression than the iterator variable.
4fill in blank
hard

Fill both blanks to create a map of server names to their uppercase versions using iterator variables.

Terraform
locals {
  servers = ["app", "db", "cache"]
  upper_map = { for [1] in local.servers : [2] => upper([2]) }
}
Drag options to blanks, or click blank then click option'
Asrv
Bserver
Cs
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the key and value parts.
Using a variable name that is not defined as the iterator.
5fill in blank
hard

Fill all three blanks to create a list of strings combining iterator variable and index in Terraform.

Terraform
locals {
  names = ["alpha", "beta", "gamma"]
  combined = [for [2], [1] in local.names : "$[1]_$[2]"]
}
Drag options to blanks, or click blank then click option'
Aname
Bidx
Ci
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of iterator variables.
Using different variable names inside the string than declared.