DevOps Peach

AWS Basics · Networking with Amazon VPC · Lesson 2 of 5

VPC, Subnet, Internet Gateway and Route Table

Build a VPC with a public subnet from four building blocks — the VPC, a subnet, an Internet Gateway and a route table.

Practice interactively →

The four building blocks

The route table decides public or private

flowchart LR
  Internet((Internet)) <--> IGW[Internet Gateway]
  subgraph VPC["VPC 10.0.0.0/16"]
    subgraph Pub["Public subnet 10.0.1.0/24 (AZ a)"]
      EC2A[EC2 with a public IP]
    end
    subgraph Priv["Private subnet 10.0.2.0/24 (AZ a)"]
      EC2B[EC2 with only a private IP]
    end
  end
  IGW <--> Pub
  %% step Internet: Traffic from the Internet arrives at the VPC through the Internet Gateway.
  %% step IGW: The IGW is attached to the VPC and translates public IPs to private IPs.
  %% step EC2A: The public subnet routes 0.0.0.0/0 to the IGW and the instance has a public IP, so it is reachable.
  %% step EC2B: The private subnet has only the local route, so the instance cannot be reached from the Internet.

The public subnet's route table:

Destination Target
10.0.0.0/16 local
0.0.0.0/0 igw-xxxx

The private subnet only has the local row. There's no "public" flag on a subnet — it's the 0.0.0.0/0 → IGW route that makes it public.

But a route alone isn't enough: the instance also needs a public IP or Elastic IP. Remember to turn on auto-assign public IPv4 for your public subnet.

Creating it with the CLI

Step Command
Create the VPC aws ec2 create-vpc --cidr-block 10.0.0.0/16
Create a subnet aws ec2 create-subnet --vpc-id vpc-xxxx --cidr-block 10.0.1.0/24 --availability-zone ap-southeast-1a
Create the IGW aws ec2 create-internet-gateway
Attach the IGW aws ec2 attach-internet-gateway --internet-gateway-id igw-xxxx --vpc-id vpc-xxxx
Create a route table aws ec2 create-route-table --vpc-id vpc-xxxx
Route to the Internet aws ec2 create-route --route-table-id rtb-xxxx --destination-cidr-block 0.0.0.0/0 --gateway-id igw-xxxx
Associate the subnet aws ec2 associate-route-table --route-table-id rtb-xxxx --subnet-id subnet-xxxx

Common mistakes

Default limits (soft limits): 5 VPCs per region, 200 subnets per VPC.

Practice interactively →