Building a Secure AWS VPC with Public and Private Subnets

Building a Secure AWS VPC with Public and Private Subnets: Bastion Host SSH Access


Table of Contents

  1. Introduction
  2. Architecture Overview
  3. Prerequisites
  4. Implementation
  5. 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

  1. 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 click Create VPC.
  2. 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-01
      Availability Zone us-east-1a
      IPv4 CIDR 172.16.0.0/24

      Click Add new subnet to create the private subnet.

    • Enter the following information for the private subnet:
      Configuration Value
      Subnet name demo-sub-private-01
      Availability Zone us-east-1b
      IPv4 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.

  3. 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.
    • Result: The Internet Gateway should now be attached to your VPC.
  4. 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-rt
        VPC demo-vpc
      • Select demo-public-rt → Routes → Edit routes
      • Add the following route, then click Save changes.
        Destination Target
        0.0.0.0/0 Internet 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
    • Private Route Table
      • Create another route table named demo-private-rt and select demo-vpc.
      • Under Subnet association, select demo-sub-private-01 and save the associations.

    Result: You should now have three route tables: one default route table, one public route table, and one private route table.

  5. 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-sg
        Description 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.
    • Private EC2 Security Group
      • Create another security group with the following details:
        Configuration Value
        Name demo-private-ec2-sg
        Description 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.
  6. 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-host
      AMI Select your preferred Linux AMI
      Instance type Choose your instance type
      Key pair Select your existing key pair (ec2-user)
      VPC demo-vpc
      Subnet demo-sub-public-01
      Security group bastion-host-sg
      Auto-assign public IP Enable ( in the screen short, I forgot)
  7. 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-01
      AMI Select your preferred Linux AMI
      Instance type Choose your instance type
      Key pair Select your key pair
      VPC demo-vpc
      Subnet demo-sub-private-01
      Security group demo-private-ec2-sg
      Auto-assign public IP Disable
  8. 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.
  9. 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.

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.