Ravindra BagaleCourses & study guides मराठी Track your progress

AWS IAM Users, Roles, Policies and Least Privilege Explained

IAM is the lock-and-key system of AWS. Users are people keys. Roles are temporary jackets a service or person wears. Policies are the rule papers. Least privilege means the smallest keys that still get the job done.

What you'll learn in this chapter

  • Users, groups, roles — who is who
  • Identity vs resource policies
  • Managed vs inline policies
  • Trust policy vs permissions policy
  • Least privilege, MFA, root dangers

1. Everyday example

Why do we need this?

In a Pune office, the receptionist does not get the server room key. The cleaning staff get a time-bound visitor pass. AWS must work the same way — or one leaked key empties your bank.

What is it?

IAM (Identity and Access Management) answers: who can call which AWS API on which resource.

IdentityUse forExample
Root userAccount owner only — MFA, almost never daily useOpen the shop in the morning once
IAM userLong-lived human (better: Identity Center / SSO)Named staff badge
GroupAttach policies to many users at onceTeam “developers”
RoleTemporary credentials for EC2, Lambda, or humans via SSOVisitor jacket for 1 hour

2. Policies in plain words

Why do we need this?

Without a policy, an identity can do almost nothing useful.

What is it?

  • Permissions policy: Allow/Deny actions on resources (e.g. s3:GetObject on one bucket).
  • Trust policy (role only): who may AssumeRole — EC2, Lambda, account, SAML…
  • AWS managed: AWS maintains (convenient, often broad).
  • Customer managed: you write/reuse — best for least privilege.
  • Inline: stuck to one user/role — use sparingly.
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:GetObject"],
    "Resource": "arn:aws:s3:::lab-bucket/*"
  }]
}

3. Least privilege habits

  1. Prefer roles over long-lived access keys on EC2/Lambda.
  2. Scope Resource ARNs — avoid * when you can.
  3. Separate duties: deploy role ≠ read-only auditor role.
  4. Turn on MFA for humans; never share root.
  5. CloudTrail on — know who called what.

Ravindra Bagale's Tip

AdministratorAccess on an IAM user for daily coding is a common beginner mistake. Use a power-user or custom policy; keep admin for break-glass.

4. Interview angles

Q. User vs role?

User: long-lived principal; role: assumable temporary creds.

Q. Trust policy?

Who can assume the role.

Q. Inline vs managed?

Inline = one identity; managed = reusable.

Q. Explicit Deny?

Deny always wins over Allow.

Q. Why not root daily?

Root is unlimited; breach = full account.

Try at home

  1. List every IAM user in your lab account; disable unused access keys.
  2. Next chapter: build a real EC2 + Lambda role lab.

In short

  1. Users/groups/roles + policies = IAM.
  2. Trust ≠ permissions.
  3. Least privilege and MFA beat shared keys.
  4. Root is for rare account tasks only.

Practice Tasks

About this scenario

Patil Dairy is a dairy business in Kolhapur. Sunita Patil looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy Read-only access to one bucket

Why: Least privilege: give only the actions and the bucket the job needs.

What to do: Write an identity policy that lets the user read objects (s3:GetObject) only in bucket patildairy-data.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Lambda access to one table

Why: The function only needs to read and write items in its own table.

What to do: Allow dynamodb:GetItem, dynamodb:PutItem, dynamodb:Query on arn:aws:dynamodb:ap-south-1:100444365147:table/patildairy-orders only.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Start and stop servers only

Why: The night operator may start and stop servers but not terminate them.

What to do: Allow ec2:StartInstances and ec2:StopInstances on all instances in account 100444365147, Region ap-south-1. Also allow ec2:DescribeInstances on *.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium List and read one bucket

Why: ListBucket works on the bucket ARN; GetObject works on object ARNs.

What to do: Allow s3:ListBucket on bucket patildairy-data and s3:GetObject on all its objects. Nothing else.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Upload-only to one folder

Why: Field staff can drop files but cannot read or delete anything.

What to do: Allow s3:PutObject only under the prefix uploads/ in bucket patildairy-data.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Attach a managed policy to a group

Why: AWS managed policies are ready-made and kept up to date by AWS.

What to do: Attach the AWS managed policy arn:aws:iam::aws:policy/ReadOnlyAccess to the group Auditors.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Create a customer managed policy

Why: Your own managed policy can be reused on many users, groups and roles.

What to do: Create a policy named patildairy-s3-read from the document in policy.json.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Let a function write logs

Why: Without these actions, Lambda logs never appear in CloudWatch.

What to do: Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Deny access from outside the office

Why: An explicit Deny with aws:SourceIp blocks keys used from anywhere else.

What to do: Write one Deny statement: deny all actions (*) on all resources (*) when the source IP is NOT in 203.0.113.0/24.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Allow only on tagged servers

Why: Tag conditions let one team control only its own servers.

What to do: Allow ec2:StopInstances on any instance (*) only when the instance tag Team equals patildairy.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Add an inline policy to a user

Why: An inline policy lives inside one identity and is deleted with it.

What to do: Put an inline policy named billing-view on user sunita, from billing.json.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Allow PassRole for one role

Why: To launch EC2 with a role, the user needs iam:PassRole on that role.

What to do: Allow iam:PassRole on arn:aws:iam::100444365147:role/patildairy-ec2-role only.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: IAM users, roles, policies. Root login for daily work and shared access keys are classic interview red flags. IAM controls who can do what in AWS. Users are people/services with long-lived creds; roles are assumable identities; policies are JSON Allow/Deny documents. Prefer IAM Identity Center or roles for humans; attach least-privilege policies; enable MFA on privileged users. An EC2 app reads S3 via an instance role — no access keys in the AMI.
Revision card: IAM users, roles, policies · Download PNG · Open card page
Revision card: Least privilege. AdministratorAccess on every role turns one leak into a full account breach. Least privilege means Allow only the actions and resource ARNs needed — prefer customer-managed policies over broad AWS managed when you can. Start with s3:GetObject on one bucket ARN, test, then add only what fails. Lambda that only reads one bucket cannot DeleteBucket even if compromised code tries.
Revision card: Least privilege · Download PNG · Open card page
Revision card: Trust policy vs permissions. People mix “who may assume this role” with “what the role can do”. Trust policy (role only): who can AssumeRole (EC2, Lambda, account…). Permissions policy: Allow/Deny API actions on resources. EC2 role trust Principal Service ec2.amazonaws.com; permissions allow s3:GetObject on lab bucket. Lambda cannot assume an EC2-only trust; you create a separate Lambda execution role.
Revision card: Trust policy vs permissions · Download PNG · Open card page
Comparison: IAM user vs IAM role. IAM user: Long-lived password/access keys for that identity. IAM role: Temporary credentials via AssumeRole.
Comparison: IAM user vs IAM role · Download PNG · Open card page
Interview card: Why not use root daily?. Root has unrestricted power; a phish or leaked password owns the account. Use an admin IAM user/role with MFA for daily admin; lock root away. Enable MFA on root; create IAM admin; never make access keys for root.
Interview card: Why not use root daily? · Download PNG · Open card page
Interview card: Roles vs long-lived access keys on EC2?. Roles deliver temporary credentials via instance metadata; keys get committed and leaked. Rotate pain disappears for instance workloads when you use roles. Attach instance profile; AWS CLI works without aws configure keys.
Interview card: Roles vs long-lived access keys on EC2? · Download PNG · Open card page

More cards: Ravi Sir's Cards.