How do I use Session Manager to create port forwarding?

2 minute read
0

I want to use Session Manager, a capability of AWS Systems Manager, to create port forwarding.

Resolution

Create a port forwarding session for port 80, then load your website from the local host address on your local machine. It isn't necessary to open inbound port 80 on the managed instance in a security group or a network access control list (network ACL).

Prerequisites

Complete the following steps:

  1. Confirm that Systems Manager is managing your Amazon Elastic Compute Cloud (Amazon EC2) instance. For more information, see Managing EC2 instances with Systems Manager and Manually installing and uninstalling SSM Agent on EC2 instances for Linux.

  2. Install the Session Manager plugin for the AWS Command Line Interface (AWS CLI).
    Note: If you receive errors when you run AWS CLI commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

  3. Run the following commands to verify that the port in the target instance is in listening mode:

    Linux OS

    netstat -plant 

    Windows OS

    netstat -nb

Create a port forwarding session

Run the following commands. Be sure to replace the example values with your required values:

Linux or macOS

# aws ssm start-session \ 
 --target example-EC2-instance-ID \ 
 --document-name AWS-StartPortForwardingSession \  
  --parameters '{"portNumber":["80"], "localPortNumber":["example-local-port"]}'

Windows

> aws ssm start-session  
 --target "example-EC2-instance-ID" --document-name AWS-StartPortForwardingSession --parameters "portNumber"=["80"],"localPortNumber"=["example-local-port"]

Open the browser on your local machine, and then load http://localhost:example-local-port to view the web server content from your Amazon EC2 instance.

AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago
2 Comments

--document-name AWS-StartPortForwardingSession always gives error as no session policy allows the ssm:StartSession but I can connect it directly without portforwarding...

replied a month ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a month ago