Sub topic 9
IAM roles and why they replace long-lived keys
This chapter assumes you know what an IAM user is, how access keys work, and how to read a JSON policy. It covers what a role is, how it differs from a user, and why roles are the preferred way to give access to applications and automation.
What a role is
An IAM role is an identity with permissions but no password and no access keys of its own. Nobody signs in as a role. A trusted principal assumes it, and AWS Security Token Service (STS) returns temporary credentials for that session. Those credentials expire, usually after one hour by default.
A role carries two kinds of policy:
- Permissions policies say what the role can do once assumed.
- A trust policy says who is allowed to assume it.
The next chapter covers trust policies in detail. For now, remember that both must be in place before anything works.
The problem with long-lived keys
An access key for an IAM user stays valid until someone deletes or deactivates it. That causes familiar problems:
- Keys get copied into code, config files, chat messages, and public repositories.
- Nothing forces rotation, so old keys stay active for years.
- A leaked key works from any network until you notice it.
- Each machine or teammate that shares a key makes it harder to tell who did what.
A role's credentials are short-lived and issued on demand. A leaked credential stops working when its session expires, and there is no stored secret to rotate.
How a role is used
Each assumption creates a session. CloudTrail records the role and the session name, which helps you trace actions back to a caller.
A role for an EC2 instance
An application on an EC2 instance should not hold an access key. Give the instance a role instead. The trust policy lets the EC2 service assume the role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}You attach the role to the instance through an instance profile, which is a container that passes the role to the instance. The AWS SDKs and CLI find the temporary credentials automatically and refresh them before they expire. Your code needs no keys.
When to choose a role
| Situation | Preferred identity |
|---|---|
| Application on EC2, Lambda, or ECS | Role attached to the workload |
| Access to another AWS account | Role assumed from the other account |
| Temporary elevated access for a person | Role assumed for a session |
| Third-party system that cannot assume roles | IAM user with an access key, as a last resort |
Common mistakes
- Creating a role with permissions but a trust policy that names the wrong principal. Assumption fails with
AccessDenied. - Putting access keys in an instance's environment when a role would work.
- Granting a role broad permissions because it is "only used by one service". Scope it like any other identity.
Exercise
A Lambda function needs to read objects from one S3 bucket. Why is attaching a role better than storing an IAM user's access key in the function's environment variables?
Show answer
The role gives the function temporary credentials that AWS issues and refreshes automatically and that expire on their own. An access key in an environment variable is a long-lived secret that can leak and must be rotated by hand. The role's permissions policy can also be limited to that one bucket.