Now your not going to want to expose all your AWS EC2 instances on the internet for obvious reasons. So how do you connect to it? Setting up and connecting via a ‘Bastion’ server is the quickest and easiest way to connect. A Bastion server is a server that does nothing more than allow you to connect to EC2 instances that are on a private subnet in your VPC. In this guide, I cover how to do this securely via SSH. If you are using Mac, then the SSH agent is installed by default and is accessed via the ‘Terminal’ app.
This guide assumes:
- You have already launched an EC2 to act as your Bastion server, and it is internet facing
- It resides in the same VPC as the machine you wish to access
- You have created and applied a security group for SSH access
- You are using a Mac.
The following guide covers:
- Adding your private key to the keychain
- Verifying the keys that are available for SSH to use
- Connecting to your Bastion server
- How to connect to your private Subnet EC2 instance.
Add your private key to your keychain
By adding your private key to the key chain, you can avoid having to specify which private key to use each time you connect, making life more comfortable.
- Open the ‘Terminal App’
- Type ‘ssh-add -K YourKeyName.pem’
- The SSH agent will now prompt you for further information, complete this information and press return
- Verify your key by typing ‘ssh-add list. This returns a long string and your key name.
The key has now been added.
If you skip this step you will need to specify the required key using the ‘-i <YourKeyName.pem>’.
Connect to your Bastion server
- Type ‘ssh –A ec2-user@<bastion-external IP-address or external-DNS-entry>’
The connection has now been established.
Connect to your private subnet EC2 instance
- Type ‘ssh ec2-user@<internal-IP-address or internal-DNS-entry>’
You have now connected to your EC2 instance on your private subnet. Despite connecting to this machine it will not be connected to the internet, so you will not be able to access any external resources such as updates etc. In a later guide, I will show you how to do this safely. Find out more about my AWS certification or drop me a line on Twitter.