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.
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:
- Creates a VPC
- Creates an Internet Gateway and attaches it to the VPC to allow traffic within the VPC to be reachable by the outside world.
- Creates a public and private subnet
- 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
- Creates a route table for the public and private subnets and associates the table with both subnets
- 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
The VPC.tf file contains all VPC credentials such as cidr range vpc name etc.
internetgateway.tf file contains internet gateway name with vpc attachment...
Here we can attach it with vpc using vpc id. [ aws_vpc.NewVpc.id]
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
Pub-routes.tf file contains routes table with subnet association of public sunbet.
nat.tf file contain a nat gatway which will be present inside our public subnet thats why here we have attach public subnet id.
priv-routes.tf file contains routes table with subnet association of private sunbet.
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.
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
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.
Now our resources are created successfully lets verify it in aws console
PUBLIC & PRIVATE SUBNETS WITH ROUTES TABLES
ROUTES AND SUBNET ASSOCIATION OF PUBLIC SUBNET
Top comments (0)