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
}
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"
}
}
vi igw.tf
resource “aws_internet_gateway” “demo-igw” {
vpc_id = aws_vpc.demo-vpc.id
}
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”
}
}
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
- execute the below commands to format,validate and plan the terraform scripts
terraform fmt
terraform validate
terraform 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
- see the terraform apply is complete Resources: 3 added,0 changed,0 destroyed
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.