Launching an infrastructure on AWS with custom VPC,Subnet, Internet Gateway and Routing Tables using Terraform
What is a VPC?
A Virtual Private Cloud (VPC) is a logically isolated network in the AWS cloud that you can customize. You can control the IP address range, create subnets, and configure routing tables and network gateways. A VPC is an essential component of any AWS infrastructure, as it allows you to create and manage your own network topology in the cloud.
What is a Private VPC?
A Private VPC is a VPC that does not have a direct connection to the internet. Private VPCs can only be accessed by resources within the VPC, or through a VPN connection to the VPC. Private VPCs are often used to host resources that should not be publicly accessible, such as databases or backend servers.
What is a Public VPC?
A Public VPC is a VPC that has a direct connection to the internet. Resources in a public VPC can be accessed from the internet, and can also access resources on the internet. Public VPCs are often used to host web servers or other resources that need to be publicly accessible.
What is NAT?
Network Address Translation (NAT) is a technique used to enable resources in a private subnet to access the internet. NAT translates the private IP address of a resource to a public IP address, allowing the resource to communicate with resources on the internet.
What is Internet gateway?
An internet gateway enables communication over the internet, and a virtual private network (VPN) connection enables communication with your corporate network.
Now, let’s discuss how to set up a VPC with private and public subnets with NAT using Terraform.
Configure the AWS Provider
To start, we need to configure the AWS provider in Terraform. We will use the aws
provider in this example.
Create the VPC
We will create the VPC and set up the IP address range for the VPC.
Create the Subnets
Next, we will create the subnets. We will create one public subnet and one private subnet.
Create the Internet Gateway
To enable resources in the public subnet to access the internet, we need to create an internet gateway.
Create the Route Table
Next, we will create the route table for the VPC. We will associate the public subnet with the internet gateway and the private subnet with a NAT gateway.
Create the NAT Gateway
To enable resources in the private subnet to access the internet, we need to create a NAT gateway.
Create the NAT Gateway
To enable resources in the private subnet to access the internet, we need to create a NAT gateway.
With these resources, we have set up a VPC with private and public subnets and a NAT gateway to enable private subnet resources to access the internet.
Terraform Run: VPC
steps to run terraform code include:
- terraform init:
This step initializes the plugins and providers, which are needed to work with the various resources we “coded.”
- terraform plan:
confirms our configuration code syntax is correct and provides an overview of which resources will be created in your infrastructure . As more complex code and modifications are done to your code,
- terraform apply:
This is the actual launch step that will create your infrastructure. .
Confirmation
If all goes well in the previous sections, you can log into the AWS console and confirm all resources were successfully created.
VPC
Subnets
public subnet with the internet gateway
internet Gateways
private subnet with the internet gateway
Terraform makes it easy to create and manage infrastructure in AWS. we covered how to create a VPC with private and public subnets and a NAT gateway using Terraform. you can host resources in a private subnet that are not accessible from the internet, while also enabling resources in a public subnet to access the internet.