Lesson 4 of 8 in Cloud fundamentals with AWS

VPC and subnets: your private network in AWS

Core12 minWritten for AWS CLI v2 (preinstalled in CloudShell)

A Virtual Private Cloud (VPC) is your own isolated network inside AWS, with an address range you choose. You divide it into subnets, and route tables decide where each subnet's traffic can go.

Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.

Read these first: Networking for DevOps, Accounts and billing safety before you build

The problem this solves

Servers need to talk to each other and sometimes to the internet, but a database should not be reachable by strangers. On a shared public network you could not guarantee that. You need walls and doors that you control.

A VPC gives you a private address space, for example 10.0.0.0/16. Inside it you decide which parts may talk to the internet and which may not. A web server sits in a part that can be reached, and a database sits in a part that cannot.

This is the same IP addressing, CIDR ranges and routing you met in the networking topic, now drawn as cloud objects.

How it works

What makes a subnet public or private
What makes a subnet public or privateInternet traffic enters through the internet gateway, a route in the public subnet's route table sends it onward, and the private subnet has no route to the gateway.1 inboundrequest2 routed in3 app callsdatabasedecides the pathInternetInternet gatewaydoor to the internetPublic subnetroute to the gatewayPrivate subnetno route to the gatewayRoute table0.0.0.0/0 to gateway
  • People and clients
  • Network
  • Runs code
  • Data
  • Control

Internet traffic enters through the internet gateway, a route in the public subnet's route table sends it onward, and the private subnet has no route to the gateway.

  1. Internet to Internet gateway: 1 inbound request
  2. Internet gateway to Public subnet: 2 routed in
  3. Public subnet to Private subnet: 3 app calls database
  4. Route table to Public subnet: decides the path
How a private server reaches out for updates
How a private server reaches out for updatesA server in a private subnet sends its request to a NAT gateway in a public subnet, which forwards it through the internet gateway, and replies come back the same way.1 outboundrequest2 addresstranslated3 leaves theVPCPrivate serverno public addressNAT gatewaysits in a public subnetInternet gatewayInternetpackage mirror, API
  • Runs code
  • Network
  • People and clients

A server in a private subnet sends its request to a NAT gateway in a public subnet, which forwards it through the internet gateway, and replies come back the same way.

  1. Private server to NAT gateway: 1 outbound request
  2. NAT gateway to Internet gateway: 2 address translated
  3. Internet gateway to Internet: 3 leaves the VPC

Read the first diagram left to right. The internet gateway is the one door between your VPC and the internet. A subnet is a slice of the VPC's address range that lives in a single availability zone. A route table is a list of rules, such as 'traffic for 0.0.0.0/0 goes to the internet gateway'.

A subnet is called public when its route table sends internet-bound traffic to the internet gateway, and its servers have public addresses. A private subnet has no such route, so nothing from the internet can start a connection to it. These names are habits, not switches. The route table is what decides.

Private servers still need to fetch updates. A NAT gateway lets them start outbound connections while blocking inbound ones. It is convenient but it bills by the hour whether or not traffic flows, and it also bills for the data it handles. It is one of the most common causes of surprise bills.

Other clouds use the same parts. Azure calls it a Virtual Network (VNet) with subnets and route tables. Google Cloud calls it a VPC network, whose subnets are regional rather than tied to one zone.

Try it yourself

