Creating a VPC with Terraform: Building Your AWS Network Foundation

mdshamsfiroz
3 min readOct 27, 2024

--

In this blog post, we’ll walk through the process of creating a Virtual Private Cloud (VPC) in AWS using Terraform. A VPC is a fundamental component of your AWS infrastructure, providing a logically isolated section of the AWS cloud where you can launch resources in a virtual network that you define.

Prerequisites

Before we begin, ensure you have:

  1. Terraform installed on your local machine
  2. AWS CLI configured with your credentials
  3. Basic understanding of VPC concepts

Setting Up the Terraform Configuration

First, create a new directory for your Terraform project and initialize it:

mkdir terraform-vpc
cd terraform-vpc
terraform init

Now, create a file named main.tf and add the following content:

provider "aws" {
region = "us-west-2" # Change this to your preferred region
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-igw"
}
}
resource "aws_subnet" "public_1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
tags = {
Name = "Public Subnet 1"
}
}
resource "aws_subnet" "private_1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-west-2b"
tags = {
Name = "Private Subnet 1"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "Public Route Table"
}
}
resource "aws_route_table_association" "public_1" {
subnet_id = aws_subnet.public_1.id
route_table_id = aws_route_table.public.id
}

This configuration creates:

  1. A VPC with a CIDR block of 10.0.0.0/16
  2. An Internet Gateway attached to the VPC
  3. A public subnet and a private subnet
  4. A route table for the public subnet, with a route to the Internet Gateway

Applying the Terraform Configuration

Now that we have our configuration, let’s apply it:

terraform apply

Terraform will show you a plan of what it’s going to create. Type ‘yes’ to proceed.

Understanding the Configuration

Let’s break down the key components of our Terraform configuration:

  1. VPC: We’re creating a VPC with a CIDR block of 10.0.0.0/16, which gives us 65,536 available IP addresses.
  2. Internet Gateway: This allows communication between our VPC and the internet.
  3. Subnets: We’re creating two subnets — one public and one private. The public subnet can be used for resources that need direct internet access, while the private subnet is for resources that should not be directly accessible from the internet.
  4. Route Table: We’re creating a route table for the public subnet and adding a route that directs internet-bound traffic to the Internet Gateway.

Best Practices and Considerations

  1. CIDR Block Planning: Carefully plan your CIDR blocks to ensure you have enough IP addresses for your current and future needs.
  2. Multiple Availability Zones: For high availability, consider creating subnets in multiple Availability Zones.
  3. Network ACLs: Consider adding Network ACLs for an additional layer of security.
  4. VPC Flow Logs: Enable VPC Flow Logs to capture information about IP traffic going to and from network interfaces in your VPC.
  5. Tagging: Use meaningful tags to easily identify and manage your resources.

Extending the Configuration

You can extend this basic configuration to include:

  • Additional subnets
  • NAT Gateways for private subnets
  • VPC Endpoints for accessing AWS services without leaving the Amazon network
  • VPN or Direct Connect for connecting to on-premises networks

Conclusion

Creating a VPC with Terraform allows you to define your network infrastructure as code, making it easily reproducible and manageable. This approach ensures consistency across environments and enables version control of your infrastructure.

So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

Happy coding! Happy learning!

--

--

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet