Terraform code to create a VPC , Subnet & Routes table with subnets association.
Tags: terraform, aws, devops
Published: 2022-09-28 16:09:00

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zti7nmvu67ugw9mwgda0.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trpbscp3tkx9ylz4gnkc.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gowb133y02bwoicnepq1.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4r0wcc4vlqt6hlmlp1v.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o226u66ybyyjdjlube4y.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95w3wk4csw4ryhqra1ib.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gyyk928vksmmqno13rh.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ng77962o4vl2ejpdogbk.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwzkl8wftrld5xk762lf.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0p5z4myu8s64rnclw9e3.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lt12bii7g0fb4v7locma.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jh5msrtb7ruzfb2ofxm9.png)

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

**VPC**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1l5kiss6fxo6jqa5f5ht.png)

**PUBLIC & PRIVATE SUBNETS WITH ROUTES TABLES**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7laj6vzjoo6ms6o7p7q.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jovejsvmfgv5xkqtgbg.png)

**ROUTES AND SUBNET ASSOCIATION OF PUBLIC SUBNET**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uonw6zf0yefmdsuc80wz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpzukx6o4ca2lv56dbyn.png)

**ROUTES AND SUBNET ASSOCIATION OF PRIVATE SUBNET**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uom450ron3kq2vqic0h0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssn3kfs5rhajdv00g829.png)

**INTERNET AND NAT GATWAYS**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oo6o29mwapgbec4zddyd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nwulc1tgb3spe56bfo3.png)