Run these in CloudShell. Looking is free. A VPC and a subnet are free to create, and you delete them at the end. You will not create a NAT gateway or any server in this lesson.

  1. 1
    Look at the VPC AWS gave you
    aws ec2 describe-vpcs --query "Vpcs[].{Id:VpcId,Cidr:CidrBlock,Default:IsDefault}" --output table
    aws ec2 describe-subnets --query "Subnets[].{Id:SubnetId,Zone:AvailabilityZone,Cidr:CidrBlock,PublicIpOnLaunch:MapPublicIpOnLaunch}" --output table

    A new account normally has a default VPC with one subnet per availability zone in the Region. Each subnet has its own slice of the VPC range.

  2. 2
    Find the route that makes a subnet public
    aws ec2 describe-route-tables --query "RouteTables[].Routes[].[DestinationCidrBlock,GatewayId]" --output table

    Look for a row with 0.0.0.0/0 and a gateway ID starting with igw-. That is the route to the internet gateway. The row with local is the VPC's own range, which lets subnets reach each other.

  3. 3
    Build your own tiny VPC
    VPC=$(aws ec2 create-vpc --cidr-block 10.20.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=learn-vpc}]' --query Vpc.VpcId --output text)
    SUBNET=$(aws ec2 create-subnet --vpc-id "$VPC" --cidr-block 10.20.1.0/24 --query Subnet.SubnetId --output text)
    aws ec2 describe-subnets --subnet-ids "$SUBNET" --query "Subnets[].[SubnetId,CidrBlock,AvailabilityZone]" --output table
    aws ec2 describe-route-tables --filters Name=vpc-id,Values="$VPC" --query "RouteTables[].Routes[].[DestinationCidrBlock,GatewayId]" --output table

    The new VPC has only the local route, because nothing connects it to the internet. Any subnet you put in it is private. A /24 is 256 addresses, and AWS reserves five in each subnet.

  4. 4
    Clean up
    aws ec2 delete-subnet --subnet-id "$SUBNET"
    aws ec2 delete-vpc --vpc-id "$VPC"
    aws ec2 describe-vpcs --filters Name=tag:Name,Values=learn-vpc --query "Vpcs[].VpcId" --output text

    The last command should print nothing. If CloudShell restarted and the variables are gone, find the ID with that same last command, then delete using it. Do not delete the default VPC.

What breaks, and where to look

  • ssh: connect to host <public-ip> port 22: Connection timed out

    WHY IT HAPPENS
    Three things must all be true: the server has a public address, its subnet's route table has a route to an internet gateway, and a security group allows the port. A timeout usually means one of them is missing.
    CHECK FIRST
    Find the subnet's route table with aws ec2 describe-route-tables --filters Name=association.subnet-id,Values=<subnet-id>. If that is empty, the subnet uses the VPC's main route table, so use Name=association.main,Values=true instead. Then check the security group's inbound rules.
  • A server in a private subnet cannot download updates: curl hangs and then reports a connection timeout.

    WHY IT HAPPENS
    The private subnet has no route out. Outbound access needs a route for 0.0.0.0/0 to a NAT gateway that sits in a public subnet.
    CHECK FIRST
    Read the private subnet's route table for a 0.0.0.0/0 row pointing at a nat- gateway. Adding a NAT gateway fixes it but starts hourly billing, so delete it when you finish.
  • An error occurred (DependencyViolation) when calling the DeleteVpc operation: The vpc has dependencies and cannot be deleted.

    WHY IT HAPPENS
    Something still lives inside the VPC, such as a subnet, a server, a network interface, a NAT gateway or an attached internet gateway.
    CHECK FIRST
    List what remains with aws ec2 describe-subnets --filters Name=vpc-id,Values=<vpc-id> and aws ec2 describe-network-interfaces --filters Name=vpc-id,Values=<vpc-id>. Delete from the inside out.

When not to use this

  • The default VPC is fine for learning, but its subnets are all public. Real workloads put databases in private subnets.
  • A VPC, subnets, route tables and an internet gateway are free. NAT gateways, public IPv4 addresses and interface endpoints are billed, so read the pricing page before adding them.

Check yourself

What actually makes a subnet public?

Its route table sends internet-bound traffic to an internet gateway, and its servers have public addresses. The name is just a label for that setup.

Why do private servers need a NAT gateway for updates?

They have no route to the internet. A NAT gateway makes outbound requests on their behalf while blocking connections that start from the internet.

What does the local route in a route table do?

It lets resources inside the VPC reach each other across subnets.

Lesson 58 of 92 in the DevOps path

Search

Search cheat sheets and projects