AWS VPC Basics
Learn the basic building blocks of an Amazon Virtual Private Cloud (VPC).
What Is an AWS VPC?
An Amazon Virtual Private Cloud (VPC) is a logically isolated virtual network in AWS. It gives you control over IP address ranges, subnets, routing, internet access, and network security.
A VPC exists in one AWS Region. Resources in the VPC, such as EC2 instances, database instances, and load balancers, communicate using private IP addresses.
In this tutorial, we will explore the core concepts of AWS VPCs, including CIDR blocks, subnets, route tables. Also, we will cover internet gateways, NAT gateways, and other key networking components.
CIDR Block
CIDR stands for Classless Inter-Domain Routing. It is a method used to allocate IP address spaces in networks and subnets more flexibly than the older classful addressing scheme.
When you create a VPC, you assign an IPv4 CIDR block, such as 10.0.0.0/16. This CIDR block defines the private IP addresses available inside the VPC. The number after the slash (the prefix length) indicates how many bits of the IP address are used for the network portion.
A /16 network provides a large address range, while a /24 network provides 256 total addresses in IPv4. AWS reserves five IP addresses in every subnet, so the usable address count is lower than the CIDR size.
Choose CIDR ranges carefully. AWS requires VPC CIDR blocks to avoid conflicts when you plan to use VPC peering or another private network connection.
Subnets
A subnet is a section of a VPC CIDR block in one Availability Zone. Subnets divide a VPC into smaller networks.
For example, a VPC with 10.0.0.0/16 could use:
10.0.1.0/24for public web servers.10.0.2.0/24for private application servers.10.0.3.0/24for private database servers.
AWS considers a subnet public or private based on its route table, not a setting on the subnet itself.
A public subnet has a route to an internet gateway. A private subnet does not route directly to the internet.
Private subnets can access the internet through a NAT gateway. A NAT gateway sits in a public subnet, translates the source IP of outbound traffic, and forwards it to the internet gateway. This allows private resources to initiate outbound connections while preventing unsolicited inbound traffic from the internet.
Route Tables
A route table contains routes that determine where network traffic goes. Every subnet is associated with one route table.
A route table in a VPC might contain:
| Destination | Target |
|---|---|
10.0.0.0/16 |
local |
0.0.0.0/0 |
Internet gateway |
The local route allows resources in the VPC to communicate with one another. The 0.0.0.0/0 route sends other IPv4 traffic to the configured target.
A route target can also be another network component. Common targets include:
- Internet gateway: Sends IPv4 or IPv6 internet traffic from public subnets.
- NAT gateway: Sends outbound IPv4 internet traffic from private subnets.
- Virtual private gateway: Connects the VPC to an on-premises network through a Site-to-Site VPN or AWS Direct Connect.
- Transit gateway: Routes traffic through a central hub shared by multiple VPCs or networks.
- VPC peering connection: Sends traffic directly to a matching CIDR block in another VPC.
- VPC endpoint: Sends traffic privately to supported AWS services.
- Network interface or instance: Sends traffic to a specific network interface or EC2 instance, often for a network appliance.
AWS uses the most specific matching route to select a destination. For example, a route for 10.0.1.0/24 takes precedence over the general 0.0.0.0/0 route. Routes in a subnet’s route table only work if the target and security configuration allow the traffic.
Internet Gateway
An internet gateway enables communication between a VPC and the internet. Attach it to the VPC and add a route to it in the public subnet’s route table.
A resource in a public subnet also needs a public IPv4 address or an Elastic IP address to accept or initiate direct internet communication.
NAT Gateway
A NAT gateway allows resources in private subnets to make outbound IPv4 connections to the internet. It does not allow unsolicited internet connections to reach those resources.
A NAT gateway is placed in a public subnet and is assigned an Elastic IP address. The private subnet’s route table sends internet-bound traffic to the NAT gateway.
For examples and development environments, AWS also provides NAT instances, but a managed NAT gateway is usually simpler to operate.
Security Groups
A security group is a virtual firewall for AWS resources such as EC2 instances, Lambda functions, and database instances. It controls inbound and outbound traffic for each associated resource.
Security groups are stateful:
- If inbound traffic is allowed, the response traffic is usually allowed automatically.
- Return traffic does not need a separate outbound rule.
Security groups do not provide an explicit deny rule. Traffic is denied unless it is allowed by a rule.
Network ACLs
A network ACL is an optional subnet-level firewall. It controls traffic entering or leaving a subnet.
Network ACLs are stateless, so both inbound and outbound traffic require matching rules. They also support numbered rules with explicit allow and deny actions.
For most application designs, security groups provide sufficient instance-level protection, while network ACLs provide an additional subnet-level control.
VPC Endpoint
A VPC endpoint allows private access to supported AWS services without sending traffic through the internet. It is useful for services such as Amazon S3 and DynamoDB.
AWS supports interface endpoints and gateway endpoints. The correct endpoint type depends on the AWS service and the endpoint configuration.
Creating a VPC with AWS CLI
You can create a VPC and its basic components using the AWS CLI. Here’s an example of creating a VPC with a CIDR block:
1 | # Create a VPC with tags |
After creating the VPC, you can create subnets, route tables, and attach an internet gateway using similar AWS CLI commands.
Create Subnets with AWS CLI
1 | # Create a public subnet |
Example Architecture
1 | Internet |
The load balancer accepts public traffic, application servers remain private, and the database is accessible only from approved application resources.
Basic VPC Checklist
Before launching workloads:
- Plan non-overlapping CIDR blocks.
- Create subnets across multiple Availability Zones.
- Configure route tables for public and private subnets.
- Add an internet gateway if public inbound traffic is required.
- Use a NAT gateway for private subnet outbound internet access.
- Restrict security groups to required ports and sources.
- Add network ACL rules only when additional subnet-level control is needed.
- Enable VPC flow logs if network traffic monitoring is required.