DevOps.Academy
CoursesCurriculumPricing

    Loading…

    ↑ ↓ move · ↵ open · Ctrl K anywhere

    ◔My learningSign in
    DevOps.Academy

    Free, hands-on DevOps lessons. Made with care in India.

    Learn

    All coursesLinux roadmapCurriculumMy learning

    Academy

    PricingCertificates

    Legal

    PrivacyTermsContact

    devops.rajeev.pro

    AWS for DevOpsLesson 4 of 17
    Your progress · 0%
    On this page
    1. Why a network of your own
    2. Plan the addresses
    3. Subnets in two AZs
    4. What makes a subnet public
    5. NAT: the way out for private subnets
    6. Two firewalls
    7. Endpoints, peering and flow logs
    8. Clean up
    ↖ Course roadmap
    Lesson 4 · VPC

    VPC: your own private network

    Build a real VPC with public and private subnets in two AZs, read its route tables, and see how internet gateways, NAT, security groups, network ACLs and endpoints fit together.

    35 min read · intermediate · hands-on

    By the end of this lesson you'll be able to

    0 of 6 complete

    Why a network of your own

    In lesson 3 the server landed in the default VPC, which AWS creates in every Region with only public subnets: fine for a test, wrong for a database. A VPC (Virtual Private Cloud) is your own private network in a Region. You choose its address range, cut it into subnets in different Availability Zones, and decide which ones the internet can reach.

    The target for this lesson, built in a real account and deleted afterwards:

    SubnetAZRangeCan the internet reach it?
    public 1ap-south-1a10.0.0.0/20yes, through the internet gateway
    public 2ap-south-1b10.0.16.0/20yes
    private 1ap-south-1a10.0.128.0/20no
    private 2ap-south-1b10.0.144.0/20no

    Load balancers go in the public subnets; app servers and databases in the private ones (lesson 5 uses this).

    Plan the addresses

    A CIDR block is an address range written as a start address and a prefix length. The prefix says how many bits are fixed; the rest are yours:

    CIDRAddressesTypical use
    10.0.0.0/1665,536a whole VPC (the largest AWS allows)
    10.0.0.0/204,096one subnet
    10.0.0.0/24256a small subnet

    AWS reserves 5 addresses in every subnet (the first four and the last), so a /24 gives 251 usable. Use the private ranges 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16, and give every VPC a range that doesn't overlap the others or your office network, or you can never connect them later.

    Subnets in two AZs

    VPC → Your VPCs → Create VPC → VPC and more builds the whole network in one go, and draws it as you type:

    (1) VPC and more. (2) A name prefix for everything it creates. (3) The VPC's address range.© Screenshot: DevOps Academy

    (1) VPC and more. (2) A name prefix for everything it creates. (3) The VPC's address range.© Screenshot: DevOps Academy

    (1–3) Two AZs, each with a public and a private subnet. (4) No NAT gateway: it costs money by the hour. (5) A free S3 gateway endpoint.© Screenshot: DevOps Academy

    (1–3) Two AZs, each with a public and a private subnet. (4) No NAT gateway: it costs money by the hour. (5) A free S3 gateway endpoint.© Screenshot: DevOps Academy

    Everything the wizard did for you, step by step. Each of these is one API call you could also script.© Screenshot: DevOps Academy

    Everything the wizard did for you, step by step. Each of these is one API call you could also script.© Screenshot: DevOps Academy

    The VPC's Resource map tab shows how the pieces connect:

    (1) Subnets → (2) their route tables → (3) where traffic can leave: the internet gateway or the S3 endpoint.© Screenshot: DevOps Academy

    (1) Subnets → (2) their route tables → (3) where traffic can leave: the internet gateway or the S3 endpoint.© Screenshot: DevOps Academy

    (1) A public subnet's range. (2) A private one. Same size, different route tables.© Screenshot: DevOps Academy

    (1) A public subnet's range. (2) A private one. Same size, different route tables.© Screenshot: DevOps Academy

    What makes a subnet public

    Nothing about the subnet itself: only its route table. Every packet leaving a subnet looks up its destination there:

    The public route table: 0.0.0.0/0 (everything not local) goes to the internet gateway.© Screenshot: DevOps Academy

    The public route table: 0.0.0.0/0 (everything not local) goes to the internet gateway.© Screenshot: DevOps Academy

    A private route table: only local traffic and S3 through the endpoint. No route to the internet at all.© Screenshot: DevOps Academy

    A private route table: only local traffic and S3 through the endpoint. No route to the internet at all.© Screenshot: DevOps Academy
    RouteMeans
    10.0.0.0/16 → localin every route table: any subnet can reach any other inside the VPC
    0.0.0.0/0 → igw-…the internet gateway: makes the subnet public, both directions
    pl-… → vpce-…S3 through the gateway endpoint, without the internet

    An instance in a public subnet also needs a public IP to be reachable; one in a private subnet has only a private address, and nothing outside the VPC can start a connection to it.

    NAT: the way out for private subnets

    Private servers still need to download packages and call APIs. A NAT gateway sits in a public subnet and lets private subnets go out (private route table: 0.0.0.0/0 → nat-…) while nothing comes in. The wizard offers three choices: None, Regional (one NAT for all AZs, new) and Zonal (one per AZ).

    NAT gateways cost money even when idle

    A NAT gateway is billed per hour and per GB processed, plus its public IPv4 address: tens of dollars a month for one sitting idle. That's why the wizard defaults to None and this lesson didn't create one. For learning, skip it; in production, size it on purpose, and use gateway endpoints for S3 and DynamoDB traffic.

    Two firewalls

    Security groupNetwork ACL
    Attached toan instance (its network interface)a subnet
    Statestateful: a reply to allowed traffic is allowedstateless: rules needed in both directions
    Rulesallow onlyallow and deny, evaluated in number order
    Defaultnothing in, everything outthe default ACL allows everything

    (1) Rule 100 allows everything. (2) The * rule denies whatever no numbered rule matched.© Screenshot: DevOps Academy

    (1) Rule 100 allows everything. (2) The * rule denies whatever no numbered rule matched.© Screenshot: DevOps Academy

    A VPC's default security group only lets in traffic from other members of itself.© Screenshot: DevOps Academy

    A VPC's default security group only lets in traffic from other members of itself.© Screenshot: DevOps Academy

    Most teams leave network ACLs at their defaults and filter with security groups, because a security group can name another security group as the source ("only from the load balancer"), which lesson 5 uses.

    Endpoints, peering and flow logs

    • Gateway endpoints (S3 and DynamoDB only) add a route, cost nothing, and keep that traffic off the NAT gateway. The wizard created one: academy-vpce-s3.
    • Interface endpoints (PrivateLink) put a private IP for an AWS service (ECR, Secrets Manager, SSM) inside your subnet. They cost per hour, and they let private subnets work with no internet access at all.
    • VPC peering connects two VPCs one-to-one; ranges must not overlap. Transit Gateway is a hub for many VPCs and VPN connections, when peering every pair gets out of hand.
    • VPC Flow Logs record accepted and rejected connections per interface, to CloudWatch Logs or S3: the first thing to turn on when "the network is blocking it" (lesson 13).

    Clean up

    Delete the VPC from Your VPCs → Actions → Delete VPC; the console deletes its subnets, route tables, internet gateway and endpoint with it. From the CLI you delete them yourself, in order:

    $ aws ec2 delete-vpc-endpoints --vpc-endpoint-ids vpce-…
    $ aws ec2 detach-internet-gateway --internet-gateway-id igw-… --vpc-id vpc-…
    $ aws ec2 delete-internet-gateway --internet-gateway-id igw-…
    $ aws ec2 delete-subnet --subnet-id subnet-…                 # each of the four
    $ aws ec2 delete-route-table --route-table-id rtb-…          # each non-main table
    $ aws ec2 delete-vpc --vpc-id vpc-…
    
    aws ec2 delete-vpc-endpoints --vpc-endpoint-ids vpce-…
    • awsthe AWS command line
    In production

    Plan non-overlapping CIDRs for every VPC and environment up front, use at least two AZs, keep databases and app servers in private subnets, give each team or environment its own VPC (or account), turn on Flow Logs, and build it all with Terraform or CloudFormation (lesson 16) rather than the wizard.

    ⌘
    Mini mission

    Build and read a two-tier network

    Create a VPC with 2 AZs, 2 public and 2 private subnets, no NAT gateway and an S3 gateway endpoint. Prove from the route tables which subnets are public, then delete the whole VPC and confirm it's gone.

    Input: An AWS account with the console and AWS CLIOutput: Route tables showing 0.0.0.0/0 to an internet gateway only for the public subnets, then no academy-vpc left
    Reveal one safe solution
    $ # 1. Console: VPC → Create VPC → VPC and more, name academy, 10.0.0.0/16, 2 AZs, 2 public, 2 private,
    $ #    NAT None, S3 Gateway → Create VPC
    $ V=$(aws ec2 describe-vpcs --filters Name=tag:Name,Values=academy-vpc --query 'Vpcs[0].VpcId' --output text)
    $ aws ec2 describe-route-tables --filters Name=vpc-id,Values=$V \
        --query 'RouteTables[].[Tags[?Key==`Name`]|[0].Value, Routes[?DestinationCidrBlock==`0.0.0.0/0`]|[0].GatewayId]' --output text
    $ # 2. Console: Your VPCs → select academy-vpc → Actions → Delete VPC → type delete
    $ aws ec2 describe-vpcs --filters Name=tag:Name,Values=academy-vpc --query 'length(Vpcs)'   # 0 when it's gone
    
    aws ec2 describe-route-tables --filters Name=vpc-id,Values=$V \
    • awsthe AWS command line
    aws ec2 describe-vpcs --filters Name=tag:Name,Values=academy-vpc --query 'length(Vpcs)'
    • --querypick fields from the JSON response

    Expect one line per route table: only academy-rtb-public shows an igw-… ID; the private tables and the main table show None, because they have no route to the internet. Without a NAT gateway this network costs nothing, so you can rebuild it whenever you like.

    Try it yourself

    0 of 6 steps done

    Run each task in your own account or terminal, then tick it off.

    Knowledge check

    Quick check

    Question 1 of 5

    What makes a subnet public?

    Go deeper

    Official reading for this lesson

    • DocsHow Amazon VPC worksAWS Docs ↗
    • DocsSubnet CIDR blocksAWS Docs ↗
    • DocsNAT gatewaysAWS Docs ↗
    • DocsCompare security groups and network ACLsAWS Docs ↗
    • DocsVPC Flow LogsAWS Docs ↗

    Or get every quiz answer right and it completes itself.

    ← PreviousEC2: your first server in the cloudNext lesson →Scaling and load balancing