Building a Secure AWS VPC with Public and Private Subnets: Bastion Host SSH Access
Table of Contents
- Introduction
- Architecture Overview
- Prerequisites
- Implementation
- Conclusion
1. Introduction
In this tutorial, we will build a basic AWS VPC architecture with public and private subnets to provide a secure environment for hosting EC2 instances.
The architecture consists of a public subnet hosting a Bastion Host and a private subnet hosting one or more EC2 instances. We will use the Bastion Host as an SSH gateway to securely access private instances without exposing them directly to the internet.
2. Architecture Overview
Download the editable here → Click here to download

3. Prerequisites
- An active AWS account
- Basic understanding of AWS networking
- Basic Linux and SSH knowledge
4. Implementation
-
Create a VPC
-
Go to VPC → Your VPCs → Create VPC.
-
Enter the VPC name (
demo-vpc) and IPv4 CIDR (172.16.0.0/16), then clickCreate VPC.
-
Go to VPC → Your VPCs → Create VPC.
-
Create Public and Private Subnets
-
Go to VPC → Subnets → Create subnet.
-
Select the VPC (
demo-vpc) you just created.
-
Under Subnet settings, enter the following information for the public
subnet:
Configuration Value Subnet name demo-sub-public-01Availability Zone us-east-1aIPv4 CIDR 172.16.0.0/24Click Add new subnet to create the private subnet.
-
Enter the following information for the private subnet:
Configuration Value Subnet name demo-sub-private-01Availability Zone us-east-1bIPv4 CIDR 172.16.1.0/24
-
Click Create subnet.
-
Result: You should now have two subnets in your VPC.
Note: At this stage, neither subnet is public or private yet. Their route table configurations determine their internet connectivity.
-
Go to VPC → Subnets → Create subnet.
-
Create and Attach an Internet Gateway
-
Go to VPC → Internet gateways → Create internet gateway.
-
Attach the Internet Gateway to your VPC (
demo-vpc):- Select
demo-igw - Click Actions → Attach to a VPC
-
Select
demo-vpc, then click Attach internet gateway.
- Select
-
Result: The Internet Gateway should now be attached to your
VPC.
-
Go to VPC → Internet gateways → Create internet gateway.
-
Configure Route Tables
Go to VPC → Route tables → Create route table.
-
Public Route Table
-
Enter the following route table information:
Configuration Value Name demo-public-rtVPC demo-vpc
-
Select
demo-public-rt→ Routes → Edit routes
-
Add the following route, then click Save changes.
Destination Target 0.0.0.0/0Internet Gateway ( demo-igw)
-
Associate the public subnet with the route table:
- Go to Subnet associations → Edit subnet associations.
- Select
demo-sub-public-01 - Click Save associations
-
Enter the following route table information:
-
Private Route Table
-
Create another route table named
demo-private-rtand selectdemo-vpc.
-
Under Subnet association, select
demo-sub-private-01and save the associations.
-
Create another route table named
Result: You should now have three route tables: one default route table, one public route table, and one private route table.
-
Public Route Table
-
Configure Security Groups (#27)
Go to EC2 → Security Groups → Create security group.
-
Bastion Host Security Group
-
Enter the security group details:
Configuration Value Name bastion-host-sgDescription Allow SSH from my machine VPC demo-vpc
-
Configure the inbound rule to allow SSH (TCP 22) from
My IP. Then click Create security group.
-
Enter the security group details:
-
Private EC2 Security Group
-
Create another security group with the following details:
Configuration Value Name demo-private-ec2-sgDescription Allow SSH from Bastion Host VPC demo-vpc
-
Configure the inbound rule to allow SSH (TCP 22) from
bastion-host-sg. Then click Create security group.
-
Create another security group with the following details:
-
Bastion Host Security Group
-
Launch the Bastion Host
-
Go to EC2 → Instances → Launch instances.
-
Enter the instance information and configure the following settings:
then click Launch instance.
Configuration Value Name bastion-hostAMI Select your preferred Linux AMI Instance type Choose your instance type Key pair Select your existing key pair ( ec2-user)VPC demo-vpcSubnet demo-sub-public-01Security group bastion-host-sgAuto-assign public IP Enable ( in the screen short, I forgot)
-
Go to EC2 → Instances → Launch instances.
-
Launch Private EC2 Instances
- Go to EC2 → Instances → Launch instances.
-
Enter the instance information and configure the following settings,
then click Launch instance.
Configuration Value Name private-ec2-01AMI Select your preferred Linux AMI Instance type Choose your instance type Key pair Select your key pair VPC demo-vpcSubnet demo-sub-private-01Security group demo-private-ec2-sgAuto-assign public IP Disable
-
Connect to the Bastion Host
-
Enable Auto-assign public IP is enabled for the Bastion Host.
-
From your local machine, SSH into the Bastion Host using its public IP
address:
ssh -i ec2-user.pem ec2-user@34.228.44.18
-
Copy the private SSH key to the Bastion Host so you can use it to
connect to the private EC2 instance.
-
Enable Auto-assign public IP is enabled for the Bastion Host.
-
Connect to Private EC2 Instances
-
From the Bastion Host, SSH into the private EC2 instance using its
private IP address:
ssh -i ec2-user ec2-user@172.16.1.238
You should now be connected to the private EC2 instance through the Bastion Host.
-
From the Bastion Host, SSH into the private EC2 instance using its
private IP address:
5. Conclusion
We have successfully built a basic AWS VPC environment with public and private subnets and configured SSH access to private EC2 instance through a Bastion Host.
This architecture provides a foundation for exploring more advanced AWS networking and infrastructure services.