AWS [VPC]
Introduction
In the world of cloud computing, understanding networking is essential. AWS Virtual Private Cloud (VPC) allows you to create an isolated and customizable network environment within the AWS cloud, offering granular control over your infrastructure. This guide walks you through setting up your own VPC, introduces concepts like CIDR and subnets, and includes steps for launching a basic EC2 instance inside your VPC.
What is a VPC?
Key Benefits of Using a VPC
- Security: You can isolate your resources, apply access controls using security groups and network ACLs, and create secure connections (like VPNs) to your on-premises environment.
- Customization: Define your network topology, CIDR blocks, subnet sizes, and routing to match your application’s requirements.
- High Availability: Spread resources across multiple Availability Zones (AZs) for redundancy and fault tolerance, ensuring higher uptime.
Understanding CIDR (Classless Inter-Domain Routing)
Key Concepts of CIDR:
- CIDR Notation: Represents IP addresses with a base IP and a subnet mask (e.g.,
192.168.1.0/24
). The/24
specifies the first 24 bits as the network part of the address. - Subnetting: Allows you to divide a larger network into smaller sub-networks (subnets), giving better management and use of IP address space.
- Routing Efficiency: Aggregates multiple IP addresses into a single routing rule, reducing the complexity of network routing tables.
CIDR Block Sizes
CIDR NotationNumber of IP AddressesUsable IP Addresses/8
16,777,21616,777,214/16
65,53665,534/24
256254/28
1614
When designing your VPC, the CIDR block you select should accommodate your future growth and resource requirements. AWS recommends using non-overlapping IP ranges, such as 10.0.0.0/16
or 192.168.0.0/16
.
Step 1: Access the VPC Dashboard
- Log in to the AWS Management Console.
- Search for VPC in the services menu and select it.
Tip: Before setting up, plan your CIDR blocks and decide how many subnets (public and private) you will need. Having a clear network architecture helps avoid mistakes later on.
Step 2: Create a VPC
-
- In the left menu, click on Your VPCs.
- Click Create VPC and fill in the following details:
- Name tag: e.g., MyVPC
- IPv4 CIDR block: e.g.,
10.0.0.0/16
- Tenancy: Default (unless you need dedicated hardware).
- Click Create VPC.
Tip: Choose the CIDR block carefully. A
/16
block provides 65,536 IP addresses—more than enough for most environments.
Step 3: Create Subnets
- In the VPC dashboard, click on Subnets.
- Click Create Subnet and fill in:
- Name tag: e.g., PublicSubnet
- VPC: Select the VPC you just created.
- Availability Zone (AZ): Choose an AZ (e.g.,
us-east-1a
). - IPv4 CIDR block: e.g.,
10.0.1.0/24
.
- Click Create Subnet.
- Repeat the process for a private subnet with a CIDR block like
10.0.2.0/24
.
Tip: Public subnets are for resources needing internet access (e.g., web servers), while private subnets are for resources that should remain internal (e.g., databases).
Step 4: Create an Internet Gateway
- In the VPC dashboard, select Internet Gateways.
- Click Create Internet Gateway, give it a name (e.g., MyInternetGateway), and click Create.
- Attach it to your VPC using Actions → Attach to VPC.
Tip: Internet Gateways enable communication between resources in your VPC and the internet. Only instances in public subnets can access the internet through this gateway.
Step 5: Update Route Tables
- Navigate to Route Tables in the VPC dashboard.
- Select the route table associated with your public subnet and click the Routes tab.
- Edit the routes and add the following:
- Destination:
0.0.0.0/0
(indicating all traffic) - Target: Select the Internet Gateway you just created.
- Save the changes.
Tip: Always ensure that your public subnet route table points to the internet gateway; otherwise, your instances won’t be able to connect to the internet.
Step 6: Configure Security Groups
- In the VPC dashboard, navigate to Security Groups.
- Click Create Security Group and fill in:
- Name tag: e.g., WebServerSG
- Description: e.g., Allow HTTP and SSH access.
- VPC: Select your VPC.
- Set inbound rules:
- Type: HTTP | Port Range: 80 | Source:
0.0.0.0/0
- Type: SSH | Port Range: 22 | Source: Your IP (or
0.0.0.0/0
for testing).
Tip: Never leave SSH open to all IP addresses (
0.0.0.0/0
). For production, always restrict SSH to specific IPs or use VPN access.
Step 7: Launch an EC2 Instance
- In the EC2 Dashboard, click Launch Instance.
- Choose an Amazon Machine Image (AMI).
- Select an instance type (e.g.,
t2.micro
for the free tier). - Under Network settings, select your VPC and choose the public subnet.
- Attach the security group you created earlier.
- Complete the launch process.
Tip: If the instance is in a private subnet, it cannot access the internet unless you configure a NAT Gateway (Step 8)
Step 8: (Optional) Set Up a NAT Gateway
- In the VPC dashboard, go to NAT Gateways and click Create NAT Gateway.
- Choose the public subnet and allocate an Elastic IP.
- Update your private subnet route table to route
0.0.0.0/0
to the NAT Gateway.
Tip: Use NAT Gateways to allow internet access from private subnets without exposing them directly.