Sub topic 8
How Session Manager works and why it needs no inbound port
This chapter explains the working model of AWS Systems Manager Session Manager. It covers what runs on the instance, which direction connections go, and which errors you see most often. It assumes you can use a Linux shell, know what an EC2 instance, a VPC subnet and a security group are, and understand IAM roles and policies. You also need the AWS CLI installed and configured with credentials.
The core idea: the instance calls out
With SSH, your client opens a connection to port 22 on the instance. The instance must be reachable from your network, and a security group rule must allow the traffic in.
Session Manager reverses the direction. A small program on the instance, the SSM Agent, opens an outbound HTTPS connection (port 443) to AWS Systems Manager endpoints and keeps it open. When you start a session, you talk to the Systems Manager service with your IAM credentials, and the service relays your keystrokes to the agent over the connection the agent already opened.
Because no connection is ever opened toward the instance, the instance needs:
- No inbound security group rules. The group can have zero inbound rules.
- No public IP address, and no bastion host.
- No SSH key pair, unless you choose to tunnel SSH through the session.
The instance does need a path to send HTTPS traffic out. That path can be an internet gateway, a NAT gateway, or VPC interface endpoints.
The components
Four pieces take part in every session. A failure in any one of them stops the session.
| Component | Where it runs | Role |
|---|---|---|
| SSM Agent | On the instance | Registers the instance, keeps the outbound channel open, and starts the shell process |
| Instance profile | Attached to the instance | Gives the agent IAM credentials so it can talk to Systems Manager |
| Systems Manager service | In AWS | Authorizes the request and relays traffic between you and the agent |
| Session Manager plugin | On your workstation | Lets the AWS CLI handle the interactive data stream (CLI use only) |
The next chapter covers how to prepare the instance. For now, the point is that the instance must be a registered managed node before any session can start.
Managed nodes and registration
An instance becomes a managed node when its SSM Agent starts, finds credentials, reaches a Systems Manager endpoint, and registers. The agent gets credentials from the instance profile through the instance metadata service. You can check registration from your workstation:
aws ssm describe-instance-information \
--query "InstanceInformationList[].[InstanceId,PingStatus]" \
--output textAn instance with PingStatus of Online is ready. An instance missing from the list, or showing ConnectionLost, cannot accept a session. A missing instance usually means the agent is not running, the role is missing, or the agent cannot reach the endpoints.
Many current Amazon Linux and Windows AMIs ship with the agent preinstalled and running. Not every AMI does, so confirm this for the image you use.
Two separate permission checks
Session Manager has two independent permission sets. Mixing them up is the most common source of confusion.
- The instance's permissions. The instance profile role lets the agent call Systems Manager. The AWS managed policy
AmazonSSMManagedInstanceCoregrants what is needed. - The user's permissions. Your IAM identity needs
ssm:StartSessionon the target instance and on a session document, for exampleSSM-SessionManagerRunShell. A later chapter covers writing this policy.
Having one without the other fails. A user with full permissions cannot reach an instance whose role is missing. An instance with a perfect role is unreachable by a user who lacks ssm:StartSession.
What happens when you start a session
Take the command:
aws ssm start-session --target i-0123456789abcdef0The steps are:
- The AWS CLI signs a
StartSessionAPI call with your credentials. - Systems Manager checks your IAM policy for
ssm:StartSessionon that instance and document. - Systems Manager confirms the instance is an online managed node.
- The service tells the agent, over its existing channel, to start the session.
- The agent opens a data channel back to the service and starts a shell process.
- The service returns a stream URL and a token to the CLI. The Session Manager plugin uses them to open a WebSocket connection and relay your terminal.
All of this uses HTTPS and is encrypted in transit with TLS. You can also encrypt session data with an AWS KMS key as an additional layer, which is set in Session Manager preferences.
Which user the shell runs as
SSH logs you in as a named user, such as ec2-user. Session Manager works differently. By default, the agent runs the shell as a dedicated local account called ssm-user, which the agent creates the first time a session starts. On Linux, this account is added to a sudoers file, so it has administrator rights through sudo.
You can change this in Session Manager preferences by setting a fixed operating system user to run the shell as. Preferences are stored in an account- and Region-level document, SSM-SessionManagerRunShell.
Session lifetime
Sessions end when you type exit, when you terminate them, or when they sit idle past a timeout. The default idle timeout is 20 minutes, and you can set it between 1 and 60 minutes in preferences. A long-running job started in a session stops with that session unless you run it under nohup, tmux, or a service manager.
Network paths for the agent
The agent needs to reach three Systems Manager endpoints in the instance's Region:
ssm.<region>.amazonaws.comssmmessages.<region>.amazonaws.comec2messages.<region>.amazonaws.com
The ssmmessages endpoint carries the Session Manager data channel. Console and CLI session traffic needs it specifically.
| Network design | How the agent reaches the endpoints | Notes |
|---|---|---|
| Public subnet with public IP | Internet gateway | Works, but the outbound path is only needed, not the public IP for inbound |
| Private subnet with NAT gateway | NAT gateway to the internet gateway | Common design, with NAT data processing charges |
| Private subnet with VPC endpoints | Interface endpoints inside the VPC | No internet path needed. Private DNS must be enabled |
For the endpoint design, create interface endpoints for ssm, ssmmessages and ec2messages. The endpoint security group must allow inbound HTTPS (443) from the instance. This is the one place inbound rules appear, and they protect the endpoint network interface, not the instance.
Common errors and how to read them
Most failures fall into a few patterns.
| Symptom | Likely cause | What to check |
|---|---|---|
| Instance not in `describe-instance-information` | No instance profile, agent not running, or no network path | Role attached, agent status, route to endpoints or VPC endpoints |
| `ConnectionLost` | Agent lost its channel, or instance stopped | Instance state, agent logs, network changes |
| `TargetNotConnected` on `start-session` | Instance is not an online managed node | Same checks as the first row |
| `AccessDeniedException` on `StartSession` | Your IAM policy lacks permission on that instance or document | Policy resource ARNs, permission boundaries, SCPs |
| `SessionManagerPlugin is not found` | Plugin not installed on your workstation | Install the Session Manager plugin |
| Role attached after boot but still not registered | Agent has not retried yet | Restart the agent, or wait a few minutes |
On the instance, you can check the agent service on Amazon Linux with systemd:
sudo systemctl status amazon-ssm-agent
sudo tail -n 50 /var/log/amazon/ssm/amazon-ssm-agent.logThe agent log states whether it found credentials and whether it reached each endpoint. Messages about failed endpoint connections point to routing, DNS or security group problems. Messages about missing credentials point to the instance profile.
Trade-offs to know up front
- No inbound exposure, but a new dependency. You depend on the agent, the role, and the Systems Manager service being available.
- IAM is the control point. You get central, revocable access without distributing keys. You also need to manage who may start sessions.
- Auditing is built in.
StartSessioncalls are recorded in CloudTrail, and session content can be logged to CloudWatch Logs or S3. A later chapter covers setup. - Different client behavior. The session is not a full SSH connection, so tools such as
scpwork only when you run SSH through the session.
Exercise
An instance in a private subnet has no inbound security group rules, no public IP, and an instance profile with AmazonSSMManagedInstanceCore. Users with ssm:StartSession permission get TargetNotConnected. The agent log shows repeated timeouts connecting to ssmmessages.us-east-1.amazonaws.com. Which direction does the failing connection go, and what are two ways to fix it?
Show answer
The failing connection is outbound, from the agent on the instance to the Systems Manager endpoint over HTTPS (443). Inbound rules are irrelevant to this design. Fix it by giving the subnet a route to a NAT gateway so the agent can reach the public endpoints, or by creating VPC interface endpoints for ssm, ssmmessages and ec2messages with private DNS enabled and a security group that allows inbound 443 from the instance.
Exercise
A user has AdministratorAccess in IAM, but start-session returns TargetNotConnected, while describe-instance-information does not list the instance. Is the problem the user's permissions or the instance's setup, and why?
Show answer
It is the instance's setup. TargetNotConnected means the instance is not an online managed node. The user's permissions are checked separately, and a failed check would return AccessDeniedException. Check that an instance profile with the needed permissions is attached, that the SSM Agent is running, and that the agent can reach the Systems Manager endpoints.