hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.75k stars 9.1k forks source link

[Bug]: aws_ec2_transit_gateway_route_tables returns a list intead of a set #26961

Open jdeluyck opened 1 year ago

jdeluyck commented 1 year ago

Terraform Core Version

1.3.0

AWS Provider Version

4.30.0

Affected Resource(s)

Expected Behavior

data.aws_ec2_transit_gateway_route_tables.ids should return a set per documentation

If you look at https://github.com/hashicorp/terraform-provider-aws/blob/3c691a0a3f6ae42c95e502fd6dd58270067921fc/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go#L14, it clearly shows it now returns a list. Diffing against older versions is difficult, as the module was renamed.

Actual Behavior

data.aws_ec2_transit_gateway_route_tables.ids returns a list

Relevant Error/Panic Output Snippet

╷
│ Error: Invalid for_each argument
│
│   on .terraform/route_table_data.tf line 95, in data "aws_ec2_transit_gateway_route_table" "rtb":
│   95:   for_each = data.aws_ec2_transit_gateway_route_tables.rtbs.ids
│     ├────────────────
│     │ data.aws_ec2_transit_gateway_route_tables.rtbs.ids is list of string with 1 element
│
│ The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type list of string.
╵

Terraform Configuration Files

data "aws_ec2_transit_gateway_route_tables" "rtbs" {
  filter {
    name   = "tag:Type"
    values = ["SomeType"]
  }
}

data "aws_ec2_transit_gateway_route_table" "rtb" {
  for_each = data.aws_ec2_transit_gateway_route_tables.rtbs.ids

  id = each.value
}

Steps to Reproduce

Upgrade to a recent aws provider (4.24 already has the issue)

Provider 3.75.2 is fine.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

No response

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

ewbankkit commented 1 year ago

The change from TypeSet to TypeList was done in v4.0.0 as part of https://github.com/hashicorp/terraform-provider-aws/pull/21219, but was not documented. This should be corrected.

jdeluyck commented 1 year ago

I think that aws_ec2_transit_gateway_route_table should be updated so this works when iterating over items from aws_ec2_transit_gateway_route_tables?

oniGino commented 1 year ago

I would say it should be left as a TypeSet and properly Pass in the full set that the API call returns https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-transit-gateway-route-tables.html

{
    "TransitGatewayRouteTables": [
        {
            "TransitGatewayRouteTableId": "tgw-rtb-0ca78a549EXAMPLE",
            "TransitGatewayId": "tgw-0bc994abffEXAMPLE",
            "State": "available",
            "DefaultAssociationRouteTable": true,
            "DefaultPropagationRouteTable": true,
            "CreationTime": "2018-11-28T14:24:49.000Z",
            "Tags": []
        },
        {
            "TransitGatewayRouteTableId": "tgw-rtb-0e8f48f148EXAMPLE",
            "TransitGatewayId": "tgw-0043d72bb4EXAMPLE",
            "State": "available",
            "DefaultAssociationRouteTable": true,
            "DefaultPropagationRouteTable": true,
            "CreationTime": "2018-11-28T14:24:00.000Z",
            "Tags": []
        }
    ]
}

If a Set of all matched was passed, there would be no need for multiple data objects