Sub topic 1
How IAM Works: Principals, Authentication, and Authorization
AWS Identity and Access Management (IAM) is the service that decides who can do what in an AWS account. This chapter gives you the working model that the rest of the tutorial builds on. You will learn the vocabulary, the two checks every request goes through, and the common mistakes that come from mixing them up.
Prerequisites
This chapter assumes you have the following:
- An AWS account and basic familiarity with the AWS Management Console.
- Comfort reading JSON.
- Basic command-line usage, ideally the AWS CLI.
- A general idea of the difference between authentication and authorization.
You do not need to have written an IAM policy before. Later chapters cover policy syntax in detail.
What IAM Does
IAM is a global service. Its data is not tied to one Region, and you do not pay extra for it. Every API call to AWS, whether it comes from the console, the AWS CLI, an SDK, or another AWS service, is sent to a service endpoint. That service asks IAM two questions about the caller:
- Who are you, and can you prove it? This is authentication.
- Are you allowed to do this specific thing? This is authorization.
IAM answers both. Keeping the two questions separate is the most useful habit you can build, because the errors and fixes for each are different.
Key Terms
| Term | Meaning |
|---|---|
| Principal | An entity that can make a request to AWS. Examples: the root user, an IAM user, an IAM role, an AWS service, a federated user. |
| Identity | An IAM resource that represents a principal inside your account: a user, a group, or a role. |
| Authentication | Proving that the principal is who it claims to be, using a password, an access key signature, or a session token. |
| Authorization | Deciding whether the authenticated principal may perform the requested action on the requested resource. |
| Action | The operation requested, such as `s3:GetObject` or `ec2:StopInstances`. |
| Resource | The AWS object the action applies to, identified by an Amazon Resource Name (ARN). |
| Request context | The information that comes with a request: who is calling, which action, which resource, and extra details such as time, source IP address, and whether MFA was used. |
A group is an identity but not a principal. You cannot sign in as a group or make a request as one. A group only collects users so that you can attach permissions to all of them at once. Groups get their own chapter later.
Principals You Will Meet
AWS accounts contain several kinds of principals.
- Root user. The identity created with the account, signed in with the email address you used to open it. It has unrestricted access to everything in the account. Use it only for the few tasks that require it.
- IAM users. Long-lived identities that you create inside the account. A user has a name and can have a password for the console and up to two access keys for programmatic access.
- IAM roles. Identities with permissions but with no password or access keys of their own. A principal assumes a role and receives temporary credentials.
- AWS services. Services such as Amazon EC2 or AWS Lambda can act as principals, usually by assuming a role that you define.
- Federated users. People or applications that sign in through an external identity provider and then receive temporary access.
The distinction between users and roles drives most IAM design decisions. A user is a durable identity with durable credentials. A role is a set of permissions that someone or something borrows for a short time.
Authentication: Proving Who You Are
A request to AWS must be authenticated unless the resource is configured for anonymous public access. How a principal proves its identity depends on how it connects.
- Console sign-in. An IAM user supplies the account ID or alias, a user name, a password, and, if enabled, a second factor from a multi-factor authentication (MFA) device.
- Programmatic requests. The AWS CLI and SDKs sign each request with Signature Version 4. The signature is computed from a secret access key. The secret itself is never sent over the network. AWS recomputes the signature and compares it.
- Temporary credentials. A role session supplies three values: an access key ID, a secret access key, and a session token. The token must be sent with every request, and it expires.
An access key ID that begins with AKIA belongs to a long-lived user key. One that begins with ASIA belongs to temporary credentials. This is a quick way to tell which kind you are looking at.
Authentication only establishes identity. A freshly created IAM user who signs in successfully still cannot do anything, because no one has granted that user any permissions.
Authorization: Deciding What Is Allowed
After authentication, AWS builds the request context and evaluates it against the policies that apply. A policy is a JSON document that lists permissions. The default for every request is deny. A request is allowed only when a policy explicitly allows it and no policy explicitly denies it.
Here is a small identity-based policy. It lets its holder list one S3 bucket and read the objects in it.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListTheBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket"
},
{
"Sid": "ReadObjects",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
}
]
}Each statement says that a given effect applies to a given action on a given resource. Two details are worth noticing now.
- The bucket itself and the objects inside it have different ARNs.
s3:ListBucketacts on the bucket, so its resource has no trailing path.s3:GetObjectacts on objects, so its resource ends in/*. - Nothing in this policy says who it applies to. It becomes meaningful only when you attach it to a user, group, or role.
Policy structure, policy types, and the full evaluation logic have their own chapters. For now, remember the three-part outcome: explicit deny, explicit allow, or implicit deny, which is what you get when nothing matches.
The Flow of a Request
The following diagram shows the path of one request.
Authentication failures and authorization failures look different. A bad or expired credential produces errors such as InvalidClientTokenId, SignatureDoesNotMatch, or ExpiredToken. A missing permission produces AccessDenied or UnauthorizedOperation, and the message usually names the principal and the action that was refused.
Seeing Your Own Identity
When a command fails, the first thing to check is who AWS thinks you are. The sts get-caller-identity command needs no permissions, so it always works with valid credentials.
aws sts get-caller-identityThe response contains three fields:
UserId: a unique identifier for the principal.Account: the account the credentials belong to.Arn: the ARN of the user or the assumed-role session.
An IAM user has an ARN like arn:aws:iam::111122223333:user/dev-user. A role session has an ARN like arn:aws:sts::111122223333:assumed-role/RoleName/SessionName. If the ARN is not the one you expected, the problem is in your credential setup, not in your policies.
Where Permissions Come From
A principal's effective permissions can come from several places. This chapter only names them. Later chapters examine each.
- Identity-based policies attached to the user, to a group the user belongs to, or to the role.
- Resource-based policies attached to the resource, such as an S3 bucket policy.
- Permissions boundaries, which cap what an identity-based policy can grant.
- Service control policies (SCPs), which cap permissions for accounts in an AWS Organization.
- Session policies, which cap the permissions of a particular role session.
Think of these as filters and grants. Identity-based and resource-based policies grant permissions. Boundaries, SCPs, and session policies only limit them. A limit never grants anything on its own.
Common Errors
- Treating a successful sign-in as permission. Authentication gives no access. New users start with none.
- Testing as the wrong identity. Environment variables, a named profile, or an instance role can silently override what you expect. Run
aws sts get-caller-identitybefore debugging a policy. - Confusing an authentication error with an authorization error. Editing a policy cannot fix an expired token.
- Using the root user for daily work. Root cannot be restricted by identity-based policies, so a leaked root credential exposes the whole account.
- Treating a group as a principal. A group cannot appear as a principal in a resource policy or make requests.
Summary
IAM authenticates a principal, then authorizes each request by evaluating policies against the request context. Users hold long-lived credentials, roles supply temporary ones, and groups only organize users. Every request is denied by default, so access exists only where a policy allows it and nothing denies it. The next chapters cover users, credentials, and the structure of policy documents.
Exercise
You run an AWS CLI command and receive AccessDenied. A colleague suggests rotating your access key. Explain why that suggestion probably does not address the error, and name the command you would run first.
Show answer
AccessDenied is an authorization error. AWS authenticated the request and then found no policy allowing the action, so it denied it implicitly or explicitly. Rotating the key would address an authentication problem, such as InvalidClientTokenId or SignatureDoesNotMatch. First run aws sts get-caller-identity to confirm which principal made the request. Then inspect the policies that apply to that principal and the action named in the error.
Exercise
In the S3 policy shown in this chapter, why does s3:ListBucket use the resource arn:aws:s3:::amzn-s3-demo-bucket while s3:GetObject uses arn:aws:s3:::amzn-s3-demo-bucket/*?
Show answer
s3:ListBucket is an action on the bucket itself, so its resource is the bucket ARN. s3:GetObject is an action on objects in the bucket, so its resource is the object ARN pattern ending in /*. Using the wrong ARN type for an action means the statement does not match the request, and the request is implicitly denied.