Member-only story

Dynamically Adding Route Table Rules Using Terraform

Vinayak Pandey
2 min readFeb 24, 2021

In this post, we’ll see how we can read route table rules from a tfvars file and then create corresponding rules for a route table.

Pre-requisite: Terraform should be installed and access_key/IAM role should be configured to create resources in your AWS account. I am using Terraform version 0.14.6 but code should be working fine with 0.12+

Step 1: Download code given at https://github.com/vinycoolguy2015/awslambda/tree/master/terraform_aws_route_table

Step 2: In main.tf, comment out following 2 lines. We’ll add them back once we have created the VPC.

#create_nat_routes     = true  
#create_gateway_routes = true

Now run terraform plan and terraform apply. This will create a VPC,some private subnets and a route table.

Step 3: Now create and attach an internet gateway to this VPC, manually.

Step 4: Now we’ll add some route table rules which will use this internet gateway. In the main.tf, uncomment this line

create_gateway_routes = true

Now run terraform plan and terraform apply and our routes will be added.

You can modify the routes destination in terraform.tfvars file.

--

--

Vinayak Pandey
Vinayak Pandey

Written by Vinayak Pandey

Experienced Cloud Engineer with a knack of automation. Linkedin profile: https://www.linkedin.com/in/vinayakpandeyit/

No responses yet