Lesson 3 of 8 in Cloud fundamentals with AWS
IAM: who is allowed to do what
AWS Identity and Access Management (IAM) is the service that decides who can do what to which resource. An identity such as a user or a role is given policies, which are JSON documents that allow or deny actions.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Accounts and billing safety before you build
The problem this solves
Every action in AWS is an API call: create a server, read a file, delete a bucket. Without rules, anyone with a login could do any of them. With one all-powerful login, a single leaked password or a single typo can delete everything.
IAM lets you give each person and each program only what they need. A deploy script that reads one bucket should not be able to create servers. This idea is called least privilege.
Programs need identities too. A server that reads files should not carry a permanent password. An IAM role gives it short-lived keys that AWS issues and rotates for you.
How it works
- People and clients
- Security
- Data
A user or role sends a request, IAM consults the attached policies, and only an allowed request reaches the resource.
- User or role to IAM check: 1 request
- Policies to IAM check: 2 rules consulted
- IAM check to Resource: 3 only if allowed
- Runs code
- Security
- Data
A server assumes a role, the trust policy decides who may do that, and the server then calls S3 with temporary keys instead of a stored password.
- EC2 instance to Role: 1 assume, get temp keys
- Trust policy to Role: decides who
- Role to S3 bucket: 2 call with those keys
A user is an identity for one person or program, with a name and optional long-term credentials. A group is a bundle of users that share policies. A role is an identity with no password of its own. Someone or something assumes it and receives temporary keys that expire.
A policy is a JSON document with statements. Each statement says Effect (Allow or Deny), Action (for example s3:GetObject), and Resource (which bucket or object). IAM starts from an implicit deny, so a request with no matching Allow is refused. An explicit Deny always wins over any Allow.
Roles are how servers, functions and pipelines get access without storing secrets. AWS recommends temporary credentials over long-term access keys for people as well, through IAM Identity Center. For a single learning account, a normal IAM user with MFA is simpler, and CloudShell avoids creating access keys at all.
Closest equivalents elsewhere: Azure uses role assignments (Azure RBAC) and managed identities, and Google Cloud uses IAM roles and service accounts.
Try it yourself
All commands run in AWS CloudShell, so you need no access keys. Everything here is read-only or local. IAM itself has no charge.
- 1Ask AWS who you are
aws sts get-caller-identity
You should see
{ "UserId": "<unique-id>", "Account": "<12-digit-account-id>", "Arn": "arn:aws:iam::<12-digit-account-id>:user/<your-user>" }This is the first thing to run whenever a command is denied. If the
Arnends in:root, you are signed in as root and should switch to your daily user. - 2Create a daily user in the console
In the console open IAM, then Users, then Create user. Name it
learner, allow console access, and attach the AWS managed policy AdministratorAccess. Use this only in a throwaway learning account. Finish by assigning an MFA device to this user, then sign in as it from now on. Real teams use IAM Identity Center and narrower permissions. - 3Read an AWS managed policy
aws iam get-policy --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess --query "Policy.DefaultVersionId" --output text aws iam get-policy-version --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess --version-id <version-id-from-above> --query "PolicyVersion.Document"
Replace the placeholder with the version the first command printed. Read the
Effect,ActionandResourcefields. You should see wildcard actions such ass3:Get*ands3:List*withResourceset to*, which is broad but read-only. - 4Write a narrower policy and test it without applying it
cat > read-one-bucket.json <<'EOF' { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": ["s3:GetObject"], "Resource": "arn:aws:s3:::example-learning-bucket/*" }, { "Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": "arn:aws:s3:::example-learning-bucket" } ] } EOF aws iam simulate-custom-policy --policy-input-list file://read-one-bucket.json --action-names s3:GetObject s3:PutObject --resource-arns arn:aws:s3:::example-learning-bucket/hello.txt --query "EvaluationResults[].[EvalActionName,EvalDecision]" --output tableThe simulator only evaluates the policy and changes nothing.
s3:GetObjectshould showallowed, ands3:PutObjectshould showimplicitDenybecause nothing allows it. The bucket does not need to exist. - 5Clean up
rm read-one-bucket.json
Nothing billable was created. The
learneruser stays and costs nothing. Delete it in the IAM console if you do not want it.
What breaks, and where to look
An error occurred (AccessDenied) when calling the ListBuckets operation: User: arn:aws:iam::<account-id>:user/learner is not authorized to perform: s3:ListAllMyBuckets because no identity-based policy allows the s3:ListAllMyBuckets action
- WHY IT HAPPENS
- Nothing in the user's policies allows that action, so IAM's default deny applies. The error names the exact action you need.
- CHECK FIRST
- Run
aws sts get-caller-identityto confirm who you are, thenaws iam list-attached-user-policies --user-name learnerandaws iam list-groups-for-user --user-name learnerto see what you have.
An error occurred (ExpiredToken) when calling the GetCallerIdentity operation: The security token included in the request is expired
- WHY IT HAPPENS
- Temporary credentials from a role, single sign-on or a long-idle session have a lifetime and have run out.
- CHECK FIRST
- Sign in again. If you use single sign-on on your laptop, run
aws sso login. In CloudShell, reload the page.
AWS emails you about an exposed access key, or unfamiliar resources appear in your account.
- WHY IT HAPPENS
- A long-term access key was committed to a public repository or pasted somewhere public. Bots scan for these and use them within minutes.
- CHECK FIRST
- In the IAM console, open the user's Security credentials and deactivate then delete the key. List keys with
aws iam list-access-keys --user-name <user>, then sweep for resources you did not create.
When not to use this
- AdministratorAccess is for a throwaway learning account. For anything real, start from a managed policy and narrow it to what the job needs.
- Do not create long-term access keys for people if single sign-on or CloudShell works. Keys that do not exist cannot leak.
Check yourself
What happens to a request that no policy mentions?
It is denied. IAM denies by default, and only an explicit Allow opens access.
Why give a server a role instead of access keys?
A role gives the server temporary keys that AWS rotates automatically, so there is no permanent secret to leak.
Which wins when one policy allows an action and another denies it?
The explicit Deny wins.
Keep going
Lesson 57 of 92 in the DevOps path