Skip to content
EgyKode
04 · Infrastructure as CodeLab 23 / 59
Guided labterraform

AWS VPC, Subnets, Gateways & Route Tables

Rebuild the network you made by hand as Terraform modules, and see the plan account for every subnet and route.

Time
47 min
Level
Intermediate
Objectives
5 objectives
Cost
Billable

Where this fits in the platform

Before you start

CostBillable

The NAT Gateway is ~$0.045/hour (~$32/month) plus $0.045/GB processed, and it bills whether or not traffic flows. The VPC, subnets and route tables are free. Destroy the NAT Gateway the moment you are done.

Nothing to pay in the browser. Open the terminal runs this against a simulated cloud — the same API calls and the same commands, with no account and no bill. The figure above applies only if you build it in your own.

How to clean up

The scenario#

You built this network by hand in the console lab. Doing it again in another region would take the same forty minutes and produce something subtly different.

This is the same network as code — and the first plan you can read line by line before anything is created.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

The network

Step 1 of 4

What you are building#

text
                        VPC 10.0.0.0/16
  ┌──────────────────────────────┬──────────────────────────────┐
  │        us-east-1a            │         us-east-1b           │
  │  public  10.0.1.0/24         │   public  10.0.2.0/24        │
  │    └─ NAT Gateway + EIP      │                              │
  │  private 10.0.10.0/24        │   private 10.0.11.0/24       │
  └──────────────┬───────────────┴──────────────┬───────────────┘
                 │                              │
        Internet Gateway                route 0.0.0.0/0 > NAT

A subnet is not public or private as a property. Both are identical objects. What makes one public is a route table entry sending 0.0.0.0/0 to an Internet Gateway; what makes the other private is that its route sends 0.0.0.0/0 to a NAT Gateway instead. Nothing else distinguishes them, and naming a subnet "public" while pointing it at a NAT is a mistake Terraform will happily make for you.

Two availability zones, because one is not high availability. An AZ is a distinct set of buildings. Losing one is rare and does happen, and an architecture with everything in us-east-1a goes down with it.


Build it#

Verify it worked#

Terminal
# Every private subnet routes 0.0.0.0/0 at a NAT, never at an IGW
aws ec2 describe-route-tables \
  --filters "Name=vpc-id,Values=$(terraform output -raw vpc_id)" \
  --query 'RouteTables[].{rt:RouteTableId,routes:Routes[?DestinationCidrBlock==`0.0.0.0/0`].[GatewayId,NatGatewayId]}' \
  --output table
 
# Both AZs are represented
aws ec2 describe-subnets --filters "Name=vpc-id,Values=$(terraform output -raw vpc_id)" \
  --query 'Subnets[].[Tags[?Key==`Name`]|[0].Value,AvailabilityZone,CidrBlock]' --output table
 
# The real test: outbound works, inbound does not
aws ssm start-session --target <private-instance-id>
#   curl -sI https://example.com | head -1     → 200, via NAT
#   (nothing on the internet can open a connection to this instance)
 
terraform plan -detailed-exitcode; echo "exit $?"    # 0 = no drift

Clean up#

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
terraform destroy -auto-approve
aws ec2 describe-nat-gateways --filter Name=state,Values=available --query 'NatGateways[].NatGatewayId'
aws ec2 describe-addresses --query 'Addresses[?AssociationId==null].[PublicIp,AllocationId]'

Cost of this lab: Billable. The NAT Gateway is roughly $0.045/hour plus per-GB processing — about $32/month if left running. Everything else here is free. Destroy it when you finish.

Success criteria

0 of 5

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 23 of 59 on the project path

IAM Roles, IRSA Policies & Security GroupsGive the cluster, the nodes and the build server exactly the permissions each needs and nothing more.Why next: vpc_id, public and private subnet ids, route tables, NAT gateway23 minIntermediate

Previous: Terraform Remote State & Locking