Karthik
Karthik

Reputation: 349

aws_route_table_association doesn't work for all private* subnets - terraform

I have created couple of subnets using the resource block below.

resource "aws_subnet" "private" {
  count                   = "${length(var.private_subnet)}"
  vpc_id                  = "${aws_vpc.vpcname.id}"
  cidr_block              = "${var.private_subnet[count.index]}"
  availability_zone       = "${data.aws_availability_zones.available.names[count.index]}"
  map_public_ip_on_launch = false

  tags {
    Name        = "${var.private}-${format("%01d", count.index + 1)}"
    Environment = "${terraform.workspace}"
  }
} 

After which I created eip's, nat's, private route table, added route to the private route table and lastly, when I try to associate the private route table to private* subnets terraform works partially i.e. out of 4 subnets only 2 private subnets are associated with the private route table.

resource "aws_route_table_association" "private-subnet-association" {
  subnet_id      = "${element(aws_subnet.private.*.id, count.index)}"
  route_table_id = "${aws_vpc.vpcname.private_route_table.id}"
}

What do I have to change to apply the association to all the private subnets.

subnet_id      = "${element(aws_subnet.private*.?.id, count.index)}"

Upvotes: 0

Views: 912

Answers (1)

thisguy123
thisguy123

Reputation: 945

Where is the variable nat_gateway_eip coming from? You need to reference by the full resource in the format TYPE.NAME.ATTR

It should look something like this... ${element(aws_eip.RESOURCE_NAME.*.id, count.index)}

Or you need to have whatever module/file that is creating the nat_gateway output the eip_id value and reference it from remote state.

Edit: Updating answer as question has changed.

You need to create multiple associations to associate this route table to different subnets.

Your current subnet association block is not iterating anything. The syntax RESOURCE.NAME.*.ATTR doesn't mean "wildcard", it's for use with a resource containing a count.

Upvotes: 2

Related Questions