Lesson 10 of 11 · 12 min · AWS security
IAM: users, roles, policies and least privilege
What you will learn
- The IAM building blocks: users, groups, roles, policies
- How a policy is written and how AWS decides allow or deny
- Roles and temporary credentials, and why they beat access keys
- Identity-based versus resource-based policies, trust policies, conditions
- Least privilege in practice and the tools that help
- Account-level safety: root user, MFA, SCPs, Organizations
In simple words
IAM answers two questions for everything in your cloud account: who are you, and what are you allowed to do? It works like ID cards and permission slips. The golden rule is to give each person and each program only the permissions the job needs, and nothing more.
Words to know
- IAM: identity and access management: the service that controls who can do what.
- Policy: a document that lists what is allowed or denied.
- Role: a set of permissions that a person or a program takes on for a short time.
- Least privilege: giving only the access a task needs.
- MFA: a second proof at sign-in, such as a code from your phone.
Why IAM matters most
Every AWS API call is authenticated ("who are you?") and authorised ("may you do this?") by IAM (Identity and Access Management), a global service. Most serious cloud incidents come down to IAM mistakes: leaked access keys, overly broad permissions, a public resource policy. Understanding IAM is the core of cloud security.
The building blocks
| Concept | Meaning |
|---|---|
| Root user | the account owner identity with unlimited power. Protect it with MFA, do not use it for daily work, delete its access keys |
| IAM user | a long-lived identity for a person or program, with a password and/or access keys. Prefer not to create them; use federation (IAM Identity Center / SSO) for people |
| Group | a collection of users to attach permissions to |
| IAM role | an identity without permanent credentials that anyone (or any service) authorised can assume, receiving temporary credentials. The preferred mechanism for applications, services and humans via SSO |
| Policy | a JSON document describing permissions; attached to identities or resources |
| Principal | whoever makes the request: user, role session, AWS service, another account, federated identity |
Create a user and a policy with the CLI:
aws iam create-user --user-name asha
aws iam create-policy --policy-name read-demo-assets --policy-document file://read.json
aws iam attach-user-policy --user-name asha --policy-arn arn:aws:iam::000000000000:policy/read-demo-assets
An ARN (Amazon Resource Name) identifies any resource: arn:aws:iam::000000000000:policy/read-demo-assets (partition, service, region, account id, resource). The account number 000000000000 is a placeholder for your own 12-digit account id.
Policies
An identity policy is a list of statements:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": ["arn:aws:s3:::demo-assets", "arn:aws:s3:::demo-assets/*"]
}
]
}
- Effect:
AlloworDeny. - Action: the API operations,
service:Operation, wildcards allowed (s3:Get*). - Resource: which objects (ARNs). Note the two ARNs:
s3:ListBucketapplies to the bucket,s3:GetObjectto the objects (/*). Mixing them is a frequent cause of "access denied". - Principal (only in resource-based and trust policies): who the statement applies to.
- Condition: extra requirements: source IP, MFA present, tags, time, TLS (
aws:SecureTransport), organisation membership, requested region.
Example conditions: "Condition": {"Bool": {"aws:MultiFactorAuthPresent": "true"}}; "StringEquals": {"aws:RequestedRegion": "ap-south-1"}; "StringEquals": {"aws:ResourceTag/Project": "${aws:PrincipalTag/Project}"} (attribute-based access control, ABAC: people and resources tagged with the same project can interact, so you don't edit policies for each new project).
Types of policy
- Identity-based (attached to users, groups, roles): what this principal may do.
- Resource-based (attached to the resource: S3 bucket policy, KMS key policy, SQS queue policy, Lambda permissions, role trust policy): who may access this resource, including principals in other accounts.
- Permissions boundaries: a maximum a user/role can ever have (delegation guard rails).
- Service control policies (SCPs) in AWS Organizations: maximum permissions for entire accounts (for example "no one in the production OU may disable CloudTrail or use regions outside India").
- Session policies and VPC endpoint policies further restrict.
Evaluation logic
When a request arrives AWS evaluates all applicable policies:
- Explicit Deny always wins. Any matching
Denyin any policy ends the matter. - Otherwise there must be an explicit Allow (in the right combination of identity policy, resource policy, boundaries and SCPs). Within an account, an Allow in either the identity policy or the resource policy is enough (unless a boundary/SCP/session policy limits it); across accounts, both sides must allow.
- Everything else is an implicit deny: IAM is default deny.
That is why a new user can do nothing until you attach permissions, and why adding a Deny is the reliable way to enforce a rule that nobody can override.
Roles and temporary credentials
A role has two parts:
- A permissions policy: what the role can do.
- A trust policy: who may assume it.
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
"Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}
This trust policy lets the EC2 service assume the role on behalf of an instance. Our test created app-role with this trust policy and attached the read-only S3 policy; get-role showed the principal {"Service":"ec2.amazonaws.com"}. An instance profile carries the role to the instance, and the AWS SDK automatically fetches temporary credentials (access key, secret, session token) from the metadata service, refreshing them before they expire. There is no key to leak, rotate or commit. The same pattern applies to:
- Lambda (execution role), ECS tasks (task role), EKS pods (IRSA / Pod Identity),
- CI/CD (OIDC federation: GitHub Actions assumes a role; CI/CD course),
- Humans via IAM Identity Center (SSO): they assume permission-set roles with short sessions, and MFA, rather than having long-lived IAM users,
- Cross-account access: a role in account B trusting account A.
Assuming a role with the STS service returns temporary credentials:
aws sts assume-role --role-arn arn:aws:iam::000000000000:role/human-role --role-session-name demo --duration-seconds 900
LSIA... 2026-11-12T15:14:03Z (access key id prefix, expiry)
The credentials expire (from 15 minutes up to hours), which limits the damage of a leak. Long-lived access keys for IAM users are the riskiest credential type: if you must use them, rotate regularly, never commit them (scan with gitleaks: CI/CD course), give one key per purpose, and delete unused ones. The IAM credential report and "last used" data show stale keys.
Least privilege in practice
Principle: grant only the permissions needed, on only the resources needed, for only as long as needed.
- Start narrow, widen on evidence. Avoid
"Action": "*"and"Resource": "*".AdministratorAccessis for a few break-glass roles, not for applications or daily use. - Scope resources. Prefer ARNs of the specific bucket/queue/table, using prefixes (
arn:aws:s3:::shop-prod-assets/uploads/*). - Use conditions (MFA, source VPC endpoint or IP, tags, region, TLS).
- Separate roles per application and per environment; production roles are not usable from dev.
- Prefer AWS managed policies for starting points but read them (
ReadOnlyAccessis broad). Customer-managed policies are versioned and reusable; inline policies suit one-off strictly coupled permissions. - Generate and verify with tools:
- IAM Access Analyzer finds resources shared outside your account/organisation and generates least-privilege policies from CloudTrail activity (what the role actually used).
- Access Advisor / last accessed shows services a role has not used in 90 days: remove them.
- The policy simulator (
aws iam simulate-principal-policy, or the console) tests whether an action would be allowed. - Terraform + policy checks (Checkov, OPA) in CI reject wildcard policies.
- Review regularly. Permissions only grow unless someone removes them.
Writing policies with Terraform's aws_iam_policy_document (Terraform course) keeps them readable and reviewable.
Account-level safety
- Root: MFA enabled (hardware or passkey), no access keys, used only for the few tasks that need it, alerts on its use.
- MFA for all humans; SSO with short sessions.
- AWS Organizations with separate accounts for production, development, security and logging; SCPs as guard rails; consolidated billing.
- Password policy and account-level S3 Block Public Access.
- Alerting: CloudWatch/EventBridge rules on root login, IAM changes, and failed console logins (CloudTrail lesson).
- Break-glass access procedure, tested and audited.
Common mistakes
- Access keys in code, repositories, chat or AMIs.
"Action": "*", "Resource": "*""to make it work".- Long-lived IAM users for people instead of SSO.
- Sharing one IAM user between several people or services.
- Mixing up bucket and object ARNs.
- Trust policies that allow
Principal: "*"(anyone can assume the role) without conditions. - Never reviewing or removing permissions.
- Using the root user for routine tasks.
Try it yourself
- Create a policy that allows read-only access to one S3 prefix, attach it to a role used by an EC2 instance, and verify that the instance can read that prefix but not others.
- Write a trust policy allowing your CI provider's OIDC identity to assume a deployment role only from the
mainbranch. - Run Access Analyzer's policy generation for a role that has been in use, and compare the generated policy with its current one.
- Add an explicit
Denyfor actions outsideap-south-1and test with the policy simulator.
Check yourself
What is the difference between an IAM user and an IAM role?
A user has long-lived credentials (password, access keys). A role has no permanent credentials: it is assumed to obtain temporary ones, so it suits applications, services and federated humans.
What does IAM do when no policy mentions the requested action?
It denies it: IAM is default deny, and an explicit Deny always overrides any Allow.
Why are roles preferred over access keys for applications?
Credentials are temporary and delivered automatically, so there is no secret to store, leak or rotate, and permissions are tied to the workload identity.
What are the two parts of a role?
The permissions policy (what the role may do) and the trust policy (who may assume it).
Interview questions
How would you give a container in ECS access to one S3 bucket?
Create an IAM role for the task with a policy allowing only the needed actions on that bucket's ARNs, set it as the task role, and let the SDK use the temporary credentials; no keys in environment variables.
How do you implement least privilege across a growing organisation?
Separate accounts and roles per workload, SCP guard rails, permission boundaries for delegation, policy generation from actual usage with Access Analyzer, regular access reviews, automated policy checks in CI, and SSO with MFA for people.
A developer reports "AccessDenied" for S3 GetObject. How do you debug?
Identify the principal and request in CloudTrail, check identity policies for the action and the object ARN (not just the bucket), resource policies (bucket policy, KMS key policy), explicit denies, SCPs/boundaries, conditions (VPC endpoint, MFA, TLS), and encryption key permissions.
Summary
- IAM is default-deny; explicit Deny wins; Allow requires the right policy combination.
- Policies = Effect, Action, Resource, Condition (and Principal in resource/trust policies); mind bucket vs object ARNs.
- Roles with temporary credentials replace long-lived keys for apps, CI and people (SSO).
- Practise least privilege: narrow actions and resources, conditions, separate roles, Access Analyzer and last-used data, reviews, and strict root/MFA/Organizations hygiene.
Going deeper
- IAM user guide: identities, policies and roles.
- Policy evaluation logic: exactly how allow and deny are decided.
- IAM security best practices: the official checklist.
- IAM Access Analyzer: findings and policy generation.
- IAM Identity Center: SSO for people.
- AWS global condition context keys: condition keys for policies.
See how much of this you can explain
A free mock interview asks you new questions on Cloud platforms and AWS fundamentals for DevOps Engineer and scores every answer, with what to fix.