Complete the code to use a map with for_each to create instances.
resource "aws_instance" "example" { for_each = [1] ami = each.value.ami instance_type = each.value.instance_type }
The for_each argument expects a map or set of strings. Here, var.instances is the map variable holding instance details.
Complete the code to access the instance type from the map value.
instance_type = [1].instance_typeeach.key which is the map key, not the value.each.Within a for_each block, each.value accesses the current map value, which contains the instance details.
Fix the error in the for_each expression to correctly iterate over the map variable.
for_each = [1]The for_each expects a map or set of strings. Using var.instances directly is correct. Using var.instances[*] tries to convert to a list, which is invalid here.
Fill both blanks to create a map of instance names to AMI IDs using for_each.
output "instance_amis" { value = { for name, details in [1] : name => details.[2] } }
The for expression iterates over var.instances map. The value accessed is the AMI ID, so details.ami is correct.
Fill all three blanks to define multiple AWS instances using for_each with map variable and set tags.
resource "aws_instance" "web" { for_each = [1] ami = each.value.[2] instance_type = each.value.instance_type tags = { Name = [3] } }
for_each uses the map variable var.instances. The AMI is accessed by each.value.ami. The tag Name uses the map key each.key to name each instance uniquely.