What is Subnet in VPC: Simple Explanation and Example
subnet in a VPC (Virtual Private Cloud) is a smaller network segment inside the VPC that divides the IP address range into parts. It helps organize and control network traffic by grouping resources like servers in isolated sections within the cloud network.How It Works
Think of a VPC as a large apartment building with many rooms. A subnet is like dividing that building into smaller sections or floors. Each subnet has its own range of IP addresses, just like each floor has its own set of rooms. This helps keep things organized and secure.
When you launch resources like virtual servers (EC2 instances), you place them inside a subnet. This controls how they communicate with each other and with the internet. Some subnets can be public (open to the internet), while others are private (isolated inside the building).
Example
This example shows how to create a VPC with two subnets using AWS CloudFormation, a tool to set up cloud resources with code.
Resources:
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.0.1.0/24
AvailabilityZone: us-east-1a
MapPublicIpOnLaunch: true
PrivateSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.0.2.0/24
AvailabilityZone: us-east-1a
MapPublicIpOnLaunch: falseWhen to Use
Use subnets to organize your cloud network for better security and management. For example, put web servers in a public subnet so they can talk to the internet, and put databases in a private subnet to keep them safe from outside access.
Subnets also help control traffic flow and apply rules like firewalls. This setup is common in real-world apps where different parts need different levels of access.
Key Points
- A subnet divides a VPC’s IP address range into smaller parts.
- Subnets help isolate and organize resources inside the cloud network.
- Public subnets allow internet access; private subnets keep resources isolated.
- Using subnets improves security and traffic control.