AWS Builder Center

Building a Secure AWS VPC with Public and Private Subnets: A Hands-On Guide

Learn how to design and configure a secure Amazon VPC using public and private subnets, internet gateways, NAT gateways, route tables, and security groups. This hands-on guide demonstrates how to isolate application resources while maintaining controlled internet connectivity.

Building a Secure AWS VPC with Public and Private Subnets: A Hands-On Guide

Introduction

When deploying applications on AWS, network security is one of the most important considerations. Applications often require internet connectivity, but not every resource should be directly accessible from the public internet.
Amazon Virtual Private Cloud (Amazon VPC) allows developers to create an isolated virtual network where they can control IP addressing, subnet configuration, routing, and network access.
In this hands-on guide, we will build a secure AWS network using Amazon VPC with public and private subnets. We will configure an Internet Gateway, NAT Gateway, route tables, and security groups to control communication between resources and the internet.
By the end of this tutorial, you will understand how to separate internet-facing resources from internal application resources.

1. Understanding the Architecture

Before creating the infrastructure, let's understand the components involved.
Our architecture will contain:
  • Amazon VPC: Provides an isolated virtual network.
  • Public Subnets: Host resources that require direct internet connectivity.
  • Private Subnets: Host internal application resources that should not be directly accessible from the internet.
  • Internet Gateway (IGW): Enables communication between the VPC and the internet.
  • NAT Gateway: Allows resources in private subnets to initiate outbound internet connections.
  • Route Tables: Determine where network traffic is directed.
  • Security Groups: Control inbound and outbound traffic at the resource level.

Architecture Overview

The architecture follows this network flow:
  1. Internet traffic enters the VPC through the Internet Gateway.
  1. Public subnet resources can communicate with the internet using their public IP addresses and appropriate security rules.
  1. Private subnet resources cannot receive unsolicited internet connections.
  1. Private resources can access the internet through a NAT Gateway located in a public subnet.
  1. Security groups restrict communication between resources.

2. Creating an Amazon VPC

First, sign in to the AWS Management Console.
  1. Open the Amazon VPC console.
  1. Navigate to Your VPCs.
  1. Select Create VPC.
  1. Choose the VPC only option.
  1. Configure the following details:
Configuration
Value
Name
Secure-VPC
IPv4 CIDR
10.0.0.0/16
IPv6 CIDR
No IPv6 CIDR block
Tenancy
Default
  1. Click Create VPC.
The VPC provides the private network address space in which we will create our subnets and networking components.

3. Creating Public and Private Subnets

Subnets divide the VPC into smaller network segments.
We will create four subnets across two Availability Zones to improve network availability.
Navigate to Subnets → Create subnet and select the Secure-VPC.
Create the following subnets:
Subnet Name
Availability Zone
IPv4 CIDR
Public-Subnet-1
Availability Zone A
10.0.1.0/24
Private-Subnet-1
Availability Zone A
10.0.2.0/24
Public-Subnet-2
Availability Zone B
10.0.3.0/24
Private-Subnet-2
Availability Zone B
10.0.4.0/24
Replace Availability Zone A and B with two actual Availability Zones available in your selected AWS Region.

Configure Public Subnets

Select each public subnet and enable:
Auto-assign public IPv4 address
This allows newly launched EC2 instances in these subnets to receive public IPv4 addresses automatically.
Keep this setting disabled for private subnets.
Remember that a subnet is not public simply because it has a public IP address. Its route table must also provide a route to an Internet Gateway.

4. Creating and Attaching an Internet Gateway

An Internet Gateway allows communication between resources in a VPC and the public internet.
Follow these steps:
  1. Navigate to Internet Gateways.
  1. Select Create internet gateway.
  1. Enter the name  Secure-IGW .
  1. Click Create internet gateway.
  1. Select the newly created gateway.
  1. Choose Actions → Attach to a VPC.
  1. Select  Secure-VPC .
The Internet Gateway is now attached to our VPC.
However, attaching an Internet Gateway alone does not make a subnet publicly accessible. We must configure the route table.

5. Configuring Public Route Tables

Route tables determine how network traffic moves within and outside the VPC.
Navigate to Route Tables → Create route table.
Create a route table with these details:
  • Name:  Public-Route-Table 
  • VPC:  Secure-VPC 
After creating it:
  1. Open the route table.
  1. Navigate to the Routes tab.
  1. Select Edit routes.
  1. Add the following route:
Destination
Target
10.0.0.0/16
local
0.0.0.0/0
Internet Gateway
Save the changes.
Next, navigate to Subnet associations → Edit subnet associations.
Associate:
  • Public-Subnet-1
  • Public-Subnet-2
Now, resources in these public subnets can communicate with the internet when they have suitable public addressing and security configurations.

6. Creating a NAT Gateway

Private subnet resources should not be directly exposed to the internet. However, some applications need outbound internet access for software updates, package installation, or external API requests.
A NAT Gateway provides this functionality.
To create one:
  1. Navigate to NAT Gateways.
  1. Select Create NAT Gateway.
  1. Enter the name  Secure-NAT .
  1. Select  Public-Subnet-1 .
  1. Choose Public as the connectivity type.
  1. Allocate an Elastic IP address.
  1. Click Create NAT Gateway.
