0
votes

I have created 4 subnets in my vpc, 2 of them public and 2 of them private. I need to associate 2 public subnets to a one route table and 2 private subnets to another route table. Looking at the docs, aws_route_table_association seems like accepts only one subnet_id.

How do I add multiple subnets as show in this pic?

enter image description here

Associate route table to subnets

resource "aws_route_table_association" "public-test" {
  subnet_id =                                         -> I need to add 2 public subnets here
  route_table_id = aws_route_table.public-test.id
}

resource "aws_route_table_association" "private-test" {
  subnet_id =                                          -> I need to add 2 private subnets here
  route_table_id = aws_route_table.private-test.id
}

Here are the subnets and routes:

Create Subnet

resource "aws_subnet" "public-test-a" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.0/28"
  map_public_ip_on_launch = true
  availability_zone = var.AZ[0]

  tags = {
    Name = "public-test-a"
  }
}

resource "aws_subnet" "public-test-b" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.16/28"
  map_public_ip_on_launch = true
  availability_zone = var.AZ[1]

  tags = {
    Name = "public-test-b"
  }
}

resource "aws_subnet" "private-test-a" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.32/28"
  availability_zone = var.AZ[0]

  tags = {
    Name = "private-test-a"
  }
}


resource "aws_subnet" "private-test-b" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.48/28"
  availability_zone = var.AZ[1]

  tags = {
    Name = "private-test-b"
  }
}

Create route table

resource "aws_route_table" "public-test" {
  vpc_id = aws_vpc.vpc-test-02.id

  route {
    cidr_block = "10.0.0.0/26"
  }

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id =aws_internet_gateway.myIG-test-02.id
  }

  tags = {
    Name = "public-test"
  }
}

resource "aws_route_table" "private-test" {
  vpc_id = aws_vpc.vpc-test-02.id

  route {
    cidr_block = "10.0.0.0/26"
  }

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_nat_gateway.myNat-test-02.id
  }
}
1

1 Answers

2
votes

You can simple declare two route table association resources.


resource "aws_subnet" "public_test_a" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.0/28"
  map_public_ip_on_launch = true
  availability_zone = var.AZ[0]

  tags = {
    Name = "public-test-a"
  }
}

resource "aws_subnet" "public-test-b" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.16/28"
  map_public_ip_on_launch = true
  availability_zone = var.AZ[1]

  tags = {
    Name = "public-test-b"
  }
}


resource "aws_route_table_association" "public-test-a" {
  subnet_id = aws_subnet.public-test-a.id # first subnet
  route_table_id = aws_route_table.public-test.id
}

resource "aws_route_table_association" "public-test-b" {
  subnet_id = aws_subnet.public-test-b.id # second subnet
  route_table_id = aws_route_table.public-test.id
}

resource "aws_route_table" "public-test" {
  vpc_id = aws_vpc.vpc-test-02.id

  route {
    cidr_block = "10.0.0.0/26"
  }

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id =aws_internet_gateway.myIG-test-02.id
  }

  tags = {
    Name = "public-test"
  }
}

Also it is considered good practice to follow naming conventions. Quoting the docs

Use _ (underscore) instead of - (dash) in all: resource names, data source names, variable names, outputs. Beware that actual cloud resources have many hidden restrictions in their naming conventions. Some cannot contain dashes, some must be camel cased. These conventions refer to Terraform names themselves. Only use lowercase letters and numbers.