1

I think multiple people have asked the same question but my condition is different. I am taking input from the user for the vpc region, cidr value even the public subnet segment too. I have to attach all my public subnet to the default route table and private subnets to the diff route table . can you help me in how to attach them .

provider "aws" { region = var.region } resource "aws_vpc" "app_vpc" { cidr_block = var.vpc_cidr enable_dns_support = true enable_dns_hostnames = true tags = { Name = var.vpc_name } } # create igw resource "aws_internet_gateway" "app_igw" { vpc_id = aws_vpc.app_vpc.id } data "aws_availability_zones" "available" { state = "available" } #provision public subnet resource "aws_subnet" "public_subnet_01" { vpc_id = aws_vpc.app_vpc.id cidr_block = var.public_subnet_01 availability_zone = data.aws_availability_zones.available.names[0] tags = { Name = "public_subnet_01" } depends_on = [aws_vpc_dhcp_options_association.dns_resolver] } resource "aws_subnet" "public_subnet_02" { vpc_id = aws_vpc.app_vpc.id cidr_block = var.public_subnet_02 availability_zone = data.aws_availability_zones.available.names[1] tags = { Name = "public_subnet_02" } depends_on = [aws_vpc_dhcp_options_association.dns_resolver] } resource "aws_subnet" "public_subnet_03" { vpc_id = aws_vpc.app_vpc.id cidr_block = var.public_subnet_03 availability_zone = data.aws_availability_zones.available.names[2] tags = { Name = "public_subnet_03" } depends_on = [aws_vpc_dhcp_options_association.dns_resolver] } #default route table resource "aws_default_route_table" "default" { default_route_table_id = aws_vpc.app_vpc.default_route_table_id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.app_igw.id } } resource "aws_route_table_association" "default_association_01" { subnet_id = [aws_subnet.public_subnet_01.id, aws_subnet.public_subnet_02.id, aws_subnet.public_subnet_03.id] route_table_id = aws_vpc.app_vpc.default_route_table_id } 

I am getting error in adding multiple subnet so can u please help here :)

1
  • I can't see where the non-public Route Table is defined in that template. Commented Sep 20, 2020 at 22:36

1 Answer 1

1

aws_route_table_association takes only one subnet as an input, not a list of subnets.

If you want to create the associations using your list, you can use for_each:

resource "aws_route_table_association" "default_association_01" { for_each = toset([aws_subnet.public_subnet_01.id, aws_subnet.public_subnet_02.id, aws_subnet.public_subnet_03.id]) subnet_id = each.key route_table_id = aws_vpc.app_vpc.default_route_table_id } 

The above assumes that everything else is correct. There could be still some errors in your code which aren't apparent yet.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response, i am getting below error : The "for_each" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the for_each depends on.
thank you for all the help but my code works fine if I declare rote table association differently but when in for_each it gives target error :(
@AnkitSingh If you use for_each in one resource, other resources are usually also affected, because you have to reference aws_route_table_association differently now, then before. So maybe this is why you see new erros?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.