Tech Interview RoomStudy guides

AWS

Explain IAM: users, roles, policies, least privilege and how workloads get credentials.

Advanced · 2 min read · Lesson 6 of 7 in AWS

Principals and policies

  • Users (long-lived credentials): avoid for humans (use IAM Identity Center/SSO) and for workloads.
  • Roles: assumed through STS to obtain temporary credentials. Used by EC2 instance profiles, ECS task roles, Lambda execution roles, EKS pods, CI pipelines (OIDC) and cross-account access.
  • Policies: JSON documents with Effect, Action, Resource, Condition. Identity-based (attached to users and roles), resource-based (S3 bucket policies, KMS key policies, SQS queue policies), permission boundaries, session policies and organisation-level SCPs (guardrails that cap what accounts can do).

Evaluation logic

Default deny → an explicit Deny anywhere wins → otherwise access requires an Allow that is not cut off by SCPs, permission boundaries or session policies. Cross-account access requires an allow on both sides (the caller's identity policy and the resource policy or role trust policy).

Least privilege in practice

  • One role per service with only the actions and resources it needs (s3:GetObject on arn:aws:s3:::invoices/*, not s3:* on *).
  • Use conditions (aws:SourceVpce, aws:PrincipalTag, kms:ViaService).
  • Generate policies from CloudTrail activity with IAM Access Analyzer; remove unused permissions.
  • No access keys in code, images or config.

Workload credentials

  • EKS: IRSA (IAM Roles for Service Accounts, using the cluster's OIDC provider) or EKS Pod Identity: each Kubernetes service account maps to an IAM role; the AWS SDK picks up temporary credentials automatically.
  • ECS: task roles. EC2: instance profiles (with IMDSv2 required). CI: GitHub Actions OIDC → AssumeRoleWithWebIdentity.

Example

json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"],
    "Resource": "arn:aws:sqs:ap-south-1:123456789012:order-events"
  }, {
    "Effect": "Allow",
    "Action": "secretsmanager:GetSecretValue",
    "Resource": "arn:aws:secretsmanager:ap-south-1:123456789012:secret:orders/db-*"
  }]
}

Follow-up questions interviewers ask

Try answering each one before you open it.

How does cross-account access work?

Account B creates a role with a trust policy allowing a principal from account A to assume it (optionally requiring an ExternalId or MFA) and a permissions policy for what it may do in B. Account A's principal needs sts:AssumeRole on that role ARN. The caller then uses the temporary credentials returned by STS. Alternatively, resource policies (S3, KMS, SQS) can grant access directly to principals in another account.

Why require IMDSv2 on EC2?

The instance metadata service hands out the instance role's credentials. IMDSv1 answers simple GET requests, so an SSRF vulnerability in the application can steal them (the cause of well-known breaches). IMDSv2 requires a session token obtained with a PUT request and a hop limit, which blocks typical SSRF and container escapes.