Table of contents
- Introduction
- What is AWS VPC
- Benefits of AWS VPC
- How to create VPC using terraform
- Conclusion
Introduction
Networking serves as the foundation of any infrastructure in cloud computing. AWS Virtual Private Cloud (VPC) is a fundamental component of Amazon Web Services (AWS), providing a secure and scalable environment for deploying resources. Whether you’re a cloud enthusiast or a DevOps engineer, understanding VPC is crucial for designing robust architectures.
What is AWS VPC
AWS Virtual Private Cloud (VPC) is a isolated section of the AWS Cloud where you can you deploy your resources, such as EC2 instances, in a secure virtual network. It enables complete control over your network, including IP address range, subnets, routing, and security.
Key Components of AWS VPC
Subnets
Public Subnets: Subnets with internet access via an Internet Gateway.
Private Subnets: Isolated for secure resources without direct internet access.
Route Tables
Direct traffic within and outside the VPC using rules. For example:
Public subnets use routes to an Internet Gateway.
Private subnets route traffic through a NAT Gateway for internet access.
Internet Gateway (IGW) and NAT Gateway
IGW: Facilitates internet communication for public resources.
NAT Gateway: Enables outbound internet traffic for private resources.
Security Groups (SGs)
Act as virtual firewalls for your instances, controlling inbound and outbound traffic.
Network Access Control Lists (NACLs)
Stateless firewalls that provide subnet-level traffic control.
Creating Your First AWS VPC with Terraform
Here’s how to create a VPC using Terraform resources:
Set up Terraform: Ensure Terraform is installed on your system.
Terraform Configuration File: Create a file named vpc.tf and include the following:
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "MyVPC"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-1a"
tags = {
Name = "PublicSubnet"
}
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "InternetGateway"
}
}
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "PublicRouteTable"
}
}
resource "aws_route" "default_route" {
route_table_id = aws_route_table.public_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_rt.id
}
Deploy the Configuration:
Initialize Terraform: terraform init
Plan the infrastructure: terraform plan
Apply the changes: terraform apply -auto-approve
If you check your AWS console a VPC, public subnet, internet gateway, and routes would have been created. Do not forget to delete the resources created by running:
terraform destroy -auto-approve
Use Cases for AWS VPC
Hosting Web Applications: Isolate backend databases in private subnets while keeping the front end in public subnets.
Conclusion
AWS VPC provides a secure, customizable foundation for deploying cloud resources. With its robust features and flexibility, it’s an essential tool for building modern, scalable network architectures.
Top comments (0)