AWS Architecture Guide: How to Build a Custom VPC with Public & Private Subnets and Launch EC2

Deploying production cloud workloads directly into the default AWS VPC is a common security pitfall. A resilient cloud infrastructure requires an isolated network environment with distinct public and private subnets, granular routing, and strict security groups. In this guide, we walk through building a production-ready Virtual Private Cloud (VPC) on AWS and deploying an EC2 instance from scratch.

architecture - network specifications
VPC CIDR: 10.0.0.0/16
Region: ap-south-1 (Mumbai)
Public Subnet: 10.0.1.0/24 (AZ: ap-south-1a) - Bastion / Web Server
Private Subnet: 10.0.2.0/24 (AZ: ap-south-1b) - Database / Internal Apps

Step 1: Create the Custom VPC

Log in to the AWS Management Console, navigate to the VPC Dashboard, and click Create VPC.

  • Select VPC only.
  • Name tag: gaganweb-prod-vpc
  • IPv4 CIDR block: 10.0.0.0/16
  • Leave tenancy as Default and click Create VPC.

Once created, select the VPC, click Actions → Edit VPC settings, and check both Enable DNS resolution and Enable DNS hostnames.

Step 2: Provision Public & Private Subnets

Navigate to Subnets → Create subnet, select gaganweb-prod-vpc, and configure:

  1. Public Subnet:
    • Subnet name: public-subnet-1a
    • Availability Zone: ap-south-1a
    • IPv4 subnet CIDR block: 10.0.1.0/24
  2. Private Subnet:
    • Subnet name: private-subnet-1b
    • Availability Zone: ap-south-1b
    • IPv4 subnet CIDR block: 10.0.2.0/24
Note: Select public-subnet-1a, click Actions → Edit subnet settings, and check Enable auto-assign public IPv4 address so instances in this subnet receive public IP assignments automatically.

Step 3: Attach an Internet Gateway (IGW) & Configure Routing

An isolated VPC has no default path to the global internet. We must attach an Internet Gateway:

  1. Go to Internet Gateways → Create internet gateway. Name it gaganweb-igw.
  2. Select the new gateway, click Actions → Attach to VPC, and choose gaganweb-prod-vpc.
  3. Go to Route Tables → Create route table:
    • Name: public-rt
    • VPC: gaganweb-prod-vpc
  4. Open public-rt, go to the Routes tab, and click Edit routes:
    • Add Route: Destination 0.0.0.0/0 → Target Internet Gateway (select gaganweb-igw).
  5. Go to the Subnet associations tab → Edit subnet associations → select public-subnet-1a and save.

Step 4: Configure the Security Group (Stateful Firewall)

Navigate to Security Groups → Create security group:

  • Security group name: web-server-sg
  • VPC: gaganweb-prod-vpc

Add the following Inbound rules:

Type Protocol Port Range Source
SSH TCP 22 Your_Public_IP/32
HTTP TCP 80 0.0.0.0/0
HTTPS TCP 443 0.0.0.0/0

Step 5: Launch & Verify Your EC2 Instance

Go to the EC2 Dashboard → Launch instances:

  1. Name: gaganweb-web-01
  2. AMI: Ubuntu Server 24.04 LTS (HVM), SSD Volume Type.
  3. Instance type: t2.micro or t3.micro (Free tier eligible).
  4. Key pair: Create or select an existing .pem key pair.
  5. Network settings (Click Edit):
    • VPC: gaganweb-prod-vpc
    • Subnet: public-subnet-1a
    • Auto-assign public IP: Enable
    • Select existing security group: web-server-sg
  6. Click Launch instance.

Step 6: Connect and Test from Linux CLI

Once the instance displays Running status with 2/2 checks passed, connect via terminal:

bash - local machine
# Set strict private key permissions
chmod 400 my-key.pem

# Connect to the instance via public IPv4
ssh -i "my-key.pem" ubuntu@YOUR_EC2_PUBLIC_IP

After logging into the instance, verify default routing and install a lightweight web service to verify external traffic flow:

bash - ec2 instance shell
# Verify default gateway points to VPC router (.1)
ip route show

# Update apt repositories and install Nginx
sudo apt update && sudo apt install -y nginx

# Confirm service is active and listening on port 80
sudo systemctl status nginx --no-pager
sudo ss -tulpn | grep :80

Open your browser and navigate to http://YOUR_EC2_PUBLIC_IP. You will see the standard Nginx welcome page, confirming your custom VPC routing, Internet Gateway, and security group rules are operating as intended.