Sub topic 1
IAM roles and principals: a quick refresher
Cross-account access depends on two IAM ideas: the role and the principal. This chapter reviews both with the precision the rest of the tutorial needs. Later chapters assume you can read a role ARN, tell a role from its session, and say which policy answers which question.
Prerequisites
This chapter assumes you have:
- Basic familiarity with AWS accounts and the AWS CLI, including running
aws sts get-caller-identity. - Working knowledge of IAM users, roles, and JSON policy documents.
- Awareness of what an ARN (Amazon Resource Name) is. An ARN is the unique string that identifies an AWS resource.
You do not need a second AWS account yet. Every command here works in a single account.
What a role is
An IAM role is an identity inside an AWS account that has permissions but no long-term credentials. A role has no password and no access keys. A principal that is allowed to use the role asks AWS Security Token Service (STS) for temporary credentials. Those credentials expire automatically.
Compared with an IAM user, a role differs in three ways:
| Property | IAM user | IAM role |
|---|---|---|
| Credentials | Long-term password or access keys | Temporary credentials issued on demand |
| Who uses it | One person or one application | Anyone or anything the role trusts |
| Lifetime of access | Until keys are rotated or deleted | Until the session expires |
| Typical use | Direct human sign-in (legacy) | Workloads, federation, cross-account access |
Because credentials are short-lived, a leaked role session stops working on its own. This is the main reason AWS recommends roles over access keys wherever you can use them.
Principals: who can ask for access
A principal is an entity that can make a request to AWS. In policy documents, the Principal element names who a statement applies to. The common types are:
| Principal type | Example in a policy | Meaning |
|---|---|---|
| AWS account | `arn:aws:iam::111111111111:root` | Every principal in that account that is allowed by its own policies |
| IAM user | `arn:aws:iam::111111111111:user/alice` | One user |
| IAM role | `arn:aws:iam::111111111111:role/BuildRole` | One role (not a specific session) |
| Assumed-role session | `arn:aws:sts::111111111111:assumed-role/BuildRole/ci-42` | One live session of a role |
| AWS service | `ec2.amazonaws.com` | A service acting on your behalf |
| Federated identity | `arn:aws:iam::111111111111:saml-provider/Corp` | A user from an external identity provider |
The account root principal
The ARN ending in :root has a confusing name. In a policy, arn:aws:iam::111111111111:root means the account as a whole. It does not mean the root user. Used in a role's trust policy, it lets the account delegate the decision to its own IAM policies. The next chapters rely on this behavior.
Role ARN versus session ARN
A role has a fixed ARN. Each time someone assumes the role, STS creates a session with a different ARN. The sts service name and the assumed-role resource type appear in the session ARN, and the last segment is the session name that the caller chose:
Role: arn:aws:iam::222222222222:role/ReadOnlyAuditor
Session: arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-sessionCloudTrail and the output of get-caller-identity show the session ARN. You cannot use a session ARN in a trust policy Principal. Name the role instead.
Two policies on every role
Every role carries two different kinds of policy, and they answer different questions.
- The trust policy answers: who may assume this role? It is a resource-based policy attached to the role, and it always has a
Principal. - The permissions policies answer: what may a session of this role do? These are identity policies, either managed or inline, and they have no
Principalelement.
Here is a trust policy that lets the EC2 service assume a role, so that code on an EC2 instance can use the role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}This trust policy grants no access to any AWS resource. It only says who may become the role. A permissions policy, such as read access to one S3 bucket, is a separate document.
The unique ID behind an ARN
IAM stores a unique ID for each user and role. Role IDs start with AROA, and user IDs start with AIDA. If you put a role or user ARN in a Principal and then delete that identity, IAM shows the unique ID in the policy instead of the ARN. Recreating a role with the same name does not restore access, because the new role has a new ID. This protects you from someone re-creating a name to gain access, but it can look like a bug when you troubleshoot.
How a session comes to exist
Whether the caller is a person, an EC2 instance, or a Lambda function, the pattern is the same. The caller sends a request to STS. STS checks the role's trust policy. If the check passes, STS returns credentials that belong to a session of the role.
You can see the difference between a role and its session with one command. Run it first with your normal credentials:
aws sts get-caller-identityFor a user, the response contains a user ARN. For a session, it contains an assumed-role ARN:
{
"UserId": "AROAEXAMPLEROLEID:audit-session",
"Account": "222222222222",
"Arn": "arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-session"
}The UserId field joins the role's unique ID and the session name. The Account field shows which account the session lives in. This field matters more in cross-account work than any other.
Permissions come from the role, not the caller
When you assume a role, you give up your original permissions for that session. The session has only what the role's permissions policies allow, further limited by any session policy, permissions boundary, or service control policy. Your own user's permissions do not add to it.
Suppose your user can delete S3 buckets but the role can only read them. While you use the role's credentials, the delete is denied. The reverse is also true. A user with no S3 access can read buckets through a role that allows it. This is why assuming a role into another account is a clean way to give scoped access without sharing keys.
Common mistakes
- Confusing the two policies. Adding
s3:GetObjectto a trust policy has no effect, and adding aPrincipalto a permissions policy is invalid. - Reading
:rootas the root user. It means the whole account. - Putting a session ARN in a trust policy
Principal. Use the role ARN or the account. - Expecting a role to keep working after it is deleted and recreated. The unique ID changes.
- Forgetting that the caller needs its own permission to call
sts:AssumeRole. The trust policy alone is not enough. The next chapters show why.
What comes next
This chapter described a single role in a single account. The next chapter places the principal and the role in different accounts. It names the account that trusts and the account that is trusted, and it shows why permission has to be granted on both sides.
Exercise
A trust policy has "Principal": { "AWS": "arn:aws:iam::111111111111:root" }. Does this allow only the root user of account 111111111111 to assume the role? Explain what it actually means.
Show answer
No. The :root ARN stands for the whole account 111111111111, not its root user. The trust policy delegates the decision to that account, so any IAM principal there can assume the role if the account's own policies also allow it.
Exercise
You run aws sts get-caller-identity and see arn:aws:sts::222222222222:assumed-role/BuildRole/ci-42. Which account holds the role, what is the role name, what is the session name, and can you paste this ARN into a trust policy Principal?
Show answer
The role is in account 222222222222. The role name is BuildRole and the session name is ci-42. You cannot use this session ARN in a trust policy. Use the role ARN, arn:aws:iam::222222222222:role/BuildRole, or the account.
Exercise
A role's trust policy allows your user to assume it, but the role's permissions policy only allows s3:ListBucket. Your user can delete S3 objects. After you assume the role, can you delete objects with the session credentials? Why or why not?
Show answer
No. A session has only the permissions of the role (limited by any boundary or service control policy). The permissions of the user who assumed it do not carry over, so the delete is denied.