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

Last updated: 2021-02-16

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. Session Manager tunnels real SSH connections, allowing you to tunnel to another resource within your virtual private cloud (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 uses only one Amazon Elastic Compute Cloud (Amazon EC2) instance (the bastion host), and connects outbound port 443 to Systems Manager infrastructure. This allows you to use Session Manager without any inbound connections. The local resource must 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.

Note: For instructions to access your EC2 instances with a terminal or a single port forwarding using Systems Manager, see Setting up Session Manager.

Prerequisites

Complete the Session Manager prerequisites

Install the Session Manager plugin for the AWS Command Line Interface (AWS CLI)

Enable SSH connections through Session Manager and make sure that SSH connection requirements are met.

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 CLI v1.16.12 or newer on your local machine.

Resolution

Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.

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 an 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-2.compute-1.amazonaws.com:3306

Note: In the above example, port 9090 is available 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 -P 9090 -p password

Important: Any security groups, network access control list (network ACL), security rules, or third-party security software that exist on instance2 must allow traffic from instance1. In the above example, instance2 must 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: An EC2 database instance located in a private subnet.

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

instance3: An 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

Note: In the above example, ports 8080, 9090, and 9091 are available on the local machine.

2.    Access SSH from the local machine to instance1. The local port 8080 tunnels 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 tunnels to port 3306 on instance2.

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

4.    From the local machine, to access the website on instance3, open the browser and navigate to the website.

http://127.0.0.1:9091

Important: Any security groups, network ACL, security rules, or third-party security software that exist on instance2 and instance3 must allow traffic from instance1. In the above example, instance3 must allow port 80 access from instance1.


Did this article help?


Do you need billing or technical support?