Amazon VPC Explained: Build a Public and Private Network with the Console and CLI
Every resource you run on AWS, such as EC2 instances, RDS databases, and Lambda functions in a VPC, lives in a network. Amazon Virtual Private Cloud (VPC) is that network. It is your own logically isolated section of the AWS cloud, where you control IP ranges, subnets, routing, and traffic rules.In this article you will learn the core VPC concepts and then build the same network two ways: with the AWS Management Console and with the AWS CLI.
What is Amazon VPC?
A VPC is a virtual network that you define inside an AWS Region. You choose its private IP range, split it into subnets across Availability Zones, and decide which subnets can reach the internet and which stay private.
What you will build:
- One VPC with the CIDR block
10.0.0.0/16
- One public subnet (
10.0.1.0/24) for internet-facing resources
- One private subnet (
10.0.2.0/24) for application and database resources
- An internet gateway so the public subnet can reach the internet
- A NAT gateway so the private subnet can make outbound connections without being reachable from the internet
- Route tables that connect everything
- Security groups that control traffic to your resources
- An S3 gateway endpoint so private resources reach S3 without using the NAT gateway
Core VPC building blocks
- VPC: the network itself, defined by an IPv4 CIDR block such as
10.0.0.0/16.
- Subnet: a range of IP addresses within the VPC, located in a single Availability Zone. AWS reserves 5 IP addresses in every subnet.
- Route table: a set of rules that decides where network traffic from a subnet is sent.
- Internet gateway (IGW): a gateway that allows two-way communication between the VPC and the internet.
- NAT gateway: lets resources in private subnets start outbound connections to the internet while blocking inbound connections from it.
- Security group: a stateful firewall attached to a resource (such as an EC2 instance). It only has allow rules.
- Network ACL (NACL): a stateless firewall at the subnet level. It supports allow and deny rules.
- VPC endpoint: a private connection from your VPC to AWS services. Gateway endpoints (S3 and DynamoDB) are free. Interface endpoints (powered by AWS PrivateLink) cost money.
- VPC Flow Logs: records of IP traffic going to and from network interfaces, used for troubleshooting and security analysis.
What makes a subnet public or private?
There is no "public" checkbox. A subnet is public if its route table has a route to an internet gateway (
0.0.0.0/0 pointing to igw-... ). A subnet is private if it has no such route. Private subnets usually send internet-bound traffic to a NAT gateway instead.Traffic flow in this design:
- Users on the internet reach resources in the public subnet through the internet gateway.
- Resources in the private subnet send outbound traffic (for example, software updates) to the NAT gateway in the public subnet, which forwards it through the internet gateway.
- Resources in the private subnet reach S3 through the gateway endpoint, which keeps that traffic inside the AWS network.
Prerequisites
- An AWS account and permissions to create VPC and EC2 networking resources
- AWS CLI v2 configured with
aws configure(for the CLI section)
- A Region of your choice (the examples use
us-east-1)
Cost warning: a NAT gateway is billed per hour plus per GB processed, and an Elastic IP address is billed when it is not in use. Delete everything when you finish (see the cleanup step).
Implementation with the AWS Management Console
Option A: The quick way ("VPC and more" wizard)
- Open the VPC console and choose Create VPC.
- Select VPC and more.
- Set Name tag auto-generation to
demo.
- Set IPv4 CIDR block to
10.0.0.0/16.
- Set Number of Availability Zones to
1(use 2 or more for production).
- Set Number of public subnets to
1and Number of private subnets to1.
- Set NAT gateways to
In 1 AZ.
- Set VPC endpoints to
S3 Gateway.
- Under DNS options, enable both DNS hostnames and DNS resolution.
- Review the preview diagram, then choose Create VPC.
The wizard creates every resource below for you. It is the fastest route, but building the pieces manually (Option B) teaches you how they connect.
Option B: Step by step
Step 1: Create the VPC
- Open the VPC console, choose Your VPCs, then Create VPC.
- Select VPC only.
- Name tag:
demo-vpc. IPv4 CIDR block:10.0.0.0/16.
- Choose Create VPC.
- Select the VPC, choose Actions, then Edit VPC settings, and enable DNS hostnames.
Step 2: Create the subnets
- Go to Subnets and choose Create subnet.
- Select
demo-vpc.
- Subnet 1: name
demo-public-1a, Availability Zoneus-east-1a, IPv4 CIDR10.0.1.0/24.
- Choose Add new subnet. Subnet 2: name
demo-private-1a, Availability Zoneus-east-1a, IPv4 CIDR10.0.2.0/24.
- Choose Create subnet.
- Select
demo-public-1a, choose Actions, then Edit subnet settings, and enable Auto-assign public IPv4 address.
Step 3: Create and attach an internet gateway
- Go to Internet gateways and choose Create internet gateway.
- Name it
demo-igwand choose Create.
- Choose Actions, then Attach to VPC, select
demo-vpc, and choose Attach.
Step 4: Create the public route table
- Go to Route tables and choose Create route table.
- Name it
demo-public-rt, selectdemo-vpc, and choose Create.
- On the Routes tab, choose Edit routes, then Add route.
- Destination
0.0.0.0/0, target Internet Gateway, selectdemo-igw, and save.
- On the Subnet associations tab, choose Edit subnet associations, select
demo-public-1a, and save.
Step 5: Create the NAT gateway
- Go to NAT gateways and choose Create NAT gateway.
- Name it
demo-nat.
- Subnet:
demo-public-1a(the NAT gateway must sit in a public subnet).
- Connectivity type: Public. Choose Allocate Elastic IP.
- Choose Create NAT gateway and wait until the state is Available.
Step 6: Create the private route table
- Go to Route tables and choose Create route table.
- Name it
demo-private-rt, selectdemo-vpc, and choose Create.
- Add a route: destination
0.0.0.0/0, target NAT Gateway, selectdemo-nat.
- Associate the table with the subnet
demo-private-1a.
Step 7: Create security groups
- Go to Security groups and choose Create security group.
- Name
demo-web-sg, VPCdemo-vpc. Add an inbound rule: type HTTPS, source0.0.0.0/0. Create it.
- Create a second group named
demo-app-sgin the same VPC. Add an inbound rule: type Custom TCP, port8080, sourcedemo-web-sg(select the security group, not an IP range).
Referencing a security group as the source means only resources in that group can reach the app tier. This works better than IP ranges because it keeps working when instances change.
Step 8: Add the S3 gateway endpoint
- Go to Endpoints and choose Create endpoint.
- Name
demo-s3-endpoint. Type AWS services.
- Service name:
com.amazonaws.us-east-1.s3, type Gateway.
- Select
demo-vpcand the route tabledemo-private-rt, then choose Create endpoint.
Step 9: Verify
- Open the VPC, go to the Resource map tab, and confirm the subnets, route tables, and gateways are connected as expected.
- Launch a test EC2 instance in each subnet and check connectivity. The public instance should be reachable on allowed ports. The private instance should reach the internet outbound but not be reachable from it.
Implementation with the AWS CLI
All VPC commands live under
aws ec2 . Set the variables first:export AWS_REGION=us-east-1export AZ=us-east-1aStep 1: Create the VPC
VPC_ID=$(aws ec2 create-vpc \ --cidr-block 10.0.0.0/16 \ --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=demo-vpc}]' \ --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id $VPC_ID --enable-dns-hostnames '{"Value":true}'
echo $VPC_IDStep 2: Create the subnets
PUB_SUBNET_ID=$(aws ec2 create-subnet \ --vpc-id $VPC_ID \ --cidr-block 10.0.1.0/24 \ --availability-zone $AZ \ --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=demo-public-1a}]' \ --query Subnet.SubnetId --output text)
PRIV_SUBNET_ID=$(aws ec2 create-subnet \ --vpc-id $VPC_ID \ --cidr-block 10.0.2.0/24 \ --availability-zone $AZ \ --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=demo-private-1a}]' \ --query Subnet.SubnetId --output text)
aws ec2 modify-subnet-attribute --subnet-id $PUB_SUBNET_ID --map-public-ip-on-launchStep 3: Create and attach the internet gateway
IGW_ID=$(aws ec2 create-internet-gateway \ --tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=demo-igw}]' \ --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_IDStep 4: Create the public route table
PUB_RT_ID=$(aws ec2 create-route-table \ --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=demo-public-rt}]' \ --query RouteTable.RouteTableId --output text)
aws ec2 create-route \ --route-table-id $PUB_RT_ID \ --destination-cidr-block 0.0.0.0/0 \ --gateway-id $IGW_ID
aws ec2 associate-route-table --route-table-id $PUB_RT_ID --subnet-id $PUB_SUBNET_IDStep 5: Create the NAT gateway
EIP_ALLOC_ID=$(aws ec2 allocate-address --domain vpc \ --query AllocationId --output text)
NAT_ID=$(aws ec2 create-nat-gateway \ --subnet-id $PUB_SUBNET_ID \ --allocation-id $EIP_ALLOC_ID \ --tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value=demo-nat}]' \ --query NatGateway.NatGatewayId --output text)
aws ec2 wait nat-gateway-available --nat-gateway-ids $NAT_IDThe wait command blocks for a minute or two until the NAT gateway is ready.
Step 6: Create the private route table
PRIV_RT_ID=$(aws ec2 create-route-table \ --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=demo-private-rt}]' \ --query RouteTable.RouteTableId --output text)
aws ec2 create-route \ --route-table-id $PRIV_RT_ID \ --destination-cidr-block 0.0.0.0/0 \ --nat-gateway-id $NAT_ID
aws ec2 associate-route-table --route-table-id $PRIV_RT_ID --subnet-id $PRIV_SUBNET_IDStep 7: Create security groups
WEB_SG_ID=$(aws ec2 create-security-group \ --group-name demo-web-sg \ --description "Web tier" \ --vpc-id $VPC_ID \ --query GroupId --output text)
aws ec2 authorize-security-group-ingress \ --group-id $WEB_SG_ID --protocol tcp --port 443 --cidr 0.0.0.0/0
APP_SG_ID=$(aws ec2 create-security-group \ --group-name demo-app-sg \ --description "App tier" \ --vpc-id $VPC_ID \ --query GroupId --output text)
aws ec2 authorize-security-group-ingress \ --group-id $APP_SG_ID --protocol tcp --port 8080 --source-group $WEB_SG_IDStep 8: Add the S3 gateway endpoint
S3_ENDPOINT_ID=$(aws ec2 create-vpc-endpoint \ --vpc-id $VPC_ID \ --service-name com.amazonaws.$AWS_REGION.s3 \ --route-table-ids $PRIV_RT_ID \ --query VpcEndpoint.VpcEndpointId --output text)Step 9: Verify
aws ec2 describe-vpcs --vpc-ids $VPC_ID \ --query 'Vpcs[0].[VpcId,CidrBlock,State]' --output text
aws ec2 describe-subnets --filters Name=vpc-id,Values=$VPC_ID \ --query 'Subnets[].[SubnetId,CidrBlock,AvailabilityZone,MapPublicIpOnLaunch]' --output table
aws ec2 describe-route-tables --filters Name=vpc-id,Values=$VPC_ID \ --query 'RouteTables[].Routes[].[DestinationCidrBlock,GatewayId,NatGatewayId]' --output tableCheck that the public route table has a route to your
igw-... and the private route table has a route to your nat-... .Step 10: Clean up
Delete in this order, because AWS blocks deleting a resource that others depend on:
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids $S3_ENDPOINT_ID
aws ec2 delete-nat-gateway --nat-gateway-id $NAT_IDaws ec2 wait nat-gateway-deleted --nat-gateway-ids $NAT_IDaws ec2 release-address --allocation-id $EIP_ALLOC_ID
aws ec2 delete-subnet --subnet-id $PUB_SUBNET_IDaws ec2 delete-subnet --subnet-id $PRIV_SUBNET_ID
aws ec2 delete-route-table --route-table-id $PUB_RT_IDaws ec2 delete-route-table --route-table-id $PRIV_RT_ID
aws ec2 detach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_IDaws ec2 delete-internet-gateway --internet-gateway-id $IGW_ID
aws ec2 delete-security-group --group-id $APP_SG_IDaws ec2 delete-security-group --group-id $WEB_SG_ID
aws ec2 delete-vpc --vpc-id $VPC_IDConsole or CLI: which should you use?
- Console: best for learning and for seeing the layout in the Resource map. The "VPC and more" wizard builds a full network in one go.
- CLI: best for repeatable setup and scripting. You must create each piece and wire the IDs together yourself.
For production, codify your network as AWS CloudFormation, AWS CDK, or Terraform so every environment is identical and changes are reviewed in version control.
Best practices
- Plan your CIDR ranges early. Choose ranges that do not overlap with other VPCs or your on-premises network, because overlapping ranges cannot be peered or connected.
- Use at least two Availability Zones. Create a subnet pair in each zone and place a NAT gateway in each so a single zone failure does not take you offline.
- Keep databases and application servers in private subnets. Only load balancers and bastion-style entry points belong in public subnets.
- Use security groups as your main control. Reference other security groups as sources, and follow least privilege. Add NACLs only when you need explicit deny rules.
- Use VPC endpoints. Gateway endpoints for S3 and DynamoDB are free and reduce NAT gateway data charges. Use interface endpoints for other AWS services you call from private subnets.
- Turn on VPC Flow Logs. Send them to CloudWatch Logs or S3 to troubleshoot connectivity and detect unusual traffic.
- Tag everything. Consistent tags make cost tracking and cleanup much easier.
- Prefer Systems Manager Session Manager over SSH. It avoids opening port 22 to the internet.
- Watch NAT costs. NAT gateways bill per hour and per GB, so use endpoints and keep chatty traffic inside the VPC where possible.
Troubleshooting
- Instance in the public subnet has no internet access: check that the subnet's route table has
0.0.0.0/0pointing to the internet gateway, that the instance has a public IP or Elastic IP, and that its security group allows the traffic.
- Instance in the private subnet cannot reach the internet: check that the private route table points
0.0.0.0/0to the NAT gateway, that the NAT gateway is in a public subnet, and that the public subnet has an internet gateway route.
- Cannot connect to an instance from the internet: check the security group inbound rules, the NACL rules, and that the instance has a public IP.
- Cannot delete the VPC: a dependency still exists. Remove NAT gateways, endpoints, network interfaces, subnets, and the internet gateway first.
- Unexpected NAT gateway charges: the NAT gateway keeps billing until it is deleted. Release the Elastic IP after deleting it.
- Subnet CIDR error: the subnet range must fall inside the VPC range and not overlap with other subnets in the same VPC.
Conclusion
An Amazon VPC gives you complete control over your network on AWS. The key idea is simple: subnets are public or private based on their route tables, and gateways, endpoints, and security groups control how traffic flows. Use the console to understand the pieces, then move to the CLI or infrastructure as code to make your network repeatable. Start with one public and one private subnet, verify the routes, and then expand across multiple Availability Zones for production.
Console labels, pricing, and service limits change over time, so confirm current details in the Amazon VPC documentation before building.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article