Sub topic 12
Role chaining and its limits
This chapter assumes you can already write a trust policy and a caller policy, and that you can assume a role with aws sts assume-role or a CLI profile. Here you extend that to a second hop. You will see what role chaining is, how to set it up, and which limits apply.
What role chaining is
Role chaining means using the temporary credentials from one assumed role to assume a second role. The first assumption starts from a long-term identity, such as an IAM user. The second assumption starts from the session you already hold.
A common layout is a hub account that holds a role everyone enters first. From there, people or automation move into spoke accounts that hold the real permissions.
Each hop is an ordinary AssumeRole call, so each hop needs the same two permissions as before. The difference is who the caller is. In the second hop the caller is the HubRole session, not your user.
Permissions for each hop
Three policies are involved in the example above.
- HubRole's trust policy in account 222222222222 trusts the user in account 111111111111. This is the pattern from the earlier chapters.
- HubRole's identity policy lets it call
sts:AssumeRoleon the next role. - DataReader's trust policy in account 333333333333 trusts HubRole.
The identity policy attached to HubRole:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::333333333333:role/DataReader"
}
]
}The trust policy on DataReader:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222222222222:role/HubRole" },
"Action": "sts:AssumeRole"
}
]
}Name the role ARN in the Principal element. Do not try to write an assumed-role session ARN such as arn:aws:sts::222222222222:assumed-role/HubRole/some-session. IAM does not accept session ARNs as principals in a trust policy. A role ARN matches every session of that role.
If you trust the account root (arn:aws:iam::222222222222:root) instead, any principal in account 222222222222 that also has sts:AssumeRole permission can assume DataReader. Naming the exact role is tighter and is usually the better choice for a chained hop.
Doing it with the CLI
The manual way is to run two assume-role calls and pass the first set of credentials to the second call through environment variables.
aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/HubRole \
--role-session-name hub-sessionCopy the AccessKeyId, SecretAccessKey and SessionToken from the response into environment variables:
export AWS_ACCESS_KEY_ID="ASIAEXAMPLEHUB"
export AWS_SECRET_ACCESS_KEY="example-hub-secret"
export AWS_SESSION_TOKEN="example-hub-session-token"
aws sts assume-role \
--role-arn arn:aws:iam::333333333333:role/DataReader \
--role-session-name data-sessionThe values above are placeholders. Use the ones returned by your own first call. The second call is signed with the HubRole session, which is why STS evaluates DataReader's trust policy against HubRole.
The easier way is to chain profiles. A profile's source_profile can point to another profile that itself has a role_arn. The CLI resolves the chain for you.
[profile hub]
role_arn = arn:aws:iam::222222222222:role/HubRole
source_profile = default
region = us-east-1
[profile data]
role_arn = arn:aws:iam::333333333333:role/DataReader
source_profile = hub
region = us-east-1Check the result:
aws sts get-caller-identity --profile dataThe returned ARN should look like arn:aws:sts::333333333333:assumed-role/DataReader/.... The CLI makes both AssumeRole calls as needed and caches the credentials.
The limits of role chaining
One-hour session cap
When you assume a role with credentials that came from another assumed role, the session lasts at most one hour. This holds even if the target role's maximum session duration is set to 12 hours. You cannot raise it.
If you ask for more, STS rejects the call:
aws sts assume-role \
--role-arn arn:aws:iam::333333333333:role/DataReader \
--role-session-name long-session \
--duration-seconds 7200With chained credentials, this fails with a validation error saying the requested DurationSeconds exceeds the 1 hour session limit for roles assumed by role chaining. If you omit --duration-seconds, the default is one hour, so the call works.
The same call succeeds with a 7200-second duration if you make it from the original IAM user, provided the role's maximum session duration allows it. The cap comes from the chain, not from the role.
No renewal from the same session
Credentials from a chained call cannot be extended. When they expire you must assume the role again. For long-running jobs, this means your code must refresh credentials before they expire. The AWS SDKs and the CLI do this when you configure the chain as a profile. A script that copies keys into environment variables does not.
Every hop is an access decision
A chain is only as strong as its weakest link. Anyone who can assume HubRole and who may call sts:AssumeRole on DataReader can reach account 333333333333. If HubRole's identity policy uses "Resource": "*", it can reach every role whose trust policy accepts HubRole or the whole account. Scope the Resource to specific role ARNs on each hop.
What carries across hops
Some session attributes travel with the chain, and some do not.
| Attribute | Behavior in a chain |
|---|---|
| Permissions | Not inherited. Each role gets only its own permissions policy. The second session does not gain the first role's access. |
| Session policy | Applies only to the session it was passed to. Pass a new one on the next call if you want to restrict that hop. |
| Source identity | Once set, it persists through every later hop and cannot be changed. |
| Session tags | Carry forward only if marked transitive. |
| Role session name | Set freshly on each hop. |
Source identity is useful for audit trails. If the first call sets --source-identity alice, that value appears in CloudTrail for every later call in the chain. The trust policy of each chained role must allow sts:SetSourceIdentity for the value to pass through.
Self-assumption must be explicit
A role can assume itself, which is a way to refresh a session or to shorten its permissions with a session policy. This only works if the role's trust policy explicitly names the role's own ARN. Account-level trust through :root does not cover it.
Failure modes
Chained calls fail for the same reasons as single-hop calls, plus a few new ones.
- AccessDenied on the second call. Check that DataReader's trust policy names HubRole, not the user from the first hop. The user is no longer the caller.
- AccessDenied because HubRole lacks
sts:AssumeRole. HubRole's identity policy must allow the call. The permission that let the user assume HubRole does not carry over. - Duration validation error. A chained call asked for more than 3600 seconds.
- Stale credentials. A script that exported the first-hop keys keeps working only until HubRole's session expires, then fails on the next hop with an expired token error.
- Wrong profile order. If
source_profilepoints to a profile that does not exist, the CLI reports that the source profile could not be found. Circular references between profiles are also rejected.
Auditing a chain
CloudTrail records each AssumeRole call as its own event. For the second hop, the userIdentity field shows type: AssumedRole with HubRole's ARN and session name. The request parameters show the DataReader ARN. Later API calls made with the DataReader session show DataReader as the assumed role, and the session name you chose.
To follow a person through the chain, set a recognizable --role-session-name on each hop, or use a source identity. Without one of them, you can only link events by matching the access key ID from the first hop's response to the next call's signer.
When to chain and when not to
Chain when a hub role is a deliberate design choice, for example a central operations role that fans out to many accounts. Avoid it when a direct trust would do. Each extra hop adds a policy to maintain, a one-hour ceiling, and an extra place for a request to fail. If a job needs to run for hours, grant the first-hop principal direct trust on the target role instead.
Exercise
A CI job in account 111111111111 assumes HubRole in account 222222222222, then calls AssumeRole on DataReader in account 333333333333 with --duration-seconds 14400. DataReader's maximum session duration is set to 12 hours. The call fails. Why, and which two changes would let the job run for four hours?
Show answer
The second call uses credentials from an assumed role, so it is role chaining, and chained sessions are capped at one hour regardless of the target role's maximum session duration. Four hours is rejected. The fix is either to have the CI job's own principal (not the HubRole session) assume DataReader directly, which requires adding that principal to DataReader's trust policy and giving it sts:AssumeRole permission on that role. Or keep the chain, request one hour at most, and have the job re-assume the role before each expiry.