IAM, accounts & least privilege
Create users, roles and policies without handing out admin.
By Invicly Tech, , in Full DevOps with Python. About 13 minutes to read.
In short
IAM decides who can do what in an AWS account. Everything is denied by default, and policies grant only the actions and resources a person or service needs.
Think of it like this: IAM is the building's security desk that hands out badges with specific door access.
Explained simply
IAM (Identity and Access Management) is the part of AWS that answers one question for every request: is this caller allowed to do this action on this thing? It does not matter if the request comes from the web console, the CLI, a Python script or another AWS service. Every call is checked.
The starting answer is always no. Nothing is allowed until a policy says yes. A policy is a small JSON document with a list of allowed or denied actions, such as s3:GetObject, and the resources they apply to, such as one bucket.
There are two main kinds of identity. A user is a long-lived identity, usually a person, with a password or access keys. A role has no password at all: a person or a service steps into it for a short time and gets temporary keys that expire on their own.
Least privilege means giving each identity only what its job needs. A backup script needs to write to one bucket, not to delete your databases. If its keys ever leak, the damage stays small.
The root user is the email address that created the account. It can do everything, including closing the account. Turn on MFA for it, do not create access keys for it, and use it only for the few tasks that truly need it.
Press Play on a diagram to walk through it one step at a time.
- Root user
- Full control
- Enable MFA
- Lock away, never use daily
- Users and groups
- People who sign in
- Attach policies to groups
- Roles
- Assumed by services or people
- Temporary credentials
- EC2, Lambda, CI pipelines
- Policies
- JSON documents
- Effect, Action, Resource
- Explicit deny always wins
- 1Start narrow
Only the actions the task clearly needs
- 2Test
Policy simulator or a real run in dev
- 3Read AccessDenied
Note the exact action and resource
- 4Add one permission
That action, on that ARN only
- 5Review usage
Last accessed and Access Analyzer; remove unused
- Back to step 1
- Attached to a user, group or role
- Says what that identity can do
- No Principal field
- Attached to a bucket, queue or key
- Has a Principal field
- Can grant access to other accounts
- A resource policy on a role
- Says who may assume the role
- Action is sts:AssumeRole or a variant
In depth
How AWS decides allow or deny
For each request AWS collects every policy that applies: the caller's identity policies, the resource's own policy (for example a bucket policy), and any guard rails set above the account, such as Service Control Policies (SCPs) in AWS Organizations or a permissions boundary on the role.
The rules are simple. First, if any policy has an explicit Deny that matches, the answer is deny, full stop. Second, if a guard rail like an SCP or boundary does not allow the action, the answer is deny, because these only set a maximum and never grant anything. Third, if an identity policy or a resource policy allows it, the answer is allow. Otherwise the request ends in an implicit deny.
This is why adding a new Allow can never undo a Deny, and why an admin in a member account can still be blocked by an SCP from the organization.
- Explicit deny beats every allow
- SCPs and permissions boundaries limit, they never grant
- No matching allow means implicit deny
- 1Collect policies
Identity, resource, SCP, boundary, session
- 2Explicit Deny?
If any matches, the request is denied
- 3Guard rails allow?
SCP and boundary must permit the action
- 4Any Allow?
Identity or resource policy grants it
- 5Result
Allow, or implicit deny if nothing matched
Reading and writing a policy
A policy has a Version (always "2012-10-17", the current language version, not a date you change) and one or more Statements. Each statement has an Effect (Allow or Deny), an Action list such as s3:PutObject, a Resource given as an ARN, and an optional Condition. An optional Sid is just a label.
An ARN (Amazon Resource Name) looks like arn:aws:service:region:account-id:resource. Some services leave parts empty: S3 bucket ARNs are arn:aws:s3:::bucket-name because bucket names are global.
A classic trap: s3:ListBucket acts on the bucket ARN, while s3:GetObject and s3:PutObject act on object ARNs ending in /*. If you put only bucket-name/* in Resource, listing fails with AccessDenied.
Conditions make policies precise. aws:MultiFactorAuthPresent checks MFA, aws:SourceIp checks the caller's IP, aws:RequestedRegion limits regions, and s3:prefix limits which folder a user may list.
- Wildcards work in actions and ARNs, like s3:Get*
- Prefer AWS managed policies to start, then write your own
- Validate new policies in the console's policy editor or with Access Analyzer
Roles and temporary credentials
A role has two policies. The trust policy says who may assume the role, for example the EC2 service, a Lambda function, another account, or GitHub's OIDC provider. The permissions policy says what the role can do once assumed.
Assuming a role is a call to AWS STS (Security Token Service). STS checks the trust policy and returns three values: an access key ID, a secret key and a session token. They expire, by default after one hour, and the role's maximum session duration can be set from 1 to 12 hours.
On EC2 you attach the role through an instance profile. The instance metadata service (use IMDSv2, which needs a session token) hands fresh credentials to the SDK, which refreshes them before they expire. In GitHub Actions, the workflow presents a signed OIDC token and calls AssumeRoleWithWebIdentity, so no AWS keys are stored as repository secrets at all.
- GitHub job to GitHub OIDC: Request signed ID token1Request signed ID token
- GitHub OIDC to GitHub job: JWT with repo and branch claims2JWT with repo and branch claims
- GitHub job to AWS STS: AssumeRoleWithWebIdentity3AssumeRoleWithWebIdentity
- 4Check trust policy conditionsAWS STS to AWS STS: Check trust policy conditions
- AWS STS to GitHub job: Temporary keys, about 1 hour5Temporary keys, about 1 hour
- GitHub job to S3: PutObject signed with temp keys6PutObject signed with temp keys
Root, users, groups and people in 2026
Only a handful of tasks need the root user: changing the account's root email or name, closing the account, restoring permissions when every admin is locked out, and some billing settings. Protect it with MFA (AWS now requires MFA for root in most accounts and supports several devices), and keep the credentials offline.
For IAM users, attach policies to groups such as developers or readonly, then put users in groups. A group is not an identity: you cannot name it as a Principal in a policy, and groups cannot contain other groups.
For people, AWS recommends IAM Identity Center (the successor of AWS SSO) over IAM users. People sign in once, pick an account and a permission set, and receive temporary role credentials; aws configure sso sets this up for the CLI. Plain IAM users with access keys are best kept for the rare tool that cannot use roles, and their keys should be rotated.
- Use aws iam generate-credential-report to see users, MFA status and key ages
- Delete access keys not used for 90 days
- One person, one identity: never share logins
Finding the smallest policy that works
Least privilege is a process, not a guess. Start with a narrow policy. When something fails, read the error: AccessDenied messages from many services now name the missing action and the policy type that blocked it. Add exactly that action on exactly that resource.
The IAM policy simulator, in the console or through aws iam simulate-principal-policy, tests whether a user or role would be allowed an action on a resource, without making the real call. It shows which statement allowed or denied it.
IAM Access Analyzer helps from the other side. It can generate a policy from the actions a role really used in CloudTrail logs, flag unused roles, keys and permissions, and warn when a bucket or role is shared outside your account. The Last accessed tab on a user or role shows which services were used and when, so you can remove the rest.
- aws sts get-caller-identity shows who you really are right now
- CloudTrail records every API call with the caller and the error code
- Review permissions on a schedule, not only when something breaks
Learn it in 5 short steps
About 50m each. Track them with a timer in your plan.
- 1Secure the root account with MFA~50m
- 2Create IAM users, groups and roles~50m
- 3Write a least-privilege JSON policy~50m
- 4Use roles instead of long-lived keys~50m
- 5Test access with the policy simulator~50m
Key points
- Protect the root user with MFA and do not create root access keys.
- Grant permissions to groups and roles, not to individual users one by one.
- Roles give short-lived credentials, so prefer them over long-lived access keys.
- Start with the smallest policy that works and widen only when something fails.
Key terms
Tap a card to flip it. 0/5 known
Examples
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::acme-uploads/*"
}]
}Read and write objects in one bucket, nothing else.
Give a new developer read-only access through a group
Step 1: create a group called developers. Step 2: attach the AWS managed ReadOnlyAccess policy to the group, not to the person. Step 3: create the user and add them to the group. Step 4: give them a console password that must be changed at first sign-in, and ask them to register MFA. Step 5: later, if they need more, add a policy to the group so every developer gets the same access.
aws iam create-group --group-name developers
aws iam attach-group-policy --group-name developers \
--policy-arn arn:aws:iam::aws:policy/ReadOnlyAccess
aws iam create-user --user-name alice
aws iam add-user-to-group --group-name developers --user-name alice
aws iam create-login-profile --user-name alice \
--password 'Temp-Change-Me-2026!' --password-reset-required
aws iam list-attached-group-policies --group-name developersLet an EC2 app read one bucket with a role, not keys
Step 1: write a trust policy that lets the EC2 service assume the role. Step 2: create the role and add an inline policy that allows s3:GetObject on acme-uploads/* only. Step 3: wrap the role in an instance profile, which is how EC2 carries a role. Step 4: attach the profile to the running instance. Step 5: on the instance, boto3 or the CLI now finds credentials automatically; there is nothing to copy or rotate.
cat > trust.json <<'EOF'
{"Version": "2012-10-17", "Statement": [{"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"}, "Action": "sts:AssumeRole"}]}
EOF
aws iam create-role --role-name app-reader --assume-role-policy-document file://trust.json
aws iam put-role-policy --role-name app-reader --policy-name read-uploads \
--policy-document file://s3-read.json
aws iam create-instance-profile --instance-profile-name app-reader
aws iam add-role-to-instance-profile --instance-profile-name app-reader --role-name app-reader
aws ec2 associate-iam-instance-profile --instance-id i-0abc123def4567890 \
--iam-instance-profile Name=app-readerQuick reference
- aws sts get-caller-identity prints the account, user or role you are using right now
- Policy Version is always "2012-10-17"; do not change it to today's date
- s3:ListBucket uses arn:aws:s3:::bucket; object actions use arn:aws:s3:::bucket/*
- Explicit Deny wins; SCPs and permissions boundaries only cap permissions
- Role = trust policy (who can assume) + permissions policy (what it can do)
- aws iam simulate-principal-policy tests access without making the real call
- aws iam generate-credential-report lists every user, MFA status and access key age
- For people, prefer IAM Identity Center and aws configure sso over IAM user keys
Common mistakes
- Attaching AdministratorAccess to get unblocked and never removing it.
- Committing access keys to Git; bots find and abuse them within minutes.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Find out which identity your CLI is using and list the managed policies attached to your user or role.
- Exercise 2medium
Write a policy that lets a user list only the team-a/ folder of bucket acme-data and read or write objects only inside it. Test it with the simulator.
- Exercise 3hard
Write a trust policy so that only workflows on the main branch of GitHub repo acme/api can assume a deploy role through OIDC.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between an IAM user and an IAM role?
- A user is a permanent identity with a password or long-lived access keys. A role has no credentials of its own; someone assumes it and gets temporary keys that expire, which makes leaks far less dangerous.
- Is IAM free?
- Yes, IAM users, groups, roles and policies cost nothing. You pay only for the AWS resources those identities use.
- Is IAM global or per region?
- IAM is global: users, roles and policies apply in every region of the account. You can still limit regions inside a policy with the aws:RequestedRegion condition.
- What should I do if I leak an access key?
- Deactivate and delete the key at once, create a new one if it is still needed, then check CloudTrail for what the key was used for. Roles with temporary credentials avoid this problem.