How can I use an SSH tunnel through AWS Systems Manager to access my private VPC resources?
I want to use an SSH tunnel through AWS Systems Manager to access my private VPC resources. How can I do this?
"I want to use an SSH tunnel through AWS Systems Manager to access my private VPC resources. How can I do this?Short descriptionTo create an SSH tunnel, you can use Session Manager, a capability of AWS Systems Manager that lets you use port forwarding for remote hosts. This feature is supported on SSM Agent versions 3.1.1374.0 and later. Port forwarding is an alternative to the steps below. For more information about remote host port forwarding, see Start a session.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's 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.PrerequisitesComplete the Session Manager prerequisitesInstall the Session Manager plugin for the AWS Command Line Interface (AWS CLI)Allow 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.    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.ResolutionTo start the SSH tunnel using Session Manager, follow these steps: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.    To start the SSH tunnel, run the following command:ssh -i /path/my-key-pair.pem username@instance-id -L localport:targethost:destport2.    To test access to the tunnel on the target port created in step 1, run the following command:telnet 127.0.0.1 localportIn the preceding example, 127.0.0.1 and localport translate to access targethost:destport.Example ConfigurationsScenario 1Create 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 usedinstance1: 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-0123456789abcdefainstance2: An EC2 instance running MySQL Database on the default port 3306.    Hostname = ec2-198-51-100-2.compute-1.amazonaws.comInstructions1.    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:3306Note: In the preceding 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 passwordNote: 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 preceding example, instance2 must allow port 3306 access from instance1.Scenario 2Create three tunnels over a single SSH connection from your local machine to:Connect to the SSH port in instance1Access a MySQL database in RDS instanceAccess a webserver in instance3Resources usedinstance1: 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-0123456789abcdefaRDS instance: A MySQL RDS instance located in a private subnet.    Hostname = DBinstanceidentifier.abcdefg12345.region.rds.amazonaws.cominstance3: An EC2 instance located in a private subnet    Hostname = ec2-198-51-100-3.compute-3.amazonaws.comInstructions1.    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:DBinstanceidentifier.abcdefg12345.region.rds.amazonaws.com:3306 -L 9091:ec2-198-51-100-3.compute-1.amazonaws.com:80Note: In the preceding 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 80803.    Access the database on RDS instance. The local port 9090 tunnels to port 3306 on RDS instance. You can use MySQL workbench, which allows you to access the DB server using the GUI, with 127.0.0.1 as hostname and 9090 as port. Or, run the following command in the shell command prompt:mysql -u user -h 127.0.0.1 -P 9090 -p password4.    From the local machine, to access the website on instance3, open the browser and navigate to the website.http://127.0.0.1:9091Important: Any security groups, network ACL, security rules, or third-party security software that exist on RDS instance and instance3 must allow traffic from instance1. In the preceding example, instance3 must allow port 80 access from instance1.Related informationAutomated configuration of Session Manager without an internet gatewaysession-manager-without-igwSecurely connect to an Amazon RDS or Amazon EC2 database instance remotely with your preferred GUIFollow"