I want to use an SSH tunnel through AWS Systems Manager to access my private VPC resources. How can I do this?

Last updated: 2020-03-02

I want to use an SSH tunnel through AWS Systems Manager to access my private VPC resources. How can I do this?

Short Description

AWS Systems Manager Session Manager uses the Systems Manager infrastructure to create an SSH-like session with an instance. This capability means that Session Manager tunnels real SSH connections, allowing you to tunnel to another resource within your VPC directly from your local machine. A managed instance that you create acts as a bastion host, or gateway, to your AWS resources. The benefits of this configuration are:

Increased Security: This configuration works with only one Amazon Elastic Compute Cloud (Amazon EC2) instance (the bastion host) with outbound port 443 connecting to Systems Manager infrastructure. This allows you to use Session Manager without any inbound connections. The local resource needs to allow inbound traffic only from the instance acting as bastion host. Therefore, there is no need to open any inbound rule publicly.

Ease of use: You can access resources in your private VPC directly from your local machine.

Prerequisites

Complete the Session Manager Prerequisites

Install the Session Manager Plugin for the AWS CLI

Enable SSH Connections Through Session Manager

Note: You must have the following installed to use the SSH feature:

1.    AWS Systems Manager Agent (SSM Agent) v2.3.672.0 or newer.

2.    Session Manager Plugin v1.1.23 or newer on your local machine.

3.    AWS Command Line Interface (CLI) v1.16.12 or newer on your local machine.

Resolution

1.    Start the SSH tunnel using Session Manager.

ssh -i /path/my-key-pair.pem username@instance-id -L localport:targethost:destport

2.    Test access to the tunnel on the target port created in step 1.

telnet 127.0.0.1 localport

In the preceding example, 127.0.0.1 and localport translate to access targethost:destport.

Example Configurations

Scenario 1

Create a tunnel from your local machine to access a MySQL database running on a private EC2 instance using the SSM host as a bastion host.

Resources used

instance1: An EC2 instance acting as a bastion host and managed by AWS Systems Manager.

    Hostname = ec2-198-51-100-1.compute-1.amazonaws.com Instance id = i-0123456789abcdefa

instance2: An EC2 instance running MySQL Database on the default port 3306.

    Hostname = ec2-198-51-100-2.compute-1.amazonaws.com

Instructions

1.    From a local machine (for example, your laptop), run the SSH command to connect to instance1, using Session Manager-based SSH. This command establishes a tunnel to port 3306 on instance2, and presents it in your local machine on port 9090.

ssh -i /path/key-pair_instance1.pem username_of_instance1@i-0123456789abcdefa -L 9090:ec2-198-51-100-1.compute-1.amazonaws.com:3306

In the preceding example, port 9090 is an available port on the local machine.

2.    From the local machine, access the database using the available port used in step 1 (in this example, 9090)

mysql -u user -h 127.0.0.1:9090 -p password

Note: Make sure that any security groups, network ACL, security rules, or third-party security software that exist on instance2 allow traffic from instance1. In the preceding example, instance2 needs to allow port 3306 access from instance1.

Scenario 2

Create three tunnels over a single SSH connection from your local machine to:

  • connect to the SSH port in instance1
  • access a MySQL database in instance2
  • access a webserver in instance3

Resources used

instance1: An EC2 instance acting as a bastion host and managed by AWS Systems Manager.

    Hostname = ec2-198-51-100-1.compute-1.amazonaws.com Instance id = i-0123456789abcdefa

instance2: EC2 Database instance located in a private subnet.

    Hostname = ec2-198-51-100-2.compute-2.amazonaws.com

instance3: EC2 instance located in a private subnet

    Hostname = ec2-198-51-100-3.compute-3.amazonaws.com

Instructions

1.    Start the session with three tunnels, using the SSH command.

Note: There are three separate tunnel invocations in the command.

ssh -i /path/key-pair_instance1.pem username_of_instance1@i-0123456789abcdefa -L 8080:ec2-198-51-100-1.compute-1.amazonaws.com:22 -L 9090:ec2-198-51-100-2.compute-1.amazonaws.com:3306 -L 9091:ec2-198-51-100-3.compute-1.amazonaws.com:80

In the preceding example 8080, 9090 and 9091 are available ports on the local machine.

2.    Access SSH from the local machine to instance1. The local port 8080 is tunneled to the SSH port (22) on instance1. The key-pair and username are for the instance you are tunneling to (instance1 in this example).

ssh -i /path/key-pair_instance1.pem username_of_instance1@127.0.0.1 -p 8080

3.    Access the database on instance2. The local port 9090 is tunneled to port 3306 on instance2.

mysql -u user -h 127.0.0.1:9090 -p password

4.    From the local machine, access the website on instance3 by opening the browser and navigating to it.

http://127.0.0.1:9091

Note: Make sure that any security groups, network ACL, security rules, or third-party security software that exist on instance2 and instance3 allow traffic from instance1. In the preceding example, instance3 must allow port 80 access from instance1.


Did this article help you?

Anything we could improve?


Need more help?