AWS route updated every time

I have a script that creates a VPC with public and private subnets, a internet gateway and some nat gateways.

The script works but if I run it again with no changes, it updates the routes in the route tables.

Why is this happening?

This is my route resource

# Create Private routes
resource "aws_route" "private_az1_route" {
  route_table_id                                    = aws_route_table.private_az1_rt.id
  destination_cidr_block                            = "0.0.0.0/0"
  gateway_id                                        = aws_nat_gateway.az1_nat_gw.id
}

This is the output from apply

 # aws_route.private_az1_route will be updated in-place
  ~ resource "aws_route" "private_az1_route" {
      + gateway_id             = "nat-0fe8273a37e902043"
        id                     = "r-rtb-0e72816e6977d7b651080289494"
      - nat_gateway_id         = "nat-0fe8273a37e902043" -> null
        # (4 unchanged attributes hidden)
    }
1 Like

Hi @pjbeard99,

It seems like in the underlying AWS API there are two different ways to assign a NAT gateway to a route, but when reading this data back from AWS it gets normalized to always be assigned to using nat_gateway_id. The provider doesn’t seem to understand that these two are equivalent and so it’s generating a plan to switch it back to how you wrote it, instead of how it is expressed in the response from the API.

If so, an immediate solution would be to change you configuration to use nat_gateway_id instead, so that your configuration will match how the API or provider is expecting this argument to be set.

Might also be worth opening an issue for this in the AWS provider repository if there isn’t already one about it, because ideally the provider would notice that these two situations are equivalent and avoid switching to a different way to write the same information.

2 Likes

Hi @apparentlymart

Many thanks for taking the time to reply and the solution.

This worked in my situation as well. I now get a match message: “No changes. Your infrastructure matches the configuration.”