How to Build an AWS VPC from Scratch: Network Guide
In this article
This guide walks you through creating a custom Amazon Virtual Private Cloud (VPC) from the ground up in the AWS Management Console. You’ll set up subnets, an internet gateway, route tables, and security groups to launch resources in a private, isolated network. Before starting, ensure you have an AWS account with billing enabled and the IAM user you’re using has permissions for VPC, EC2, and related services.
What You'll Need
- AWS account with appropriate IAM permissions (AdministratorAccess or VPC-specific policies).
- Basic understanding of IP addressing (CIDR notation).
- A web browser with access to the AWS Management Console.
Step-by-Step
- Open the VPC Console: Log in to AWS, navigate to Services > Networking & Content Delivery > VPC. Ensure you’re in the correct region (top-right corner).
- Create the VPC: Click “Your VPCs” in the left sidebar, then “Create VPC.” Choose “VPC only” (not “VPC and more”). Enter a name (e.g., “MyVPC”), and set IPv4 CIDR to
10.0.0.0/16(gives 65,536 IPs). Leave IPv6 and tenancy as defaults. Click “Create VPC.” - Create Subnets: Go to “Subnets” > “Create subnet.” Select your VPC. Create at least two subnets: one public and one private. For example, public:
10.0.1.0/24(usable IPs: 10.0.1.1-254) in Availability Zone A; private:10.0.2.0/24in AZ B. Name them clearly (e.g., “PublicSubnetA”, “PrivateSubnetB”). Click “Create subnet” for each. - Enable Auto-Assign Public IP for Public Subnet: Select the public subnet, click “Actions” > “Edit subnet settings.” Check “Enable auto-assign public IPv4 address” and save. This ensures instances in this subnet get public IPs automatically.
- Create an Internet Gateway (IGW): Go to “Internet Gateways” > “Create internet gateway.” Name it (e.g., “MyIGW”) and create. Then select it, click “Actions” > “Attach to VPC,” choose your VPC, and attach.
- Create a Route Table for Public Subnet: Go to “Route Tables” > “Create route table.” Name it (e.g., “PublicRT”), select your VPC, and create. Select the new route table, click “Actions” > “Edit routes,” then “Add route.” Destination
0.0.0.0/0, target “Internet Gateway,” choose your IGW. Save. - Associate Public Subnet with Public Route Table: In the route table’s “Subnet associations” tab, click “Edit subnet associations,” check the public subnet, and save.
- Create a Route Table for Private Subnet: Repeat step 6, but name it “PrivateRT.” Do NOT add a route to the internet. Associate the private subnet with this route table (as in step 7).
- Create a NAT Gateway for Private Subnet (Optional but Recommended): If instances in the private subnet need outbound internet (e.g., for updates), create a NAT gateway. Go to “NAT Gateways” > “Create NAT gateway.” Choose the public subnet, allocate an Elastic IP (or create one), and create. Then edit PrivateRT: add route
0.0.0.0/0targeting the NAT gateway ID. Note: NAT gateway incurs costs. - Create Security Groups: Go to “Security Groups” > “Create security group.” Create a WebSG for public instances: allow inbound HTTP (80) and HTTPS (443) from
0.0.0.0/0, and SSH (22) from your IP (use “My IP” option). Create a DBSG for private instances: allow inbound MySQL/Aurora (3306) or PostgreSQL (5432) from the WebSG’s ID (or from the public subnet CIDR). - Launch Resources: Go to EC2, launch an instance. For a public web server, choose an Amazon Linux or Ubuntu AMI, select your VPC and public subnet, enable auto-assign public IP (should be on), attach WebSG. For a private database, choose a compatible AMI, select private subnet, attach DBSG. Test connectivity: SSH to public instance, then from there, try to reach the private instance using its private IP.
- Verify Network Paths: Use VPC Flow Logs (optional) to monitor traffic. Also, test that the public instance can access the internet (ping 8.8.8.8 or curl example.com) and that the private instance cannot be reached from the internet.
Common Mistakes
- Wrong CIDR overlap: Using overlapping CIDR blocks for subnets (e.g., 10.0.1.0/24 and 10.0.1.128/25) causes routing issues. Always ensure subnets do not overlap.
- Forgetting to attach the internet gateway: If your public instances can’t reach the internet, the IGW is often not attached to the VPC.
- Missing route table association: Even with a correct route, if the subnet isn’t associated with the route table, it uses the default (local) route only.
- Not enabling auto-assign public IP: Instances launched in public subnets without this setting won’t get a public IP unless you manually assign one at launch.
- Security group rules too permissive: Opening all ports (0-65535) to 0.0.0.0/0 is a common security risk. Restrict to necessary ports and sources.
- Using a NAT gateway in a private subnet: NAT gateways must be in a public subnet to work.
Tips
- Use descriptive names and tags: Tag every resource with Name, Environment, and Owner to keep track.
- Plan your CIDR carefully: Choose a CIDR block that doesn’t conflict with on-premises networks if you’ll later connect via VPN or Direct Connect.
- Use multiple AZs for high availability: Distribute subnets across at least two availability zones for resilience.
- Consider VPC endpoints: For private subnets, use VPC endpoints (e.g., S3, DynamoDB) to avoid NAT gateway costs and improve security.
- Enable DNS hostnames and DNS resolution: In your VPC settings, ensure “Enable DNS hostnames” and “Enable DNS resolution” are checked for proper DNS behavior.
Troubleshooting
- No internet from public instance: Check that the IGW is attached, the public route table has a 0.0.0.0/0 route to the IGW, and the subnet is associated with that route table. Also, ensure the instance has a public IP and the security group allows outbound traffic (default allows all).
- Cannot SSH to public instance: Verify security group allows inbound SSH from your IP. Also check that the instance has a public IP and that your local firewall isn’t blocking port 22.
- Private instance cannot reach internet: If you set up a NAT gateway, ensure it’s in a public subnet, has an Elastic IP, and the private route table has a 0.0.0.0/0 route to the NAT. Also check that the NAT gateway’s security group (if any) allows outbound traffic.
- Subnets show “not available” for launch: This usually means the subnet is in an AZ that doesn’t support the instance type. Try a different AZ or instance type.
- Route table not working: Verify the route table is associated with the correct subnet. In the VPC console, select the subnet and check the “Route table” tab.
- Ping works but SSH fails: ICMP may be allowed by security group, but TCP 22 is not. Check security group rules.
Frequently Asked Questions
Can I create a VPC without an internet gateway?
Yes, a VPC can be completely isolated. Simply don’t attach an IGW and don’t add a 0.0.0.0/0 route. This is common for private networks.
How many subnets can I have in a VPC?
AWS allows up to 200 subnets per VPC. You can create subnets in each AZ, and each subnet must be within the VPC CIDR.
What is the difference between a public and private subnet?
A public subnet has a route to the internet via an IGW and typically auto-assigns public IPs. A private subnet has no direct internet route; it uses a NAT gateway for outbound traffic or is fully isolated.
Do I need a NAT gateway for a private subnet?
Only if instances in that subnet need to access the internet (e.g., for updates). If not, you can skip it and save costs.
Can I change my VPC CIDR after creation?
No, you cannot modify the primary CIDR block of an existing VPC. You can add secondary CIDR blocks, but the original is fixed.
How do I delete a VPC?
First, terminate all instances, delete subnets, route tables, security groups, NAT gateways, and detach/delete the IGW. Then you can delete the VPC.