Sub topic 1
What a VPC is and how it is isolated
A Virtual Private Cloud (VPC) is a logically isolated virtual network that you define inside an AWS Region. Your EC2 instances, load balancers, databases, and other network-attached resources run inside it. This chapter covers what a VPC contains, what keeps it separate from other networks, and how to create one with the AWS CLI. Later chapters cover each piece in detail.
Prerequisites
This chapter assumes you already have the following:
- Basic IP addressing and CIDR notation. You know that
10.0.0.0/16means a fixed 16-bit prefix and 65,536 addresses. - Familiarity with TCP/UDP ports and the idea of a firewall.
- An AWS account, and a basic understanding of Regions and Availability Zones (AZs).
- Comfort running AWS CLI commands, with credentials configured and a default Region set.
The working model
A VPC is a software-defined network. AWS implements it on its own infrastructure, and you configure it through API calls. You don't manage switches or cables. Four properties describe it:
- It belongs to one Region. It spans every AZ in that Region, but it cannot span Regions.
- It owns one or more IP address ranges. You choose a primary IPv4 CIDR block when you create the VPC. IPv4 blocks must be between
/16(65,536 addresses) and/28(16 addresses). You can add secondary CIDR blocks later. - It is private by default. A new custom VPC has no path to the internet and no path to any other VPC.
- It belongs to one account. Other accounts can reach your VPC only through connections that you explicitly create or share.
The VPC itself is only a boundary and an address range. You fill it with other objects: subnets, route tables, gateways, and firewalls. The next chapters cover these. This chapter introduces the ones that exist the moment you create a VPC.
What a new VPC contains automatically
When you create a VPC, AWS also creates three companion objects. You never see them as separate create calls, but they define the VPC's initial behavior:
| Object | Initial behavior |
|---|---|
| Main route table | Contains one route: your VPC CIDR block with target `local`. Subnets with no explicit association use this table. |
| Default network ACL | Allows all inbound and outbound traffic at the subnet boundary. |
| Default security group | Allows all traffic between members of the group, denies all other inbound traffic, and allows all outbound traffic. |
The local route is what lets any two resources in the VPC reach each other. It exists in every route table, and you cannot delete it. This is why two instances in different subnets can talk to each other without any router configuration, provided their security groups and network ACLs allow it.
How isolation works
Isolation comes from several layers working together. Understanding them helps you reason about what is and isn't reachable.
Network-level separation
AWS runs your VPC traffic over a virtual network layer. Packets between instances are encapsulated and mapped to your VPC, and the hypervisor layer (AWS Nitro) enforces the mapping. An instance cannot see another VPC's traffic, and it cannot spoof its way into one. Two VPCs, even in the same account, can use the same CIDR block (10.0.0.0/16 in both, for example) without conflict. They only conflict if you later try to connect them, because overlapping ranges cannot be routed.
No implicit external paths
A VPC has no route to the internet until you do three things:
- Attach an internet gateway to the VPC.
- Add a route to it (typically
0.0.0.0/0) in a route table. - Give the resource a public IP address, and allow the traffic in its security group and network ACL.
Without all three, traffic cannot enter or leave. Private connectivity to other VPCs, to on-premises networks, and to AWS services also requires explicit objects: peering connections, gateways, or endpoints.
Layered filtering
Two firewalls apply inside a VPC. Security groups act at the network interface of each resource. Network ACLs act at the subnet boundary. Both are covered in later chapters. For now, remember that isolation is a default-deny posture for inbound traffic, and you open only what you need.
What isolation does not give you
- It does not hide instances from each other inside the VPC. The default security group allows traffic between its members, and the
localroute lets any subnet reach any other. Segmentation inside a VPC is your job, done through security groups and subnet design. - It does not encrypt traffic automatically. Traffic within a VPC stays on the AWS network. Some instance types encrypt traffic between them in transit, but you should not rely on this for application-level confidentiality.
- It does not isolate API access. Calls to AWS service APIs, such as S3 or DynamoDB, go to public service endpoints unless you add VPC endpoints.
Default VPC versus custom VPC
Each Region in a standard account has a default VPC with a 172.31.0.0/16 CIDR block. Its subnets are already public, an internet gateway is attached, and the routing is preconfigured. It is convenient for experiments, but it is a poor base for production. You cannot choose its address range, and its subnets give every instance a public IP address by default.
For anything real, create a custom VPC, where you control the addressing and the exposure.
Creating a VPC
The following commands create a VPC, enable DNS hostnames on it, and add two subnets in one AZ. The subnets are empty shells here. Later chapters turn one into a public tier and one into a private tier.
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id "$VPC_ID" --enable-dns-hostnames
PUBLIC_SUBNET_ID=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.0.1.0/24 --availability-zone us-east-1a --query Subnet.SubnetId --output text)
PRIVATE_SUBNET_ID=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.0.2.0/24 --availability-zone us-east-1a --query Subnet.SubnetId --output text)Check what AWS created. The first command shows the VPC's state and CIDR. The second shows the main route table with its single local route.
aws ec2 describe-vpcs --vpc-ids "$VPC_ID" \
--query 'Vpcs[0].{Id:VpcId,Cidr:CidrBlock,State:State,Tenancy:InstanceTenancy}'
aws ec2 describe-route-tables \
--filters Name=vpc-id,Values="$VPC_ID" Name=association.main,Values=true \
--query 'RouteTables[0].Routes'The route output shows one entry with DestinationCidrBlock of 10.0.0.0/16 and GatewayId of local. Nothing else is present, which confirms the VPC has no internet path yet.
Tenancy and quotas
Two settings are fixed or limited at creation time.
- Instance tenancy. The default is
default, meaning instances run on shared hardware. Settingdedicatedforces every instance launched into the VPC onto single-tenant hardware, which costs more. You can't change a VPC fromdedicatedback todefault, so leave it alone unless a compliance requirement demands it. - Quotas. By default, an account can have 5 VPCs per Region, and each VPC can have 5 IPv4 CIDR blocks. Both can be raised through Service Quotas. Check the current values before designing a layout with many VPCs.
Common errors
- Choosing a CIDR block that is too small. You cannot resize the primary block. You can add secondary blocks, but this adds complexity, so size generously.
- Overlapping with another network. Overlap is harmless until you try to peer VPCs or connect to on-premises. Then it forces a rebuild or address translation. The next chapter covers planning.
- Assuming a new VPC can reach the internet. It can't. A timeout from a new instance usually means a missing internet gateway, route, or public IP.
- Treating the default security group as a trust boundary. Putting every resource in it lets them all talk to each other. Create purpose-specific groups instead.
- Relying on the default VPC for production. Its public-by-default subnets expose resources more easily than you intend.
Exercise
You create a custom VPC with CIDR block 10.0.0.0/16 and launch an instance in a new subnet. The instance has a public IP address and a security group that allows all outbound traffic, but it cannot reach the internet. Which VPC-level objects are missing, and why does the instance still reach other subnets in the VPC?
Show answer
The VPC has no internet gateway attached and no route to one (for example 0.0.0.0/0 pointing to the gateway) in the subnet's route table. The public IP alone does not create a path. The instance reaches other subnets because every route table has an undeletable local route for the VPC CIDR block, and the default network ACL and security rules permit that traffic.
Exercise
Two teams each create a VPC with CIDR block 10.0.0.0/16 in separate accounts. Do the VPCs conflict while they are separate? What problem appears if they later want to connect?
Show answer
They do not conflict while separate, because each VPC is an independent virtual network. If they want to connect (for example with peering), the overlapping ranges cannot be routed, so the connection cannot be set up without changing or re-addressing one of the VPCs.