Open In App

How To Create Route Table In AWS Using Terraform ?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

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.

  • write
  • plan
  • apply
Terraform Workflow

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.

  • format
  • validate
  • plan
  • apply
Terraform Execution flow

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

  • Go to AWS Management Console
  • Login by using with your credentials.
  • Now you need to generate access key to authenticate Terraform with your AWS account
  • In AWS management console in home screen search for IAM ( Identity and Access Management ) service. Choose Users and click on Add user.
  • Give a username and select administration access as the access type. Attach necessary permissions to the user.
  • Review the user details and create the user. Now you will see the access key ID and secret access key. Save this information securely as it will be required when configure Terraform.

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

connecting ec2 instance

  • Now install terraform by using following commands

Make sure we have to install terraform in our ec2 instance

  • For this we need to download terraform hashicorp related packages and repo.
  • I take these keys and repo from terraform official page.
  • https://developer.hashicorp.com/terraform/install#linux
  • to install terraform in our OS follow the below commands.
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

Terraform installation setup

Step 3: Set Up AWS Credentials

  • Configure your AWS credentials either through environment variables, the AWS CLI, or a shared credentials file. This allows Terraform to authenticate with your AWS account.
aws configure
aws configuration

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

  • Create a new directory for your Terraform configuration files and navigate into it.
  • Inside this directory, create a terraform files with .tf extension to write the terraform scripts.
mkdir terraform
cd terraform
terraform directory

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

  • inside this created terraform directory , create a new file to write the terraform scripts for Route table and define the Terraform aws configuration for your route table.
vi provider.tf

#provider

provider “aws” {

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

}

provider file

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).

  • For this create a file for VPC
  • create a file for 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

Terraform code for VPC creation

 vi igw.tf

resource “aws_internet_gateway” “demo-igw” {

vpc_id = aws_vpc.demo-vpc.id

}

Screenshot-2024-03-16-152718

Terraform code for IGW creation

Step 7: Terraform code for Route table

  • Inside this directory, create a new file to write the terraform code for Route table and define the Terraform configuration for your 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

Terraform code for Route table creation

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

  • we should initialize the terraform in backend.
  • firstly, we make ensure the terraform files shoube in declarative manner.
  • secondly, we have to check the validation of terraform code doesn’t have any syntax and resources errors.
  • then plan these terraform code and terraform files i mean check the cloud resources we are going to create.
  • finally,apply the the terraform code. this is the most important step we are going to execute because this is the step terraform will create the cloud resources we want
terraform init
terraform init

terraform init

  • execute the below commands to format,validate and plan the terraform scripts
terraform fmt
terraform validate
terraform plan
terraform fmt,validate,plan

terraform fmt,validate,plan

  • now,execute these below command to apply terraform scripts with auto approve.
  • When we execute this command then automatically our infrastructure will build automatically.
terraform apply --auto-approve

terraform apply --auto-approve

terraform apply –auto-approve

resources are created and added

resources are created and added

  • see the terraform apply is complete Resources: 3 added,0 changed,0 destroyed

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.

  • Here are the outcomes of those. The below screenshot illustrates the resource creation.
  • these are resources that are created.

VPC-virtual private cloud

VPC-virtual private cloud

IGW-internet gateway

IGW-internet gateway

Route table

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads