Open In App

How To Create Route Table In AWS Using Terraform ?

Terraform is a popular IAAC (Infrastructure as a Code) tool used in automation to create, manage, modify, update, and destroy any cloud resources and cloud environment. Terraform supports any cloud provider, including AWS, Microsoft Azure, GCP, Oracle, Alibaba, IBM, Salesforce, etc.

Here, in this guide, I am going to discuss the AWS Route Table first, and I will discuss deeply what Terraform is. After that, I will walk you through different steps to write a Terraform script and execute the scripts. By using these Terraform scripts, we can create our custom route table and associate this route table with the AWS subnet.



Understanding Of Primary Terminologies

What is the AWS Route Table?

In Amazon Web Services (AWS), a route table is a set of rules that controls network traffic and determines where the network traffic within a virtual private cloud (VPC) should be directed. It controls network traffic and it figures out where network traffic in a virtual private cloud ought to be coordinated

Default Route Table: When we create a VPC, a default route table is automatically created for it. This default route table contains a local route allowing communication within the VPC. With this default route table, subnets that are not explicitly associated with a custom route table use the default route table by default.



Custom Route Tables: You can create custom route tables to control the routing behavior for our configured subnets within your VPC. This allows for more granular control over how traffic flows in your network.

What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool that allows users to define and provision infrastructure resources in a declarative manner using Hashicorp language, also called HCL (Hashicorp Configure Language).When working with AWS, Terraform enables you to create, manage, and update resources such as EC2 instances, Security groups, VPCs, route tables, internet gateways (IGW), S3 buckets, and relational databases efficiently and consistently.

Though It is an infrastructure-as-a code software tool used primarily by DevOps teams to automate various infrastructure tasks, created by Hashicorp. Users define and provide data centers, infrastructure using a declarative configuration language known as Hashicorp Configuration Language.

block diagram of Terraform

Terraform has two types of flows

Workflow: In workflow we have three stages, first one is write the terraform code for what cloud resources we want and second one is plan it gives a blueprint of cloud resources which we desired to create and finally third one is apply it simply starts building the terraform code.

Terraform Workflow

Execution flow: In execution flow of terraform,we have four stages 1st one is format,it sets the indentation of terraform code. 2nd one is validate,it checks the terraform code whether there is a syntax error or not and corrections of cloud resources specifications. 3rd one is plan,gives a blueprint of desired cloud resources. 4th one is apply,simple executes the terraform code.

Terraform Execution flow

Step-By-Step Process To Create To Create AWS Route table Using Terraform

Here, i am going to create a aws Route table by launching AWS ec2 instance.

Step 1: Setting Up AWS account

Step 2: Launch EC2 instance And Install Terraform

configuration ofge – 8gb with root volume type gp2

connect this instance with any CLI terminal by using SSH

ssh -i  "pemfile" ec2-user@<instance-public-ip address>compute-1.amazonaws.com

connecting ec2 instance

Make sure we have to install terraform in our ec2 instance

sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform

Terraform installation setup

Step 3: Set Up AWS Credentials

aws configure

aws configuration

Terraform Scripts

In Terraform, the terraform block is used to configure settings related to the Terraform execution environment itself. This block allows you to define various options and configurations that affect how Terraform behaves when executing your infrastructure code.

Step 4: Create the terraform directory

mkdir terraform
cd terraform

terraform directory

Provider Block

In Terraform, the “provider” block is a fundamental construct used to define and configure the provider responsible for managing the resources in a specific cloud or infrastructure environment. Providers are plugins in Terraform that interface with APIs of various services or platforms to create, read, update, and delete resources.

Step 5: terraform script for aws provider

vi provider.tf

#provider

provider “aws” {

region = “us-east-1” # Specify your desired AWS region

}

provider file

Step 6: Terraform scripts for VPC & IGW

before configure the route table we have to configure the terraform code for creating VPC (virtual private code) and internet gateway(IGW).

vi vpc.tf
resource "aws_vpc" "demo-vpc" {
cidr_block = "10.0.0.0/16" # Define your VPC CIDR block
instance_tenancy = "default"
tags = {
Name = "demo-vpc"
}
}

Terraform code for VPC creation

 vi igw.tf

resource “aws_internet_gateway” “demo-igw” {

vpc_id = aws_vpc.demo-vpc.id

}

Terraform code for IGW creation

Step 7: Terraform code for Route table

vi Routetable.tf

resource “aws_route_table” “demo-route” {

vpc_id = aws_vpc.demo-vpc.id

route {

cidr_block = “0.0.0.0/0”

gateway_id = aws_internet_gateway.demo-igw.id # Reference the ID of the internet gateway

}

tags = {

Name = “route to internet”

}

}

Terraform code for Route table creation

Step 8: Execute terraform files i.e., provider.tf,vpc.tf,igw.tf,route-table.tf

terraform init

terraform init

terraform fmt
terraform validate
terraform plan

terraform fmt,validate,plan

terraform apply --auto-approve

terraform apply –auto-approve

resources are created and added

Resources Block

In Terraform, the “resources” block is not a specific construct like the provider or terraform blocks. Instead, it’s a common terminology used to refer to the section of a Terraform configuration where you define the infrastructure resources what you want to desire.

finally,We created the VPC,IGW and Route Table resources successfully.

VPC-virtual private cloud

IGW-internet gateway

Route table

Route table in aws using terraform – FAQ’s

What is a Route Table in AWS?

Ans: A route table in AWS is a networking resource used within Virtual Private Clouds (VPCs) to determine where network traffic should be directed. It consists of route entries that specify the destination for traffic and where it should be sent.

What is Terraform?

Ans: Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It allows users to define and provision infrastructure resources using declarative configuration files. Terraform supports multiple cloud providers, including AWS, Azure, and Google Cloud Platform.

How can I create a Route Table in AWS using Terraform?

Ans: To create a route table in AWS using Terraform, you need to write Terraform configuration files defining the necessary resources, such as the VPC,IGW i(internet gateway),subnet, and route table. You then initialize Terraform, fmt (format), validate, plan and apply the configuration, and Terraform will provision the route table in your AWS account.

How do I associate a Subnet with a Route Table using Terraform?

Ans: To associate a subnet with a route table using Terraform, you can use the aws_route_table_association resource. You specify the subnet ID and the route table ID in the Terraform configuration code to associate the subnet with the route table.


Article Tags :