DEV Community

Cover image for Terraform code to create a VPC , Subnet & Routes table with subnets association.
Surya Shankar
Surya Shankar

Posted on • Edited on

Terraform code to create a VPC , Subnet & Routes table with subnets association.

In a DevOps scenario, building AWS services via tools like Terraform is a more scalable and automated approach to cloud resource provisioning.

Understanding AWS VPCs

An AWS VPC is a single network that allows you to launch AWS services within a single isolated network. Technically, an AWS VPC is almost the same as owning a datacenter but with built-in additional benefits of scalability, fault-tolerance, unlimited storage, etc.

Image description

Building the Terraform Configuration for an AWS VPC

1. To start, create a folder to store your Terraform configuration files in. This tutorial will create a folder called terraform-ec2 in your home directory.

The Terraform configuration below:

  1. Creates a VPC
  2. Creates an Internet Gateway and attaches it to the VPC to allow traffic within the VPC to be reachable by the outside world.
  3. Creates a public and private subnet
  4. Subnets are networks within networks. They are designed to help network traffic flow be more efficient and provide smaller, more manageable ‘chunks’ of IP addresses
  5. Creates a route table for the public and private subnets and associates the table with both subnets
  6. Creates a NAT Gateway to enable private subnets to reach out to the internet without needing an externally routable IP address assigned to each resource.

Create a file inside ~/terraform-vpc directory, paste in the following code, and name it as provider.tf to define the AWS provider
prerequisite :- Ist of all you have to create a admin IAM user in aws

Image description

The VPC.tf file contains all VPC credentials such as cidr range vpc name etc.

Image description

internetgateway.tf file contains internet gateway name with vpc attachment...
Here we can attach it with vpc using vpc id. [ aws_vpc.NewVpc.id]

Image description

subnets.tf file contains all details of Public Subnets and Private Subnets such as cidr range , inside which vpc we have to put our subnets using vpc id.
Here inside public subnet we have used map_public_ip_on_launch = true in order to enable auto assign public ip inside this subnet

Image description

Pub-routes.tf file contains routes table with subnet association of public sunbet.

Image description

nat.tf file contain a nat gatway which will be present inside our public subnet thats why here we have attach public subnet id.

Image description

priv-routes.tf file contains routes table with subnet association of private sunbet.

Image description

Run the terraform init command in the same directory. The terraform init command initializes the plugins and providers which are required to work with resources.

Image description

Now, run the terraform plan command. This is an optional, yet recommended action to ensure your configuration’s syntax is correct and gives you an overview of which resources will be provisioned in your infrastructure

Image description

Next, tell Terraform actually to provision the AWS VPC and resources using terraform apply. When you invoke terraform apply, Terraform will read the configuration (.tf) and the other files to compile a configuration. It will then send that configuration up to AWS as instructions to build the VPC and other components.

Image description

Image description

Now our resources are created successfully lets verify it in aws console

VPC
Image description

PUBLIC & PRIVATE SUBNETS WITH ROUTES TABLES
Image description
Image description

ROUTES AND SUBNET ASSOCIATION OF PUBLIC SUBNET
Image description
Image description

ROUTES AND SUBNET ASSOCIATION OF PRIVATE SUBNET
Image description
Image description

INTERNET AND NAT GATWAYS
Image description
Image description

Top comments (0)