Sub topic 8
Reaching other accounts with IAM roles
Once an organization has many accounts, people and automation need a safe way to act in them. Creating an IAM user in every account does not scale. Long-lived access keys pile up, and offboarding someone means hunting through dozens of accounts. The standard answer is the cross-account IAM role: a role in the target account that principals from another account can assume to get temporary credentials.
This chapter assumes you know IAM users, roles and JSON policy documents, and that you can run the AWS CLI. It covers how assuming a role works, the default role that Organizations creates, the two policies every cross-account call depends on, and the errors you will meet in practice. The next chapter shows how IAM Identity Center builds workforce sign-in on top of this mechanism.
The working model
A role has two kinds of policy:
- The trust policy (also called the assume-role policy) says who may assume the role.
- The permissions policies say what the role can do once assumed.
The caller uses AWS STS (Security Token Service) to call sts:AssumeRole. STS checks the trust policy and, if the call is allowed, returns temporary credentials: an access key ID, a secret access key and a session token. These expire, by default after one hour. The caller then uses them to make requests inside the target account.
While acting under the assumed role, the caller has only the role's permissions. The permissions of the original user or role do not carry over. CloudTrail records the assumption in both accounts, which is what makes the pattern auditable.
Two sides must agree
A cross-account call succeeds only when both accounts allow it. This is the most common source of confusion.
- Account A (the caller) must grant the principal permission to call
sts:AssumeRoleon the target role's ARN. This is an identity-based policy on the user or role. - Account B (the target) must have a trust policy that names account A, or a specific principal in it.
If either side is missing, the call fails with AccessDenied. Within a single account, a trust policy that names a specific principal is enough on its own. Across accounts, both sides are required.
A trust policy that allows any principal in account 999988887777 to assume the role looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::999988887777:root" },
"Action": "sts:AssumeRole"
}
]
}Naming :root does not mean only the root user. It delegates the decision to account 999988887777, whose administrators then decide which of their users and roles may assume the role through identity policies. Naming a specific role ARN instead narrows trust to that one role.
The matching permission on the caller's side:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::111122223333:role/OrganizationAccountAccessRole"
}
]
}The role Organizations creates for you
When you create a member account with create-account, Organizations also creates a role in it. Its default name is OrganizationAccountAccessRole. The role:
- trusts the management account,
- carries the
AdministratorAccessmanaged policy, - exists so that you can administer the new account without its root user password.
You can choose another name with the --role-name parameter at creation time. Terraform exposes the same setting as role_name, as the earlier Terraform example showed.
Accounts that you invite rather than create do not get this role automatically. You must create an equivalent role yourself in each invited account, with a trust policy naming the management account. The chapter on creating and inviting accounts covers that step.
To use the role from the management account, assume it with the CLI:
aws sts assume-role \
--role-arn arn:aws:iam::111122223333:role/OrganizationAccountAccessRole \
--role-session-name admin-sessionThe response contains a Credentials block with AccessKeyId, SecretAccessKey, SessionToken and Expiration. You can export these as environment variables, but a named profile is easier and refreshes automatically.
Using roles through CLI profiles
Add a profile to ~/.aws/config that points at the role and names the profile that supplies the source credentials:
[profile mgmt]
region = eu-west-1
[profile prod-app1-admin]
role_arn = arn:aws:iam::111122223333:role/OrganizationAccountAccessRole
source_profile = mgmt
role_session_name = admin-session
region = eu-west-1Now any command with --profile prod-app1-admin makes the AssumeRole call for you and caches the credentials until they expire:
aws sts get-caller-identity --profile prod-app1-adminThe returned ARN has the form arn:aws:sts::111122223333:assumed-role/OrganizationAccountAccessRole/admin-session. Checking it is the quickest way to confirm which account and role you are acting as.
Hardening the trust policy
The default role is convenient and also powerful: it gives full administrator rights in the member account to anything the management account trusts. Treat it as a break-glass path and tighten the trust policy for other roles.
Require MFA
Add a condition so that the caller must have signed in with multi-factor authentication:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::999988887777:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}Restrict to the organization
When a role is meant for any account in your organization, avoid listing account IDs one by one. Use the aws:PrincipalOrgID condition key with a wildcard principal:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "*" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": { "aws:PrincipalOrgID": "o-exampleorgid" }
}
}
]
}The "*" principal looks alarming, but the condition limits it to principals that belong to the named organization. New accounts that join are covered automatically, and accounts that leave lose access.
The external ID for third parties
If a vendor outside your organization assumes a role in your account, use an sts:ExternalId condition. It prevents the confused deputy problem, where a third party is tricked into using its own access on behalf of another customer. External IDs are not needed between your own accounts.
Where SCPs fit in
Service control policies apply to roles as well as users. A role in a member account is limited by the SCPs on its account and parents, even if its permissions policy says Allow. An SCP never grants anything. It only caps what identity policies in member accounts can do.
Two points matter for cross-account access:
- The management account is not affected by SCPs. A principal there can assume the member account's role even if SCPs restrict the member account.
- Once the session is running inside the member account, the SCPs of that account apply to it. An administrator role in a member account cannot do what the SCPs deny.
This is why an SCP such as the region restriction from the earlier chapter holds even against OrganizationAccountAccessRole.
Session behavior and limits
| Setting | Detail |
|---|---|
| Default session length | 1 hour (3600 seconds) |
| Maximum length | The role's maximum session duration, from 1 to 12 hours, set on the role |
| Role chaining | Assuming a role using credentials from another assumed role. Sessions are capped at 1 hour, and the caller cannot choose a longer one |
| Session name | Free text you choose. Appears in CloudTrail, so use something traceable such as a ticket number or username |
| Session policy | An optional inline policy passed at assume time that further narrows permissions. It can never expand them |
The role chaining limit catches people out. If a CI role in account A assumes a role in account B, then that role assumes a role in account C, the last session cannot last longer than an hour, whatever the role's maximum says.
Common errors
AccessDeniedonAssumeRole. Check both sides: does the caller's identity policy allowsts:AssumeRoleon that ARN, and does the target's trust policy name the caller? Also check for SCPs or permission boundaries on the caller.MalformedPolicyDocumentwhen saving a trust policy. A principal in the trust policy that no longer exists is stored as a unique ID likeAIDA..., and a typo in the account ID or ARN is rejected.- A trust policy that suddenly stops working after a role was recreated. A trust policy that names a specific role stores that role's internal ID. Deleting and recreating a role with the same name gives a new ID, so you must update trust policies that referenced it.
- Credentials that stop working mid-script. The session expired. Use a profile so the CLI refreshes the credentials, or request a longer duration within the role's maximum.
- Wrong account. Run
aws sts get-caller-identitybefore changing anything. Acting in production while believing you are in a sandbox is a mistake that this check prevents.
Summary of the pattern
For most organizations the pattern looks like this: a small number of identities live in a central place, either the management account or an identity provider, and they assume narrowly scoped roles in workload accounts. Automation uses roles with trust limited to specific pipelines. No long-lived access keys exist in member accounts. The next chapter shows how IAM Identity Center creates and manages these roles for your workforce.
Exercise
A developer in account 999988887777 has an identity policy allowing sts:AssumeRole on arn:aws:iam::111122223333:role/DeployRole. The call fails with AccessDenied. DeployRole's permissions policy grants full S3 access. What is the most likely cause, and how do you fix it?
Show answer
The permissions policy is not the problem, because it only applies after the role has been assumed. The likely cause is the trust policy of DeployRole in account 111122223333, which does not name account 999988887777 or the developer's principal. Cross-account access needs both sides to allow the call. Fix it by adding a statement to the trust policy with "Principal": {"AWS": "arn:aws:iam::999988887777:root"} (or the specific user or role ARN) and "Action": "sts:AssumeRole". If that is already present, check for an SCP or permission boundary on the developer's identity that denies sts:AssumeRole.
Exercise
You want any role in any account of your organization to assume a shared AuditReader role, without editing the trust policy each time a new account is added. Which condition key do you use, and why is a wildcard principal acceptable here?
Show answer
Use aws:PrincipalOrgID with StringEquals and your organization ID, for example "o-exampleorgid". The principal can be "*" because the condition restricts the statement to principals that belong to that organization, so anyone outside it is denied. New member accounts are covered automatically when they join the organization. Each caller's own identity policy must still allow sts:AssumeRole on the role.
Exercise
A pipeline role in account A assumes a role in account B, which then assumes a role in account C and runs a four-hour job. The job fails after one hour even though the role in C has a maximum session duration of 12 hours. Why?
Show answer
Assuming a role with credentials from another assumed role is role chaining, and AWS caps chained sessions at one hour regardless of the role's maximum session duration. To fix it, break the chain, for example by letting the pipeline role in account A assume the role in account C directly, or by splitting the job into steps shorter than an hour.