I’m provisioning multiple EC2 instances (in this case 2) using the Terraform Module called ec-2 instance. In the output I want to receive IDs of provisioned EC2 instances.
locals {
app = {
instance1 = {
name = "instance1"
az = "eu-west-1a"
}
instance2 = {
name = "instance2"
az = "eu-west-1c"
}
}
}
module "ec2-instance" {
for_each = local.app
source = "terraform-aws-modules/ec2-instance/aws"
version = "5.5.0"
name = each.value.name
availability_zone = each.value.az
}
output "ids_of_instances" {
value = module.ec2-instance[*].id
}
I’m getting an error:
Error: Unsupported attribute
│
│ on main.tf line 96, in output "ids_of_instances":
│ 96: value = module.ec2-instance[*].id
│
│ This object does not have an attribute named "id".
Obviously I can try to use multiple outputs blocks like:
output "instance1" {
value = module.ec2-instance["instance1"].id
}
output "instance1" {
value = module.ec2-instance["instance2"].id
}
but the goal is to get this output from 1 single block. Any thoughts?
2
Answers
I believe the
for_each
will create a map of your modules. The documentation for the splat expression says that you should use a for expression with maps, instead of a splat expression.I’m not at a place where I can test this right now, I believe that would look like this:
Since the module is created with
for_each
, you can use the built-invalues
function to get all the values of the outputs you want: