Skip to content
VirtualLP Virtual Learning Platform

Building Complex Solutions with Amazon VPC

18 min read Topic 24 of 34 3 hands-on labs

An Amazon Virtual Private Cloud (VPC) is a network you define inside AWS (Amazon Web Services). You choose its IP (Internet Protocol) address range and divide it into subnets, smaller parts of the network.

You also choose where traffic can go and which connections are allowed. For example, a website may accept Internet traffic while its database stays on a private subnet.

gated housing society
  • The VPC is the whole gated society with its own boundary wall.
  • Subnets are the blocks/buildings inside it.
  • Route tables are the signboards that tell traffic which way to go.
  • The internet gateway is the main gate to the city road.
  • A NAT (Network Address Translation) gateway is a one-way exit: residents can go out for shopping, but strangers cannot walk in through it.
  • Security groups are the locks on each flat's door; network ACLs (Access Control Lists) are the guards at each building's entrance.
Amazon VPC

A VPC is a separate virtual network that you define inside AWS. You choose its address range, subnets, routes and access rules.

VPC building blocks

Component Purpose
VPC The isolated network, defined by an IPv4 address range (CIDR (Classless Inter-Domain Routing) block) from /16 to /28; it covers all AZs (Availability Zones) of one region
Subnet A smaller range of addresses inside the VPC, located in one AZ
Route table Rules that decide where traffic from a subnet goes; each subnet uses exactly one route table
Internet gateway (IGW) The door between the VPC and the Internet – automatically scaled and redundant; one per VPC
NAT gateway Lets servers in private subnets reach the Internet outbound only; it sits in a public subnet with an Elastic IP
Elastic IP A fixed (static) public IPv4 address that you keep
Security group A stateful firewall on each instance (its network card, ENI (Elastic Network Interface))
Network ACL A stateless firewall on each subnet
VPC endpoint A private path to AWS services without going over the Internet
VPC peering / Transit Gateway Connect VPCs to each other
VPN (Virtual Private Network) / Direct Connect Connect your own office/data centre network
Flow logs Record details of network traffic for monitoring and troubleshooting

CIDR planning (choosing addresses)

A CIDR like 10.0.0.0/16 describes a range of addresses. The number after / says how many bits are fixed: the smaller the number, the bigger the range.

A VPC must use a private IPv4 range (RFC 1918):

Range Size
10.0.0.0/8 16.7 million addresses
172.16.0.0/12 1 million
192.168.0.0/16 65,536

Rules:

  • A VPC can be from /16 (65,536 addresses) down to /28 (16 addresses).
  • A subnet's range must be inside the VPC's range and must not overlap any other subnet.
  • AWS keeps 5 addresses in every subnet for itself: the network address, .1 (the VPC router), .2 (DNS (Domain Name System)), .3 (future use) and the last (broadcast) address. So a /24 subnet (256 addresses) has 251 usable addresses.
  • Never overlap your VPC range with your office network or with VPCs you may connect to later.
Quick sizes to remember

/16 = 65,536 · /20 = 4,096 · /24 = 256 · /26 = 64 · /28 = 16. Usable in AWS = size − 5.

Worked example

VPC 10.0.0.0/16. Subnets: 10.0.1.0/24 (public, AZ-a), 10.0.2.0/24 (public, AZ-b), 10.0.11.0/24 (app, AZ-a), 10.0.12.0/24 (app, AZ-b), 10.0.21.0/24 (DB, AZ-a), 10.0.22.0/24 (DB, AZ-b). Each has 256 − 5 = 251 usable IPs.

Public vs private subnets

A subnet is public if its route table has a route to an internet gateway. Otherwise it is private.

Route table of a public subnet Route table of a private subnet
Destination Target Destination Target
10.0.0.0/16 local 10.0.0.0/16 local
0.0.0.0/0 igw-0abc… 0.0.0.0/0 nat-0def…

How to read a route table:

  • Every route table has the local route – all subnets in the VPC can talk to each other.
  • 0.0.0.0/0 (the default route) means "everything else" – send it to the IGW (public subnet) or the NAT gateway (private subnet).
  • A server in a public subnet also needs a public or Elastic IP to be reachable from the Internet.
  • If two routes match, the most specific one wins (the one with the bigger / number – longest prefix match).
Common exam traps
  1. A NAT gateway must be placed in a public subnet – not in the private subnet it serves.
  2. An IGW does nothing until it is attached to the VPC and a route points to it.
  3. A NAT gateway allows outbound connections only; the Internet cannot start a connection to private servers.

Security groups vs network ACLs

Basis Security group Network ACL
Level Instance / its network card (ENI) Subnet
State Stateful – if a request is allowed in, the reply is automatically allowed out Stateless – the reply must be allowed separately (ephemeral ports 1024–65535)
Rules Allow only Allow and deny
How rules are checked All rules together In number order; the first match wins
Default New SG: block all incoming, allow all outgoing Default NACL (Network Access Control List): allow all; a new custom NACL: deny all
Applies to Only the instances it is attached to All instances in the subnet
Can refer to Other security groups Only IP ranges (CIDR)

Security group chaining (tiered security)

Security group Inbound rule Source
alb-sg HTTPS (Hypertext Transfer Protocol Secure) 443 0.0.0.0/0 (anyone)
app-sg HTTP (Hypertext Transfer Protocol) 8080 alb-sg
db-sg MySQL 3306 app-sg

Internet users reach the load balancer. App servers can reach the database. Rules refer to security groups, so new servers in those groups get the same access.

Connecting VPCs and networks

VPC peering

A one-to-one private link between two VPCs (same or different accounts/regions). Traffic stays on AWS's private network.

  • Their address ranges must not overlap.
  • Not transitive – if A↔B and B↔C, A cannot reach C through B. Like friendship: your friend's friend is not automatically your friend.
  • Both sides must add routes to the other VPC's range.

AWS Transit Gateway

A central hub in a region that connects many VPCs and on-premises networks (hub-and-spoke). It solves the "everyone peers with everyone" mess: with 10 VPCs, peering needs 45 connections; Transit Gateway needs just 10 attachments. Its route tables can also separate networks (for example production cannot talk to development).

flowchart TB
    TGW((Transit Gateway))
    V1[VPC Prod] --- TGW
    V2[VPC Dev] --- TGW
    V3[VPC Shared services] --- TGW
    V4[VPC Analytics] --- TGW
    ONP[On-premises DC] -- VPN / Direct Connect --- TGW

VPC endpoints

  • Gateway endpoints – for S3 (Simple Storage Service) and DynamoDB only; added as a route in the route table; free.
  • Interface endpoints (AWS PrivateLink) – a network card with a private IP inside your subnet, for many services (SQS (Simple Queue Service), KMS (Key Management Service), Secrets Manager, even your own services).

With an endpoint, private servers can use S3 without a NAT gateway, and the traffic never touches the Internet – more secure and cheaper.

Hybrid connectivity (connecting your own data centre)

Option Description
Site-to-Site VPN Encrypted IPsec tunnels over the Internet between AWS (Virtual Private Gateway / Transit Gateway) and your office router; quick to set up
AWS Direct Connect A dedicated private fibre line (1–100 Gbps) from your data centre to AWS; steady speed and delay, higher cost
Client VPN Individual users connect securely to the VPC from their laptops

A complete complex solution

Requirement: Build a learning website with separate web, app and database layers. Keep databases private. Add admin access, a college data-centre connection and resources in two Availability Zones so one location can fail.

flowchart TB
    INET((Internet)) --> IGW[Internet gateway]
    subgraph VPC[VPC 10.0.0.0/16 – ap-south-1]
        subgraph AZA[AZ ap-south-1a]
            PUBA[Public 10.0.1.0/24<br/>ALB node · NAT-GW-a · Bastion]
            APPA[App 10.0.11.0/24<br/>EC2 Auto Scaling]
            DBA[DB 10.0.21.0/24<br/>RDS primary]
        end
        subgraph AZB[AZ ap-south-1b]
            PUBB[Public 10.0.2.0/24<br/>ALB node · NAT-GW-b]
            APPB[App 10.0.12.0/24<br/>EC2 Auto Scaling]
            DBB[DB 10.0.22.0/24<br/>RDS standby]
        end
        EP[S3 gateway endpoint]
    end
    IGW --> PUBA & PUBB
    PUBA --> APPA --> DBA
    PUBB --> APPB --> DBB
    APPA & APPB --> EP --> S3[(Amazon S3)]
    VPC -- Site-to-Site VPN --> COL[College data centre<br/>192.168.0.0/16]
Layer Design decision
Subnets 3 tiers × 2 AZs = 6 subnets; non-overlapping /24s
Public route table 0.0.0.0/0 → IGW, used by both public subnets
Private route tables One per AZ: 0.0.0.0/0 → NAT gateway in the same AZ (so one AZ failing doesn't cut the other's Internet)
DB route table Local route only – no Internet route at all
Security SG chaining ALB (Application Load Balancer) → app → DB; a NACL on the DB subnets blocks everything except traffic from the app subnets
Admin access A bastion host, or better, Systems Manager Session Manager (no SSH (Secure Shell) port open)
Data access S3 gateway endpoint for uploads and backups
Hybrid Site-to-Site VPN to the college network (192.168.0.0/16 does not overlap)
Monitoring VPC Flow Logs sent to CloudWatch Logs

Creating it step by step (console)

  1. Create a VPC 10.0.0.0/16 and enable DNS hostnames.
  2. Create subnets – six, in two AZs.
  3. Create an internet gateway and attach it to the VPC.
  4. Create a public route table with 0.0.0.0/0 → IGW; associate the public subnets; turn on auto-assign public IPv4.
  5. Allocate Elastic IPs and create a NAT gateway in each public subnet.
  6. Create private route tables with 0.0.0.0/0 → NAT and associate the app subnets.
  7. Create security groups (alb-sg, app-sg, db-sg) with the chained rules.
  8. Launch the load balancer, the Auto Scaling group and the RDS (Relational Database Service) Multi-AZ database.
  9. Add the S3 gateway endpoint, the VPN connection and flow logs.

The same core network with the AWS CLI

CLI means Command-Line Interface.

VPC=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id $VPC --enable-dns-hostnames
PUB=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.1.0/24 --availability-zone ap-south-1a --query Subnet.SubnetId --output text)
PRV=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.11.0/24 --availability-zone ap-south-1a --query Subnet.SubnetId --output text)
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --vpc-id $VPC --internet-gateway-id $IGW
RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $RT --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
aws ec2 associate-route-table --route-table-id $RT --subnet-id $PUB
aws ec2 modify-subnet-attribute --subnet-id $PUB --map-public-ip-on-launch

The commands create the VPC, subnets, Internet gateway and routes. Each new resource ID is saved for the next command. --query … --output text selects that ID from the reply.

Virtual lab · Amazon Web Services · 40 min

Build a custom VPC with public and private subnets

Design a two-tier network from scratch: VPC, subnets in two Availability Zones, an internet gateway, a public route table and a web-tier security group – then prove it works by launching an instance into it.

Open lab

Virtual lab · Amazon Web Services · 35 min

Give a private subnet outbound internet with a NAT gateway

Extend a VPC with a NAT gateway so private instances can download updates while staying unreachable from the internet – a core Solutions Architect pattern.

Open lab

Virtual lab · Amazon Web Services · 20 min

Automate AWS with the CLI in CloudShell

Use the AWS CLI from CloudShell to inspect your identity, list resources as JSON and create networking with commands – the foundation of infrastructure automation.

Open lab

Troubleshooting checklist – "I can't reach my instance"

  1. Is the instance running, and have its status checks passed?
  2. Does it have a public IP / Elastic IP?
  3. Does the subnet's route table send 0.0.0.0/0 to an attached IGW?
  4. Does the security group allow the port (22 / 80 / 443) from your IP?
  5. Does the network ACL allow incoming and outgoing (ephemeral port) traffic?
  6. Is the server's own firewall open and is the service actually listening on that port?
  7. Check VPC Flow Logs for REJECT entries.

Summary

  • A VPC is an isolated network with a range between /16 and /28; subnets live in one AZ; AWS keeps 5 IPs in each subnet.
  • Public subnet = route to an IGW; private subnet = default route to a NAT gateway (which sits in a public subnet), or no Internet route.
  • Security groups are stateful, instance-level, allow-only; NACLs are stateless, subnet-level, ordered allow/deny.
  • Connect networks with VPC peering (not transitive), Transit Gateway (hub), VPC endpoints (private access to AWS services), VPN and Direct Connect.
  • Complex designs use multi-AZ, multi-tier subnets, SG chaining, one NAT gateway per AZ, endpoints and flow logs.

Practice questions

  1. What is Amazon VPC? Explain its components.
  2. How many usable IP addresses are there in a /26 subnet in AWS? Why?
  3. Differentiate public and private subnets. Show their route tables.
  4. Differentiate security groups and network ACLs.
  5. Explain VPC peering and its limitations. How does Transit Gateway help?
  6. What are VPC endpoints? Differentiate gateway and interface endpoints.
  7. Design a multi-tier, multi-AZ VPC for a web application with a neat diagram.
  8. Write the AWS CLI commands to create a VPC with a public subnet.
Answer to Q2

A /26 has 2^(32−26) = 64 addresses. AWS keeps 5, so 59 are usable.

Practise this topic

Hands-on virtual labs

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.