Sub topic 1
IAM roles and principals: a quick refresher
This chapter reviews the IAM building blocks that the rest of the tutorial relies on. It covers what a role is, what a principal is, and how a role differs from a user. It also shows how a role gets its permissions.
Prerequisites
This chapter assumes you already have the following:
- Basic familiarity with AWS accounts and the AWS CLI.
- Knowledge of IAM users, roles, and JSON policy documents.
- Awareness of what an ARN is.
If any of these is unfamiliar, review it first. Later chapters build directly on them.
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. Instead, a trusted caller assumes the role and receives temporary credentials from AWS Security Token Service (STS). Those credentials expire.
A role carries two kinds of policy:
- A permissions policy says what the role can do once someone has assumed it.
- A trust policy says who is allowed to assume it.
Keep these two apart. Most cross-account errors come from mixing them up.
What a principal is
A principal is an entity that can make a request to AWS. The Principal element in a policy names who the statement applies to. Common principal types are:
| Principal type | Example |
|---|---|
| AWS account | `arn:aws:iam::111111111111:root` |
| IAM user | `arn:aws:iam::111111111111:user/alice` |
| IAM role | `arn:aws:iam::111111111111:role/Deployer` |
| AWS service | `lambda.amazonaws.com` |
The root ARN in a trust policy does not mean the account's root user. It means "the account as a whole". Using it delegates the decision to that account's own administrators, who then control which of their identities may assume the role.
Roles compared with users
| Row | IAM user | IAM role |
|---|---|---|
| Credentials | Long-term access keys or password | Temporary, issued on assume |
| Who uses it | One person or application | Anyone the trust policy allows |
| Expiry | Keys last until rotated or deleted | Session ends at expiry |
| Cross-account use | Needs a separate user in each account | One role can be assumed from another account |
The last row is the reason this tutorial exists. Instead of creating a user in every account, you create one role and let trusted principals borrow it.
How the pieces fit together
The diagram shows the single-account flow. Cross-account access adds a second account to this picture in the next chapters.
A minimal role
A role is defined by its trust policy at creation time. This example lets EC2 instances in the same account use the role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}Save it as trust.json. Then create the role and attach an AWS managed permissions policy:
aws iam create-role \
--role-name DemoRole \
--assume-role-policy-document file://trust.json
aws iam attach-role-policy \
--role-name DemoRole \
--policy-arn arn:aws:iam::aws:policy/ReadOnlyAccessThe first command sets the trust policy. The second sets what the role can do. In later chapters you will change the Principal to point at another account.
Common mistakes
- Putting permissions in the trust policy. The trust policy only controls who can assume the role.
- Assuming that a trust policy alone is enough. Access also depends on the caller's own policies, as later chapters show.
- Reading
:rootas the root user. It means the whole account.
Exercise
A trust policy names arn:aws:iam::111111111111:root as the principal. Does this give the root user of account 111111111111 access, or something broader? What does the role's permissions policy do?
Show answer
It refers to the whole account 111111111111, so any identity in that account can assume the role if its own policies also allow it. The root user is not singled out. The permissions policy does not affect who can assume the role. It defines what the session can do after the role is assumed.