POKE ME for any consultancy

Friday, June 21, 2024

How To Create AWS VPC Using Terraform

 References: https://www.geeksforgeeks.org/create-aws-vpc-using-terraform/


Steps To Create AWS VPC Using Terraform

Step 1: First mention the provider and region in which you want to create VPC.

provider.tf

provider "aws" {
region = "us-east-1"
}

provider

Step 2 : Create a VPC . Here mention the CIDR block and give a name to the VPC .

create_vpc.tf

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"

tags = {
Name = "vpc"
}
}

vpc

Step 3 : Then create a subnet inside the VPC .

subnet.tf

resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch=true
tags = {
Name = "Public-Subnet"
}
}

subnet

Step 4 : But the subnet is isolated now . If you create an EC2 instance inside this subnet then you can not connect the EC2 instance as it is present in an isolated environment . So you need an internet gateway .

internet_gateway.tf

resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id

tags = {
Name = "IGW"
}
}

igw

Step 5 : Create a route table and associate the route table with subnet . Here in the route all the traffic is passed through internet gateway .

route_table.tf

resource "aws_route_table" "rt" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

tags = {
Name = "route_table"
}
}

rt

route_subnet_association.tf

resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.main.id
route_table_id = aws_route_table.rt.id
}

rt-subnet

Step 6 : After this execute all these terraform files using the below commands one by one .

terraform init
terraform plan
terraform apply

apply

Step 7: Check on your AWS console whether the VPC is created or not

check-vpc

Now if you want to delete all the resources created through terraform , then write this command .

terraform destroy

Conclusion

Here first we learned basics about AWS VPC and terraform . Then followed the steps to create an AWS VPC . Here inside the VPC we have created a public subnet , an internet gateway which helps the traffic to go in and out of the subnet and finally created a route table and associated with the subnet .

AWS VPC Using Terraform – FAQ’s

1. What is a subnet in VPC ?

When you are creating a VPC you provide a CIDR block (a range of IP address) . Like that , in subnet we provide a segment of IP addresses which helps the VPC to organize and manage its IP addresses .

2. What are NAT gateways used for ?

NAT gateways are used to give internet connectivity to the resources which are created using private subnet .

3. How public subnet is different from private subnet ?

Public subnet access internet(in and out) by using Internet gateway . But private subnets does not not use internet gateway to access internet , rather here NAT gateways are used for outbound internet access . We can not connect private subnet from outside .

4. How to ensure that EC2 instance inside the VPC gets internet connectivity ?

To ensure EC2 instance gets internet connectivity , you should place the instance in a public subnet that has a route to internet gateway in its route table.

5. What is the use of route table in VPC ?

Route table contains a set of routes which is used to determine where network traffic should be directed in the VPC .


No comments:

Post a Comment