Hello community.
Goal
I want to output all the ids from a resource created with for_each.
Problem
When I output the ec_deployment_traffic_filter with [ * ], it shows the resources created. No problem. But when I output [ * ].id or [ * ].name, I receive the error This object does not have an attribute named "name"
Help
So I have to main questions:
- What I am doing wroing?
- What is the best way to iterate and show all the ids?
The Code
This works ok, it the output shows me the 3 traffic filters created.
variable "elastic_traffic_filters" {
type = set(string)
description = "Names and VPCs for configuring the Elastic Cloud traffic filters"
default = [ "vpce-123", "vpce-456", "vpc-789" ]
}
resource "ec_deployment_traffic_filter" "this" {
for_each = var.elastic_traffic_filters
name = "Allow traffic from ${replace(each.key, " ", "_")}"
region = var.aws_region
type = "vpce"
rule {
source = "${each.value}"
}
depends_on = [aws_vpc_endpoint.vpce]
}
output "traffic_filters" {
value = ec_deployment_traffic_filter.this <<<< ----------
}
---- OK: RESULT AS EXPECTED ------
traffic_filters = {
+ vpce-123 = {
+ description = null
+ id = (known after apply)
+ include_by_default = false
+ name = "Allow traffic from vpce-123"
+ region = "us-east-1"
+ rule = [
+ {
+ azure_endpoint_guid = ""
+ azure_endpoint_name = ""
+ description = ""
+ id = (known after apply)
+ source = "vpce-123"
},
]
+ timeouts = null
+ type = "vpce"
}
+ vpce-456 = {
+ description = null
+ id = (known after apply)
+ include_by_default = false
+ name = "Allow traffic from vpce-456"
+ region = "us-east-1"
+ rule = [
+ {
+ azure_endpoint_guid = ""
+ azure_endpoint_name = ""
+ description = ""
+ id = (known after apply)
+ source = "vpce-456"
},
]
+ timeouts = null
+ type = "vpce"
}
+ vpce-789 = {
+ description = null
+ id = (known after apply)
+ include_by_default = false
+ name = "Allow traffic from vpce-789"
+ region = "us-east-1"
+ rule = [
+ {
+ azure_endpoint_guid = ""
+ azure_endpoint_name = ""
+ description = ""
+ id = (known after apply)
+ source = "vpce-789"
},
]
+ timeouts = null
+ type = "vpce"
}
}
However, if I modify the output to print me the attributes, I have error.
variable "elastic_traffic_filters" {
type = set(string)
description = "Names and VPCs for configuring the Elastic Cloud traffic filters"
default = [ "vpce-123", "vpce-456", "vpc-789" ]
}
resource "ec_deployment_traffic_filter" "this" {
for_each = var.elastic_traffic_filters
name = "Allow traffic from ${replace(each.key, " ", "_")}"
region = var.aws_region
type = "vpce"
rule {
source = "${each.value}"
}
depends_on = [aws_vpc_endpoint.vpce]
}
output "traffic_filters" {
value = ec_deployment_traffic_filter.this[*].id <<<< ----------
}
---- ERROR ------
Error: Unsupported attribute
│
│ on outputs.tf line 19, in output "traffic_filters":
│ 19: value = module.ec_cluster.traffic_filters[*].id
│
│ This object does not have an attribute named "id".
Finally, If I use this other output, it works fine too.
variable "elastic_traffic_filters" {
type = set(string)
description = "Names and VPCs for configuring the Elastic Cloud traffic filters"
default = [ "vpce-123", "vpce-456", "vpc-789" ]
}
resource "ec_deployment_traffic_filter" "this" {
for_each = var.elastic_traffic_filters
name = "Allow traffic from ${replace(each.key, " ", "_")}"
region = var.aws_region
type = "vpce"
rule {
source = "${each.value}"
}
depends_on = [aws_vpc_endpoint.vpce]
}
output "traffic_filters" {
value = ec_deployment_traffic_filter.this["vpc-123"].id <<<< ----------
}
---- OK: RESULT AS EXPECTED ------
Changes to Outputs:
+ traffic_filters = (known after apply)
Thanks for the help!