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 have assumed a role with aws sts assume-role. Here you will see what happens when one assumed role assumes another.
What role chaining is
Role chaining means using the temporary credentials of one assumed role to assume a second role. The caller is no longer an IAM user or a human identity. It is a role session.
A common case is a hub-and-spoke layout. Engineers assume a role in a central account. That role then assumes roles in many workload accounts.
Permissions needed at each hop
Every hop needs the same two sides as a single cross-account call.
- The trust policy of the target role must name the calling role.
- The calling role needs a permissions policy that allows
sts:AssumeRoleon the target.
The trust policy on SpokeRole names HubRole as the principal:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222222222222:role/HubRole" },
"Action": "sts:AssumeRole"
}
]
}The permissions policy on HubRole lets it assume SpokeRole:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::333333333333:role/SpokeRole"
}
]
}Naming the role ARN as the principal is narrower than trusting the whole account root. Only that role can assume SpokeRole, and only if HubRole's own policy also allows it.
The one-hour limit
Role chaining limits the session to one hour. The limit applies even if SpokeRole has a maximum session duration of 12 hours. If you request a longer DurationSeconds in a chained call, the call fails.
The limit also affects long-running work:
- A job that chains through two roles must call AssumeRole again before the hour ends.
- You cannot extend a chained session. You can only create a new one.
- The first hop is not limited this way. A direct call from a user can use the role's configured maximum duration.
Chaining with CLI profiles
The CLI follows a chain when source_profile points at another role profile:
[profile hub]
role_arn = arn:aws:iam::222222222222:role/HubRole
source_profile = default
region = us-east-1
[profile spoke]
role_arn = arn:aws:iam::333333333333:role/SpokeRole
source_profile = hub
region = us-east-1Run a command with the second profile and the CLI makes both calls for you:
aws sts get-caller-identity --profile spokeThe returned ARN is an assumed-role ARN in account 333333333333. The CLI refreshes the credentials when they expire, so a long script keeps working.
Tracing who did what
In CloudTrail, the final session shows the role session name, not the original person. If several people share HubRole, you lose track of who acted. Two settings help:
- Give every person a unique
--role-session-namewhen they assume HubRole. - Set a source identity with
--source-identity. It stays the same through later role chaining and appears in CloudTrail.
Letting a role set a source identity requires the sts:SetSourceIdentity permission in the target role's trust policy.
Common mistakes
- Expecting more than one hour. A chained session never lasts longer than 3600 seconds.
- Only fixing one side. If either the trust policy or the calling role's policy is missing, the call returns AccessDenied.
- Trusting the wrong principal. A trust policy that names the original user does not help. The caller is the role session, so the trust must name the role.
- Chains that are too long. Each hop adds a trust policy to maintain and a step to audit. Prefer a flat design if two hops are enough.
Exercise
A script in account 111111111111 assumes HubRole in account 222222222222, then assumes SpokeRole in account 333333333333 with --duration-seconds 7200. The call fails, although SpokeRole has a maximum session duration of 12 hours. Why, and what should the script do?
Show answer
The second call is role chaining, and chained sessions are limited to one hour (3600 seconds), regardless of the role's maximum duration. The script should request 3600 seconds or less and call AssumeRole again before the credentials expire.