Hands-On & Best Practices
Create a VPC with public and private subnets in one go, then follow a short checklist.
1 min read
The console's "VPC and more" wizard builds a full layout (subnets in 2 AZs, route tables, an internet gateway, and optional NAT gateways and S3 endpoint) in one step. It's the fastest way to get a correct setup. Here's what the core pieces look like from the CLI, so you can see how they fit together:
Try it
Terminal
# 1. The VPC
VPC=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
# 2. One public and one private subnet
PUB=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.0.0/24 \
--availability-zone us-east-1a --query Subnet.SubnetId --output text)
PRIV=$(aws ec2 create-subnet --vpc-id $VPC --cidr-block 10.0.10.0/24 \
--availability-zone us-east-1a --query Subnet.SubnetId --output text)
# 3. Internet gateway, attached to the VPC
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --vpc-id $VPC --internet-gateway-id $IGW
# 4. A route table that makes the public subnet public
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
# 5. Free private access to S3 from the private subnet's route table
MAIN_RT=$(aws ec2 describe-route-tables --filters Name=vpc-id,Values=$VPC Name=association.main,Values=true \
--query 'RouteTables[0].RouteTableId' --output text)
aws ec2 create-vpc-endpoint --vpc-id $VPC --service-name com.amazonaws.us-east-1.s3 \
--route-table-ids $MAIN_RTThe private subnet uses the VPC's main route table, which only has the local route, plus the S3 endpoint
we just added. That's what keeps it private.
Checklist
- Use at least two AZs for anything that must stay up.
- Put databases and app servers in private subnets. Only load balancers go in public subnets.
- Add gateway endpoints for S3 and DynamoDB, which are free and cut NAT costs.
- Turn on VPC Flow Logs to record traffic metadata for troubleshooting and security reviews.
- Leave room: pick a
/16for the VPC so you can add subnets later. - Define networks with infrastructure as code (CloudFormation or CDK) so they're repeatable.
Key takeaways
- VPC + subnets + IGW + route table association = a working public subnet.
- Private subnets only have local routes (plus NAT or endpoints).
- Two AZs, private data tiers, free gateway endpoints, and Flow Logs are good defaults.
Check your understanding
Pick an answer to see the explanation.
1. You created an Internet Gateway, but instances in your new subnet still can't reach the internet. What's missing?
Next in “Run a classic web app”
You finished VPC. Continue with the EC2 tutorial