Search code examples
amazon-web-servicesterraformamazon-vpcterraform-provider-aws

Terraform: AWS attach NAT gateway to one subnet among two


I am creating two public subnets in two different AZ's using Terraform count.index which is looping through two different CIDR's and AZ's in a list. Up to this is correct. Now I have to create a NAT gateway and attach it to only one of the public subnets created above. When passing subnet_id = aws_subnet.this[count.index].id it is attaching NAT gateway to both the public subnets.

How do I attach a NAT gateway to only one of the public subnets among two using terraform?


Solution

  • From what I can see if it is using count.index your NAT gateway resource must be using the count parameter.

    If you want only one NAT Gateway do not use the count parameter in the NAT Gateway resource, if you want the first subnet from this resource instead use subnet_id = aws_subnet.this[0].id.