Sub topic 13
Forwarding a remote port to your machine
This chapter assumes you can start a Session Manager shell with aws ssm start-session, and that the instance already meets the SSM Agent, instance profile, and network requirements. It also assumes you have the Session Manager plugin for the AWS CLI installed.
What port forwarding does
Port forwarding makes a service on the instance appear as a port on your own machine. You open a session, and while it stays open, traffic sent to a local port travels through Session Manager to a port on the instance. The instance needs no inbound security group rule for that port. The traffic rides on the same outbound HTTPS connection that the SSM Agent already holds open.
Typical uses are reaching a web admin page that listens only on localhost, or connecting a local database client to a database port that is closed to the network.
Start a forwarding session
Use the AWS-StartPortForwardingSession document and pass the two ports as parameters.
aws ssm start-session \
--target i-0123456789abcdef0 \
--document-name AWS-StartPortForwardingSession \
--parameters '{"portNumber":["80"],"localPortNumber":["8080"]}'portNumberis the port on the instance.localPortNumberis the port opened on your machine.
The command prints a line saying it is waiting for connections and then keeps running. While it runs, open http://localhost:8080 in a browser. Press Ctrl+C to end the session and close the local port.
If you omit localPortNumber, the plugin chooses a free local port and prints it. Both values are strings inside lists, even though they are numbers.
Permissions you need
Starting a forwarding session needs ssm:StartSession on the instance, as in the policy from the earlier IAM chapter. It also needs permission on the document you name. The policy shown earlier allows only SSM-SessionManagerRunShell, so it would reject this command. Add the port forwarding document to the Resource list:
arn:aws:ssm:us-east-1::document/AWS-StartPortForwardingSessionThis gives you a useful control point. You can allow shell sessions for some users and port forwarding for others by choosing which documents each policy lists.
Common errors
| Symptom | Likely cause |
|---|---|
| `AccessDeniedException` on `StartSession` | The IAM policy does not list the `AWS-StartPortForwardingSession` document. |
| `SessionManagerPlugin is not found` | The plugin is not installed or is not on your `PATH`. |
| Local port already in use | Another program holds `localPortNumber`. Pick a different one. |
| Browser shows connection reset | Nothing listens on the instance port, or the service listens on a different interface or port. |
| Instance not listed as a target | The agent is offline. Check `aws ssm describe-instance-information`. |
Ports below 1024 on your own machine usually need administrator rights, so use a high local port such as 8080.
Forwarding versus SSH over Session Manager
Port forwarding reaches one named port. SSH over Session Manager gives you a full SSH connection, which can in turn carry many forwards with ssh -L. Choose the plain forwarding document when you need a single port and do not want to manage SSH keys.
Exercise
You run aws ssm start-session with AWS-StartPortForwardingSession and the parameters {"portNumber":["5432"],"localPortNumber":["15432"]}. Which port do you connect your local database client to, and what happens when you press Ctrl+C?
Show answer
Connect the client to localhost:15432. Session Manager carries that traffic to port 5432 on the instance. Pressing Ctrl+C ends the session and closes the local port, so the client can no longer connect.