Wait until the NAT Gateway status becomes Available.
The NAT Gateway must be deployed in a public subnet with a route to the Internet Gateway.
Important: A public NAT Gateway allows instances in private subnets to initiate outbound connections. It does not allow unsolicited inbound internet connections to those instances.

7. Configuring Private Route Tables

Now we need a separate route table for the private subnets.
  1. Navigate to Route Tables.
  1. Select Create route table.
  1. Enter the name  Private-Route-Table .
  1. Select  Secure-VPC .
  1. Create the route table.
Add the following routes:
Destination
Target
10.0.0.0/16
local
0.0.0.0/0
NAT Gateway
Save the changes.
Associate the route table with:
  • Private-Subnet-1
  • Private-Subnet-2
Private subnet resources can now initiate outbound internet connections through the NAT Gateway without requiring public IP addresses.
For production environments, consider deploying a separate NAT Gateway in each Availability Zone and routing each private subnet through its same-AZ NAT Gateway. This improves availability but increases cost.

8. Configuring Security Groups

Security Groups act as virtual firewalls that control inbound and outbound traffic for supported AWS resources.
Navigate to EC2 → Security Groups → Create security group.
Create a security group named  Public-SG .
Configure:
Inbound rules
Type
Protocol
Port
Source
HTTP
TCP
80
0.0.0.0/0
HTTPS
TCP
443
0.0.0.0/0
SSH
TCP
22
Your public IP address only
For a private application instance, create another security group named  Private-SG .
Configure its inbound rules to allow application traffic only from the trusted public-tier security group.
For example:
Type
Protocol
Port
Source
Custom TCP
TCP
8080
Public-SG
Do not expose SSH or application administration ports to the entire internet.
Security groups are stateful, meaning response traffic for permitted connections is automatically allowed.

9. Testing the VPC Configuration

After configuring the network, we should validate whether the architecture works as expected.
Launch two EC2 instances:
Instance 1: Public EC2 Instance
  • Select  Public-Subnet-1 .
  • Enable public IPv4 addressing.
  • Attach  Public-SG .
Instance 2: Private EC2 Instance
  • Select  Private-Subnet-1 .
  • Disable public IPv4 addressing.
  • Attach  Private-SG .
Ensure the EC2 instances have appropriate IAM permissions if you use AWS Systems Manager Session Manager for administration.

Test 1: Public Connectivity

Connect to the public EC2 instance using SSH or Systems Manager.
Test internet connectivity:
ping -c 4 8.8.8.8
Alternatively, use:
curl -I https://aws.amazon.com
The public instance should be able to access the internet if its routes and security rules are correctly configured.

Test 2: Private Subnet Connectivity

Connect to the private EC2 instance using Systems Manager Session Manager, provided the necessary IAM role and Systems Manager connectivity are configured.
Run:
curl -I https://aws.amazon.com
The private instance should be able to initiate outbound internet connections through the NAT Gateway.

Test 3: Verify Network Isolation

Check that the private EC2 instance does not have a public IPv4 address.
Also verify that unsolicited inbound connections from the internet cannot directly reach the private instance.
These tests demonstrate the separation between public and private network resources.

10. Security Best Practices

When designing an AWS VPC, consider the following practices:
  • Place internal application servers and databases in private subnets.
  • Restrict inbound access using security groups.
  • Avoid allowing SSH access from  0.0.0.0/0 .
  • Use AWS Systems Manager Session Manager instead of exposing SSH ports wherever practical.
  • Use multiple Availability Zones for improved availability.
  • Monitor network traffic using VPC Flow Logs.
  • Apply least-privilege IAM permissions.
  • Use VPC endpoints where appropriate to access supported AWS services without relying on internet connectivity.
  • Review route tables and network access regularly.

11. Cost Considerations

Although Amazon VPC itself does not have an additional charge for creating a VPC or subnet, some networking components incur costs.
Examples include:
  • NAT Gateway hourly usage and data processing.
  • Elastic IP address charges under applicable AWS pricing conditions.
  • Data transfer charges.
  • EC2 instance usage.
For a learning environment, create only the resources you need and delete them after testing.
Remember to delete the NAT Gateway and release its associated Elastic IP address when they are no longer required.

12. Cleaning Up AWS Resources

To avoid unnecessary charges, remove the resources created during this tutorial.
Follow this order:
  1. Terminate the EC2 instances.
  1. Delete the NAT Gateway.
  1. Release the Elastic IP address after it is no longer associated.
  1. Delete custom route tables after removing their subnet associations.
  1. Detach and delete the Internet Gateway.
  1. Delete the subnets.
  1. Delete the security groups after dependent resources are removed.
  1. Delete the VPC.
Verify that no billable resources remain.

Conclusion

Amazon VPC provides the networking foundation for building secure and scalable applications on AWS.
By combining public and private subnets with Internet Gateways, NAT Gateways, route tables, and security groups, we can control how resources communicate with each other and the internet.
The architecture demonstrated in this tutorial provides a foundation for more advanced AWS deployments, including multi-tier web applications, microservices, and production workloads.
Understanding VPC networking is an essential skill for developers and cloud engineers working with AWS infrastructure.
Additional Resources
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article