Terraform - How to Associate Multiple Subnets to the Same Route Table?

Terraform - How to Associate Multiple Subnets to the Same Route Table?
In: Terraform NetDevOps AWS

In this quick read, we're diving straight into a common question many of us have when using Terraform, How to Associate Multiple Subnets to a Route Table? This task may seem straightforward, but there are some tricks to make the process efficient and error-free. So, without further ado, let's get to it!

Terraform Code

Here, we're using a simple Terraform script to set up a VPC, define some subnets, and then associate those subnets to a route table.

provider "aws" {
  region  = "eu-west-1"
}

locals {
  subnets = {
    "subnet-1" = { cidr_block = "10.210.1.0/24", availability_zone = "eu-west-1a", tag_name = "vpc-1-private-subnet-1a" }
    "subnet-2" = { cidr_block = "10.210.2.0/24", availability_zone = "eu-west-1b", tag_name = "vpc-1-private-subnet-2b" }
    "subnet-3" = { cidr_block = "10.210.3.0/24", availability_zone = "eu-west-1c", tag_name = "vpc-1-private-subnet-3c" }
  }
}

resource "aws_vpc" "vpc_test" {
  cidr_block = "10.210.0.0/16"

  tags = {
    Name = "test-vpc-1"
  }
}

resource "aws_subnet" "private-subnets" {
  for_each = local.subnets

  cidr_block        = each.value.cidr_block
  vpc_id            = aws_vpc.vpc_test.id
  availability_zone = each.value.availability_zone

  tags = {
    Name = each.value.tag_name
  }
}

resource "aws_route_table" "private-rt" {
  vpc_id = aws_vpc.vpc_test.id

  tags = {
    Name = "test-vpc-1-route-table"
  }
}

resource "aws_route_table_association" "rt-association-private" {
  for_each       = aws_subnet.private-subnets
  subnet_id      = each.value.id
  route_table_id = aws_route_table.private-rt.id
}

The script first defines an AWS provider and then creates resources for the VPC and subnets. The heart of the script—and what we'll focus on in this article—is the aws_route_table_association resource.

aws_route_table_association in Detail

resource "aws_route_table_association" "rt-association-private" {
  for_each       = aws_subnet.private-subnets
  subnet_id      = each.value.id
  route_table_id = aws_route_table.private-rt.id
}
  • for_each = aws_subnet.private-subnets - This line iterates over each subnet created earlier in the script.
  • subnet_id = each.value.id - For each iteration, it gets the ID of the current subnet.
  • route_table_id = aws_route_table.private-rt.id - Here, it specifies which route table these subnets should be associated with.

This aws_route_table_association resource takes care of looping through each of our defined subnets and associating them with the same route table we've created. And that's how you can associate multiple subnets to a single route table easily.

Closing Up

So, that's a wrap! With just a few lines of code, you've got multiple subnets all pointing to a single route table. Easy, right?

Table of Contents
Written by
Suresh Vina
Tech enthusiast sharing Networking, Cloud & Automation insights. Join me in a welcoming space to learn & grow with simplicity and practicality.
Comments
More from Packetswitch
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.