Sub topic 11
Starting a session from the console and the AWS CLI
This chapter shows how to start a Session Manager shell from the AWS console and from the AWS CLI, and how to list and end sessions. It also covers the errors you are most likely to hit.
It assumes the instance already has a running SSM Agent, an instance profile that allows Systems Manager, and a network path to the Systems Manager endpoints. It also assumes your IAM identity can call ssm:StartSession on the instance (see the previous two chapters). You also need the AWS CLI configured with credentials.
What happens when you start a session
Starting a session is a short exchange between your client, the Systems Manager service, and the SSM Agent on the instance. The agent makes only outbound connections, which is why the instance needs no inbound port.
The CLI itself only makes the StartSession API call. The separate Session Manager plugin opens the WebSocket and handles your terminal. If the plugin is missing, the call to the API succeeds but the shell never appears.
Starting a session from the console
You can reach Session Manager from two places in the console.
- EC2 console. Select the instance, choose Connect, open the Session Manager tab, and choose Connect. A browser terminal opens.
- Systems Manager console. Open Session Manager, choose Start session, select a target from the list of managed instances, and choose Start session.
The Systems Manager list shows only instances that are registered as managed instances. If your instance is missing, the agent is not registered. The usual causes are a missing instance profile, no network path to the endpoints, or an agent that is not running. The Session Manager tab in the EC2 console shows a message about unmet prerequisites in the same situation.
The browser terminal needs no local software. It is convenient for one-off work, but it gives you no port forwarding and no SSH tunnel. For those you need the CLI.
Starting a session with the AWS CLI
Install the plugin
Install the Session Manager plugin on your workstation by following the AWS documentation for your operating system. Then confirm it is on your path:
session-manager-plugin --versionIf the command is not found, aws ssm start-session fails with an error saying the plugin was not found.
Confirm the instance is ready
List the instances Systems Manager can reach. PingStatus must be Online.
aws ssm describe-instance-information \
--query "InstanceInformationList[].[InstanceId,PingStatus]" \
--output textAn instance that was stopped or lost network connectivity shows ConnectionLost. An instance that was never registered does not appear at all.
Open a shell
Pass the instance ID as the target:
aws ssm start-session --target i-0123456789abcdef0The CLI prints Starting session with SessionId: ... and then a shell prompt. If you do not pass --document-name, Session Manager uses the default shell document, SSM-SessionManagerRunShell. Type exit or press Ctrl+D to end the session.
Two details to know:
- On Linux the session runs as
ssm-user, notec2-user. The agent creates this account the first time a session starts and grants it passwordlesssudothrough/etc/sudoers.d/ssm-agent-users. Runsudo -ifor a root shell, orsudo su - ec2-userto work as the default login user. - The session starts in
/usr/binon Linux, not in a home directory. Runcd ~first if you want your home directory.
The session uses your CLI profile and Region. If the instance is in a different Region than your default, add --region us-east-1 (or whichever Region applies), or set AWS_REGION. A wrong Region produces TargetNotConnected, not "wrong Region", which makes this a common source of confusion.
Run a single command
Use the AWS-StartInteractiveCommand document to run one command and return to your local prompt when it finishes:
aws ssm start-session \
--target i-0123456789abcdef0 \
--document-name AWS-StartInteractiveCommand \
--parameters '{"command":["uptime"]}'You can restrict a user to only this kind of document through IAM, as shown in the policy later in this chapter. For automation that needs a command's exit status and output stored, aws ssm send-command (Run Command) is a better fit than a session. A session is intended for interactive use.
Listing and ending sessions
Each session has an ID that starts with the IAM user name or the role session name, for example alice-0a1b2c3d4e5f67890. That prefix is what the ${aws:username}-* pattern in the IAM policy from the previous chapter matches.
List sessions that are active for your account:
aws ssm describe-sessions --state ActiveEach entry shows the target, the owner, the document, and the start time. To end one:
aws ssm terminate-session --session-id alice-0a1b2c3d4e5f67890Use --state History to see ended sessions from the last 30 days. For a longer record, set up logging, which is covered in a later chapter.
If your terminal loses its connection, the session stays active until it reaches the idle timeout. You can attach again with aws ssm resume-session, but the plugin usually reconnects on its own after a short network drop. Terminating the session from a second terminal is the quickest way to clear one that you can no longer see.
Timeouts and preferences
Session behavior is controlled by account-wide preferences, stored in a document named SSM-SessionManagerRunShell in each Region. You change them in the console under Session Manager > Preferences. Settings that affect everyday use:
| Setting | Default | Effect |
|---|---|---|
| Idle session timeout | 20 minutes | Ends a session with no input. You can set 1 to 60 minutes. |
| Maximum session duration | Not set | Ends any session after the given time, whether or not it is active. |
| Run As | Off | Starts the shell as a named OS user instead of `ssm-user`. |
| Logging | Off | Sends session output to CloudWatch Logs or S3. |
Preferences are per account and per Region, so a change in us-east-1 does not affect eu-west-1.
Restricting which documents a user can start
The ssm:StartSession permission is checked against both the instance and the session document. Allowing only SSM-SessionManagerRunShell as in the policy from the previous chapter gives a plain shell. To also allow command, port forwarding, or SSH sessions, add those document ARNs to the Resource list. Here the Region is us-east-1 and the account ID is 111122223333:
{
"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/SSM-SessionManagerRunShell",
"arn:aws:ssm:us-east-1::document/AWS-StartPortForwardingSession"
]
}
]
}Without the document ARN you get AccessDeniedException even when the instance ARN is allowed.
Common errors
| Message or symptom | Likely cause | What to check |
|---|---|---|
| `TargetNotConnected` | Agent not registered or not reachable, or wrong Region | `describe-instance-information` shows `Online`; the Region of your command; instance profile; endpoint access |
| `SessionManagerPlugin is not found` | Plugin not installed or not on your path | `session-manager-plugin --version` |
| `AccessDeniedException` on `StartSession` | IAM policy lacks the instance or document ARN | Both resources in the policy; any deny statements or permission boundary |
| Instance missing from the console list | No instance profile, or agent stopped | Role attached to the instance; `systemctl status amazon-ssm-agent` |
| Session closes after a quiet period | Idle timeout reached | Preferences; run a command such as `top` to keep the session active if you need to |
A useful habit is to work from the instance outward. Confirm that describe-instance-information reports Online. If it does, the instance side is correct and any remaining failure is IAM or your local setup. If it does not, no IAM change will help.
Exercise
Your teammate runs aws ssm start-session --target i-0123456789abcdef0 and gets TargetNotConnected, although the instance is running and the same command works for you. Name two things to check, and the command that tells you whether the instance side is healthy.
Show answer
First, check the Region: the teammate's CLI profile may default to a different Region from the instance's, and the error is the same as for an unregistered instance. Second, check the instance's registration: the agent, instance profile, or network path may be broken. Run aws ssm describe-instance-information --query "InstanceInformationList[].[InstanceId,PingStatus]" --output text with the correct Region. If the instance appears as Online, the instance side is fine and the problem is the teammate's Region or setup. If it is missing or ConnectionLost, fix the instance.
Exercise
After you connect with Session Manager, whoami prints ssm-user, but your application files are owned by ec2-user. How do you work as ec2-user, and what does the shell's starting directory tell you about where you landed?
Show answer
Run sudo su - ec2-user (or sudo -i for root); ssm-user has passwordless sudo. The session starts in /usr/bin on Linux rather than in a home directory, so run cd ~ or switch users with a login shell to land in the right home directory.