Sub topic 10
Granting users permission to start sessions
This chapter assumes you know how IAM identity policies, ARNs, and policy evaluation work. It also assumes an instance that already meets the Session Manager requirements from the previous chapter: SSM Agent, an instance profile, and a network path. Those settings make the instance able to talk to Systems Manager. They do not decide who may open a shell on it. That decision is made by the IAM identity of the person calling StartSession.
Two separate permission sets
Session Manager checks two identities.
| Side | Identity | What it permits |
|---|---|---|
| Instance | Instance profile role | The agent registers, receives messages, and sends output (for example `AmazonSSMManagedInstanceCore`) |
| Person | IAM user, or a role assumed by a person or federated identity | Calling `ssm:StartSession` against specific instances |
Fixing one side never fixes the other. An instance that shows Online in describe-instance-information still rejects a user whose policy lacks ssm:StartSession. A user with full permissions still cannot reach an instance whose agent is not registered.
What `StartSession` is authorized against
A single ssm:StartSession call is authorized against two resource types, and your policy must cover both:
- The target instance, as an EC2 instance ARN (or a managed node ARN for non-EC2 machines).
- The Session document that defines what the session does.
The document decides the session type, so allowing or omitting it is how you control what a user can do.
| Document | Session type |
|---|---|
| `SSM-SessionManagerRunShell` | Interactive shell, the default |
| `AWS-StartPortForwardingSession` | Port forwarding to the instance |
| `AWS-StartSSHSession` | SSH tunnelled through Session Manager |
A least-privilege starting policy
The policy below lets a user start a shell on one named instance. It lets them end or resume only sessions whose ID begins with their own user name.
{
"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}-*"
}
]
}The second statement works because Session Manager builds session IDs from the caller's name plus a random suffix, such as alice-0a1b2c3d4e5f6a7b8. The policy variable ${aws:username} expands to the caller's IAM user name, so a user cannot terminate a colleague's session.
aws:username is only populated for IAM users. For people who sign in through an assumed role or federation, the variable is empty and the pattern will not match. Use ${aws:userid} instead and test the result with a real session.
Without the second statement, a user can start sessions but gets AccessDenied when trying to end them from the console or CLI. They would have to wait for the idle timeout.
Attaching the policy
Attach policies to a group or role rather than to individual users. Save the JSON as session-policy.json, then create and attach it:
aws iam create-policy \
--policy-name EC2ShellWeb1 \
--policy-document file://session-policy.json
aws iam attach-group-policy \
--group-name web-operators \
--policy-arn arn:aws:iam::111122223333:policy/EC2ShellWeb1Any user in web-operators can now run aws ssm start-session --target i-0123456789abcdef0.
Scaling with tags
Listing instance IDs does not scale. The condition key ssm:resourceTag/<tag-key> lets one policy cover every instance that carries a given tag, including instances launched later.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": "arn:aws:ec2:*:111122223333:instance/*",
"Condition": {
"StringEquals": { "ssm:resourceTag/Environment": "dev" }
}
},
{
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": "arn:aws:ssm:*:*:document/SSM-SessionManagerRunShell"
}
]
}Developers with this policy can open shells on instances tagged Environment=dev and nowhere else. The document statement has no condition because the tag lives on the instance, not on the document.
Tag-based access control depends on who may edit tags. A user who holds ec2:CreateTags on an instance can retag a production instance as dev and then connect to it. Either withhold tagging permissions from session users or deny changes to the Environment key.
Controlling session types through documents
The shell document alone gives a user a shell and nothing else. To allow tunnelling, add the other documents explicitly:
{
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": [
"arn:aws:ssm:*::document/AWS-StartPortForwardingSession",
"arn:aws:ssm:*::document/AWS-StartSSHSession"
]
}These are AWS-owned documents, so the ARN has an empty account field. Place this statement beside a statement that allows the instance ARNs, since the instance must also be permitted. You can grant port forwarding to a database administrator without ever giving them a shell, or the reverse.
This gives you a control that SSH lacks. With SSH, anyone who holds a valid key can request port forwarding, a shell, or a file copy. Here, each capability is a separate permission.
Extra permissions for the console
The aws ssm start-session command needs only ssm:StartSession. The console must also list instances and show their connection status, so console users need read permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:DescribeSessions",
"ssm:GetConnectionStatus",
"ssm:DescribeInstanceProperties",
"ec2:DescribeInstances"
],
"Resource": "*"
}
]
}These actions do not support resource-level restrictions in the way StartSession does, so "*" is expected. If your Session Manager preferences encrypt session data with a KMS key, users also need kms:GenerateDataKey on that key.
How the decision is made
IAM evaluates the call as usual. An explicit deny anywhere wins. Otherwise some policy must allow the action on every resource involved. Permissions boundaries and service control policies (SCPs) in AWS Organizations can still block a call that your identity policy allows.
Notice the two different failures at the end. AccessDeniedException means the identity side is wrong. TargetNotConnected means IAM was satisfied but the instance side is wrong, so go back to the agent, instance profile, and network path.
Testing a policy before users do
The IAM policy simulator evaluates each resource separately, so check both ARNs:
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:user/alice \
--action-names ssm:StartSession \
--resource-arns \
arn:aws:ec2:us-east-1:111122223333:instance/i-0123456789abcdef0 \
arn:aws:ssm:us-east-1::document/SSM-SessionManagerRunShell \
--query "EvaluationResults[].[EvalResourceName,EvalDecision]" \
--output textEach line should end in allowed. A line ending in implicitDeny names the resource your policy is missing. Simulation does not model every condition context, so finish with a real start-session attempt as the target user.
Common mistakes
- Allowing only the instance ARN. The call fails because the document is a second resource. The error message names the document ARN.
- Wrong Region in the ARN. Instance ARNs include the Region. A policy written for
us-east-1does not cover an instance ineu-west-1. - Using
ssm:*for convenience. It also grants commands on the instance through Run Command and access to parameters, which is far more than a shell. - Mixing up the two sides. Adding
ssm:StartSessionto the instance profile does nothing for your users. The profile belongs to the instance.
Exercise
A developer's policy allows ssm:StartSession on arn:aws:ec2:us-east-1:111122223333:instance/i-0123456789abcdef0 and on the SSM-SessionManagerRunShell document. Running aws ssm start-session --target i-0123456789abcdef0 --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["5432"],"localPortNumber":["5432"]}' fails with AccessDeniedException. Why, and what is the smallest change that fixes it?
Show answer
Each session type is its own document resource, and the policy lists only SSM-SessionManagerRunShell. The call therefore lacks an allow on the AWS-StartPortForwardingSession document. Add arn:aws:ssm:us-east-1::document/AWS-StartPortForwardingSession (or the wildcard-Region form) to the Resource list of a StartSession statement. The instance ARN is already allowed, so nothing else changes. The developer still has no right to open other session types.
Exercise
A policy ends sessions with Resource "arn:aws:ssm:::session/${aws:username}-*". Users who sign in through a federated role report that they cannot terminate their own sessions. What is the likely cause?
Show answer
aws:username is only set for IAM users, so for assumed-role or federated callers the variable has no value and the pattern matches no session ID. Use a variable that is populated for roles, such as ${aws:userid}, then confirm with a real session.