Sub topic 10
Granting users permission to start sessions
This chapter assumes you know basic IAM (users, roles, policies) and that the AWS CLI is configured. Session Manager has two sets of permissions. The instance needs an instance profile so SSM Agent can talk to Systems Manager. The person connecting needs IAM permissions to start a session. This chapter covers the second set.
What the user needs
Session Manager checks the caller's IAM identity, not an SSH key. To open a shell, the identity needs ssm:StartSession on two resources:
- The target instance, as an EC2 instance ARN.
- The session document, which defines what kind of session starts. The default shell document is
SSM-SessionManagerRunShell.
Two more actions let users manage sessions they already started: ssm:TerminateSession ends a session, and ssm:ResumeSession reconnects after a dropped network link.
A least-privilege policy
The following policy allows a shell on one named instance. Users can end or resume only sessions that they started.
{
"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"
]
},
{
"Effect": "Allow",
"Action": ["ssm:TerminateSession", "ssm:ResumeSession"],
"Resource": "arn:aws:ssm:*:*:session/${aws:username}-*"
}
]
}Session IDs begin with the name of the user who started the session, for example alice-0a1b2c3d4e5f67890. The ${aws:username}-* pattern uses that prefix, so Alice can end her own sessions and not Bob's. Replace the account ID, Region, and instance ID with your own values.
Scoping access with tags
Listing instance IDs does not scale. A common alternative is a tag condition on the instance resource. This policy allows sessions only on instances tagged Environment=dev.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": "arn:aws:ec2:*:*:instance/*",
"Condition": {
"StringEquals": {
"ssm:resourceTag/Environment": "dev"
}
}
},
{
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": "arn:aws:ssm:*:*:document/SSM-SessionManagerRunShell"
}
]
}Moving an instance from dev to prod then removes access without editing the policy.
Other documents need their own permission
Port forwarding and SSH-over-SSM use different documents. If a user needs them, add the document ARN to the ssm:StartSession resources:
arn:aws:ssm:us-east-1::document/AWS-StartPortForwardingSessionarn:aws:ssm:us-east-1::document/AWS-StartSSHSession
Leaving these out is a useful control. A user with only the shell document cannot tunnel traffic to other ports.
Attach and test
Attach the policy to a group or role, then confirm the user can start a session:
aws ssm start-session --target i-0123456789abcdef0If the policy is missing a resource, the CLI returns an AccessDeniedException that names the action and the ARN that was denied. Add that ARN to the policy.
Common errors
| Symptom | Likely cause |
|---|---|
| `AccessDeniedException` on `ssm:StartSession` | The instance or document ARN is not in the policy `Resource` list. |
| Access works for one instance and fails for another | The tag condition does not match, or the tag key is misspelled. |
| User cannot end a session | The session ID prefix does not match `${aws:username}`. |
| Session starts and then fails to connect | An instance-side problem, such as no instance profile or no network path to Systems Manager. |
Exercise
A developer can open a shell with aws ssm start-session --target i-0123456789abcdef0, but a port forwarding session using AWS-StartPortForwardingSession fails with AccessDeniedException. What change fixes it?
Show answer
Add the document ARN arn:aws:ssm:us-east-1::document/AWS-StartPortForwardingSession to the Resource list of the ssm:StartSession statement. The existing policy only allows the SSM-SessionManagerRunShell document.