Connect to a Private Instance using a Bastion Host in EC2 inside a Custom Virtual Private Cloud (VPC) in AWS

Create a VPC

1. Navigate to VPC

Create Public and Private Subnet

1. Navigate to Subnets

  • From Actions drop down select Modify auto-assign IP settings
  • Select Enable auto-assign public IPv4 address

Create Internet Gateway

1. Navigate to Internet Gateways

Create NAT Gateway

1. Navigate to NAT gateways

  • Subnet — Select your public subnet
  • Elastic IP allocation ID — Click Allocate Elastic IPx

Create Public and Private Route Table

1. Navigate to Route Tables

  • VPC — Select VPC created previously
  • In Routes tab select Edit Routes
  • Click add route, set Destination to 0.0.0.0/0 and for Target select Internet Gateway created
  • In Subnet association select Edit subnet associations under Explicit Subnet Associations
  • Select the public subnet created
  • VPC — Select VPC created previously
  • Click Create Route Table
  • In Routes tab select Edit Routes
  • Click add route, set Destination to 0.0.0.0/0 and for Target select NAT Gateway created
  • In Subnet association select Edit subnet associations under Explicit Subnet Associations
  • Select the private subnet created

Create EC2 Public Instance

1. Navigate to EC2

  • Subnet — Auto populates with Public Subnet
  • Auto-assign Public IP — Auto populates with Use subnet setting (Enable)
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
  • Description — Description desired
  • Rules as follows:

Create EC2 Private Instance

1. Navigate to EC2

  • Subnet — Change to Private Subnet
  • Auto-assign Public IP –Use subnet setting (Disable)
  • Description — Description desired
  • Rule as follows — For source select Custom and choose your Bastion Host.

Connecting to your Private Instance from your Bastion Host

1. Verify you instances are running.

  • Select everything in the file.
  • From the command line type “VIM MyKeyBB.pem” without the double quotes and the file name could be whatever you want it to be.
  • Paste the information you copied from your original private key.
  • Now hit ESC followed by :wq, your private key has been created inside the Bastion Host.

Clean Up

We have now successfully connected to Private Instance using a Bastion Host and could proceed with removing instances and VPC created since we will no longer need them. This was used for demonstration purpose only.

  • Delete Internet Gateway created.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store