Skip to the page

AWS for developers · mid

How code gets AWS credentials, and least-privilege policies

Give the compute an IAM role and let the SDK fetch short-lived credentials. Write policies that name actions and resources, keep secrets in a secrets service, and watch for iam:PassRole.

The notes

What you must know

  • Give the compute an IAM role. The SDK fetches short-lived credentials from STS and refreshes them. EC2: instance profile through the metadata service (IMDSv2). ECS and Fargate: task role. Lambda: execution role. EKS: EKS Pod Identity or IRSA.
  • The default credential chain checks environment variables first, then shared config and SSO profiles, web identity tokens, the container credentials endpoint and the instance metadata service. A leftover key in the environment silently wins over the role.
  • ECS task execution role: what ECS itself needs (pull from ECR, write logs, fetch secrets it injects). Task role: what your code calls. Do not mix them.
  • On a laptop use SSO or short-lived profile credentials, not a long-lived access key.
  • Policy review: s3:* on * lets the service read every bucket, delete buckets and change bucket policies. Narrow to s3:PutObject (and GetObject if needed) on one bucket prefix ARN. sqs:SendMessage on the one queue ARN.
  • Secret ARNs end in a six-character random suffix, so use the full ARN or a pattern like orders/db-??????.
  • iam:PassRole on * lets a service hand any role, even an admin role, to a new Lambda function or EC2 instance it creates. That is privilege escalation. Limit it to one role and condition it with iam:PassedToService.
  • Secrets Manager: built-in rotation, version labels (AWSCURRENT, AWSPENDING, AWSPREVIOUS), cross-account access, replication. Parameter Store: standard parameters for plain settings, SecureString with KMS, no built-in rotation. Read at startup and cache with a refresh.
  • AccessDenied in production but fine on the laptop: the laptop uses your admin credentials and the service uses its role. Check which identity the call used, the role policy and any resource policy or permission boundary.

The red pen

Common traps: mistake → fix

  • Mistake: Put an access key in an environment variable for the service

    Fix: Use a role. A key is long-lived and a leftover key overrides the role.

  • Mistake: The task execution role is the app's role on ECS

    Fix: The task role is for app code. The execution role is for ECS itself.

  • Mistake: s3:* on one bucket is the same as least privilege

    Fix: Name only the actions the code uses, on the prefix it needs.

Easy to hard

Questions interviewers ask, easy to hard

  1. How does your service on ECS or EKS get AWS credentials?
  2. In what order does the SDK look for credentials?
  3. What is the difference between a task role and a task execution role?
  4. Review this IAM policy for an orders service and list the risks.
  5. When would you use Secrets Manager and when Parameter Store?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview