Sub topic 13
Forwarding a remote port to your machine
This chapter assumes you can already start a Session Manager shell. That means the instance has SSM Agent, an instance profile, and a network path to Systems Manager, and your IAM identity can call ssm:StartSession. It also assumes the Session Manager plugin for the AWS CLI is installed. If aws ssm start-session --target i-0123456789abcdef0 gives you a shell, you are ready.
What port forwarding does
A shell session gives you a terminal on the instance. Sometimes you need something else: a web admin page, a database, or a debugging port that listens only on the instance's loopback address. Port forwarding makes a TCP port on the instance appear as a port on your own machine.
With SSH you would use ssh -L. That needs port 22 open and a key. Session Manager does the same job without an inbound port. The AWS CLI opens a listener on your machine and carries the TCP traffic through the Session Manager channel to the instance.
aws ssm start-session \
--target i-0123456789abcdef0 \
--document-name AWS-StartPortForwardingSession \
--parameters '{"portNumber":["80"],"localPortNumber":["8080"]}'While this command runs, http://localhost:8080 on your machine reaches port 80 on the instance. Press Ctrl+C to end the session and close the local listener.
The command prints no shell prompt. It prints a line like Waiting for connections... and stays in the foreground. Run it in a spare terminal, or in the background, and use the forwarded port from another terminal.
How the traffic flows
The CLI does not connect to the instance directly. Your traffic goes to the Systems Manager service over HTTPS. SSM Agent on the instance holds an outbound connection to the same service, and the service joins the two.
Three consequences follow from this path:
- The instance's security group needs no inbound rule for the forwarded port. The agent connects to the service on the instance's behalf, and the final hop to the web server stays on the instance.
- The local listener binds to
localhost. Other machines on your network cannot use your forwarded port unless you take extra steps, and you should not. - The target port is reached from the instance itself. A service that listens only on
127.0.0.1on the instance still works.
Choosing the ports
The --parameters value is JSON. Each value is a list of strings, even for a single number, so "80" is correct and 80 is not.
| Parameter | Meaning |
|---|---|
| `portNumber` | Port on the instance that traffic is sent to |
| `localPortNumber` | Port the CLI listens on, on your machine |
Pick a localPortNumber that nothing else is using. On Linux and macOS, ports below 1024 normally need elevated privileges, so use a high port such as 8080 or 15432. If the port is already taken, the command fails with an "address already in use" error. Choose another port, or stop whatever holds it.
You can run several forwarding sessions at once, each in its own terminal with its own local port. Each session has its own entry in Systems Manager.
Reaching a host other than the instance
The AWS-StartPortForwardingSession document only reaches ports on the instance itself. To reach another host through the instance, such as an RDS database in a private subnet, use AWS-StartPortForwardingSessionToRemoteHost. The instance then acts as a relay.
aws ssm start-session \
--target i-0123456789abcdef0 \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters '{"host":["mydb.cluster-abc123.us-east-1.rds.amazonaws.com"],"portNumber":["5432"],"localPortNumber":["15432"]}'With the session open, a client on your machine connects to localhost:15432:
psql "host=localhost port=15432 dbname=appdb user=appuser"Several points matter here:
- The instance must be able to resolve and reach the host. Check DNS, route tables, and network ACLs between the instance and the database.
- The database's security group must allow inbound traffic on the database port from the instance, usually by referencing the instance's security group.
- Your database client sees
localhostas the server name. TLS certificate checks that match the hostname can fail. Either disable hostname verification for this connection or point the client at the real certificate name with a local override such as an/etc/hostsentry.
Allowing port forwarding in IAM
Starting any session is controlled by ssm:StartSession. The document you name in --document-name is a resource in that permission, so you can allow shells and forbid forwarding, or the reverse. The policy from the previous chapter allowed only SSM-SessionManagerRunShell. A user with only that policy will get an access denied error when requesting a forwarding document.
To allow forwarding to one instance, add the forwarding document to the resource list:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": [
"arn:aws:ec2:us-east-1:111122223333:instance/i-0123456789abcdef0",
"arn:aws:ssm:us-east-1::document/AWS-StartPortForwardingSession"
]
},
{
"Effect": "Allow",
"Action": ["ssm:TerminateSession", "ssm:ResumeSession"],
"Resource": "arn:aws:ssm:*:*:session/${aws:username}-*"
}
]
}Use the document ARN for AWS-StartPortForwardingSessionToRemoteHost if the user needs relay access. Grant it separately, because it widens what the instance can reach.
Limits and behavior to expect
- Idle timeout. Sessions end after a period without activity. The default is 20 minutes and can be changed in Session Manager preferences. A long-running tool that stays quiet may be cut off, so rerun the command when that happens.
- TCP only. Forwarding carries TCP streams. UDP services such as DNS cannot be forwarded this way.
- One direction. Traffic goes from your machine to the instance. Session Manager does not give you the equivalent of
ssh -R, where the instance listens and sends traffic back to you. - Logging. Port forwarding sessions are recorded in CloudTrail as
StartSessionevents, with the document name and target. The data inside the stream is not logged, because the session carries raw TCP and not terminal input and output. Session logging is covered in a later chapter. - Plugin and agent versions. Port forwarding needs a reasonably current Session Manager plugin and SSM Agent. If forwarding fails with unclear errors while shell sessions work, update both.
Troubleshooting by symptom
| Symptom | Likely cause | What to check |
|---|---|---|
| `AccessDeniedException` on start | IAM does not allow this document or instance | The `Resource` list in the policy |
| `TargetNotConnected` | Agent offline or no network path to Systems Manager | `aws ssm describe-instance-information` and `PingStatus` |
| Command runs, browser hangs | Nothing listens on the instance port, or the service is stopped | On the instance, `ss -tlnp` and the service status |
| `address already in use` | Local port is taken | Choose another `localPortNumber` |
| Remote-host session connects, then clients time out | Instance cannot reach the host | Security groups, routes, and DNS between the instance and the host |
| Session drops after a quiet period | Idle timeout | Session Manager preferences |
A quick sanity check is to open a normal shell session first and test the target from the instance itself with curl -I http://localhost:80 or a database client. If the service does not answer from inside the instance, forwarding cannot help.
Exercises
Exercise
A web admin interface listens on port 3000 on an instance, bound to 127.0.0.1. Write the command that lets you open it at http://localhost:9000 on your machine, and say why no security group change is needed.
Show answer
aws ssm start-session --target i-0123456789abcdef0 --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["3000"],"localPortNumber":["9000"]}'. No inbound rule is needed because SSM Agent keeps an outbound connection to Systems Manager and then connects to the service from inside the instance, so the traffic never arrives through the security group. A service bound to 127.0.0.1 still works for the same reason.
Exercise
A user can start a shell on an instance but gets AccessDeniedException when running AWS-StartPortForwardingSessionToRemoteHost against it. What do you change in their IAM policy?
Show answer
Add the document ARN arn:aws:ssm:us-east-1::document/AWS-StartPortForwardingSessionToRemoteHost (with the correct region) to the Resource list of the ssm:StartSession statement, alongside the instance ARN. The existing policy only names the shell document, and the document is a separate resource in the permission check.