0
0
Terraformcloud~10 mins

For_each for map-based instances 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 use a map with for_each to create instances.

Terraform
resource "aws_instance" "example" {
  for_each = [1]
  ami           = each.value.ami
  instance_type = each.value.instance_type
}
Drag options to blanks, or click blank then click option'
Avar.instances
Bvar.instance_list
Caws_instance.example
Dlocal.instances
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list variable instead of a map for for_each.
Referencing a resource instead of a variable.
2fill in blank
medium

Complete the code to access the instance type from the map value.

Terraform
instance_type = [1].instance_type
Drag options to blanks, or click blank then click option'
Avar.instances
Blocal.instance
Ceach.value
Deach.key
Attempts:
3 left
💡 Hint
Common Mistakes
Using each.key which is the map key, not the value.
Using variable names instead of each.
3fill in blank
hard

Fix the error in the for_each expression to correctly iterate over the map variable.

Terraform
for_each = [1]
Drag options to blanks, or click blank then click option'
Avar.instances
Bvar.instances[*]
Cvar.instances[]
Dvar.instances.all
Attempts:
3 left
💡 Hint
Common Mistakes
Using list or index syntax on a map variable.
Trying to access a non-existent property like .all.
4fill in blank
hard

Fill both blanks to create a map of instance names to AMI IDs using for_each.

Terraform
output "instance_amis" {
  value = { for name, details in [1] : name => details.[2] }
}
Drag options to blanks, or click blank then click option'
Avar.instances
Bvar.instance_list
Cami
Dinstance_type
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list variable instead of a map for iteration.
Accessing the wrong property like instance_type instead of ami.
5fill in blank
hard

Fill all three blanks to define multiple AWS instances using for_each with map variable and set tags.

Terraform
resource "aws_instance" "web" {
  for_each = [1]
  ami           = each.value.[2]
  instance_type = each.value.instance_type
  tags = {
    Name = [3]
  }
}
Drag options to blanks, or click blank then click option'
Avar.instances
Bami
Ceach.key
Dlocal.instances
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list variable instead of a map for for_each.
Accessing the wrong property for AMI.
Using a fixed string instead of each.key for the Name tag.