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

AWS STS Temporary Credentials and AssumeRole Explained

STS is the counter that prints temporary AWS keys. You ask for a role; STS gives an access key, secret key and session token that die after a few minutes or hours.

What you'll learn in this chapter

  • What STS is and why it exists
  • AssumeRole and the three credential parts
  • Duration and session name
  • Who may call AssumeRole
  • Why temp beats long-lived keys

1. Everyday example first

Why do we need this?

Zomato delivery partners get a one-shift unlock code for the bike locker — not the master key forever. STS is that unlock code for AWS APIs.

Company names here are for teaching only; the locker story is made up.

What is it?

AWS STS (Security Token Service) issues temporary security credentials. The most common API is AssumeRole: “Let me wear this role for N seconds.”

AssumeRole pathCaller proveswho they areSTS checksrole trust policySTS returnsKey + Secret + TokenCaller callsS3 / EC2 / … with token
When the token expires, API calls fail until you AssumeRole again.

2. The three pieces

FieldMeaning
AccessKeyIdTemporary ID (starts like ASIA… often)
SecretAccessKeyTemporary secret — treat like a password
SessionTokenExtra token required on every signed request
ExpirationISO time when they stop working

CLI and SDKs need all three when you use assumed-role credentials. Forgetting the session token is a common “InvalidClientTokenId” mistake.

3. Duration and naming

  • Default max session is often 1 hour; you can request less. Role settings can allow up to 12 hours for some roles.
  • RoleSessionName appears in CloudTrail — use a clear name like ravi-lab-2026.
  • Optional MFA and external ID harden cross-account AssumeRole.

4. Why temporary wins

  1. Leaked session dies at expiration. Leaked IAM user key can live for years.
  2. You can revoke by changing the role trust or permissions.
  3. CloudTrail shows who assumed what and when.
  4. EC2/Lambda instance profiles use STS under the hood automatically.

Ravindra Bagale's Tip

Never commit temporary keys to Git either. They are still secrets until Expiration. Prefer profiles and instance roles.

5. Interview angles

Q. What does STS return?

AccessKeyId, SecretAccessKey, SessionToken, Expiration.

Q. AssumeRole vs GetSessionToken?

AssumeRole switches to a role; GetSessionToken often upgrades the same user (e.g. MFA).

Q. Why session token?

AWS needs it to prove the creds came from STS.

Q. Who can AssumeRole?

Principals listed in the role trust policy (and their own IAM allows sts:AssumeRole).

Q. Max duration?

Depends on role and caller; commonly up to 1–12 hours.

Try at home

  1. Read AWS docs page title only: “Temporary security credentials in IAM” — then come back and do the next lab.
  2. Next chapter: AssumeRole with the CLI step by step.

In short

  1. STS mints temporary AWS credentials.
  2. AssumeRole returns key + secret + session token.
  3. Sessions expire — that is the safety.
  4. Prefer STS/roles over permanent user keys.

Practice Tasks

About this scenario

Latur Coaching Classes is a coaching business in Latur. Pooja Jadhav 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 / 10

Easy Check who you are logged in as

Why: Always confirm the account and identity before running changes.

What to do: Show the account ID and ARN of the identity your CLI is using.

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.

Easy How many addresses in a /16?

Why: Picking a VPC size needs this count.

What to do: Latur Coaching Classes plans the VPC 10.56.0.0/16. How many IP addresses does it hold in total?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Assume a role

Why: AssumeRole gives you short-lived keys for another role, often in another account.

What to do: Assume role arn:aws:iam::123666135775:role/Auditor with session name pooja-audit.

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 Decode an "access denied" message

Why: Some errors come encoded; decoding shows exactly which policy blocked you.

What to do: Decode the authorization message stored in the shell variable $MSG.

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 Trust policy for an EC2 role

Why: The trust policy says WHO may take the role.

What to do: Write a trust policy that lets ec2.amazonaws.com assume the role.

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 Trust policy for an ECS task role

Why: The trust policy says WHO may take the role.

What to do: Write a trust policy that lets ecs-tasks.amazonaws.com assume the role.

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 Trust policy for a Lambda role

Why: The trust policy says WHO may take the role.

What to do: Write a trust policy that lets lambda.amazonaws.com assume the role.

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 Let another account assume a role

Why: Cross-account roles are safer than sharing access keys.

What to do: Write a trust policy that lets account 111122223333 assume this role.

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 Assume a role for 15 minutes

Why: Shorter sessions lower the risk if keys leak.

What to do: Assume arn:aws:iam::123666135775:role/Deployer with session name deploy-job for 900 seconds.

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 Get a session token with MFA

Why: Some actions need MFA. get-session-token returns keys that carry MFA proof.

What to do: Get a session token using MFA device arn:aws:iam::123666135775:mfa/pooja and the code 123456.

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.

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: What is AWS STS?. Zomato partners get a one-shift locker code — not the master key forever. APIs need short unlock codes too. AWS Security Token Service mints temporary security credentials. AssumeRole is the common API: wear this role for N seconds. Caller proves identity → STS checks role trust → returns AccessKeyId, SecretAccessKey, SessionToken, Expiration. After Expiration, S3 calls fail until you AssumeRole again — that expiry is the safety feature.
Revision card: What is AWS STS? · Download PNG · Open card page
Revision card: The three STS pieces. Students export only key and secret and wonder why every call is InvalidClientTokenId. STS sessions need AccessKeyId, SecretAccessKey, and SessionToken together. The token proves the creds came from STS. Export AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN from the AssumeRole response. aws sts get-caller-identity with all three shows the assumed-role ARN, not your long-lived user alone.
Revision card: The three STS pieces · Download PNG · Open card page
Revision card: Duration and session name. A 12-hour leaked lab session is worse than a 15-minute one. CloudTrail needs a name that is not “temp”. DurationSeconds is how long the temp creds live (role max and caller limits apply; labs often use 900). RoleSessionName appears in CloudTrail. Pass --duration-seconds 900 and a clear --role-session-name like ravindra-lab1 on AssumeRole. CloudTrail shows ravindra-lab1 assuming the role; after 15 minutes the same exported keys stop working.
Revision card: Duration and session name · Download PNG · Open card page
Comparison: AssumeRole vs GetSessionToken. AssumeRole: Switches into a role (new identity ARN). GetSessionToken: Stays the same IAM user; often after MFA.
Comparison: AssumeRole vs GetSessionToken · Download PNG · Open card page
Interview card: What does AssumeRole return?. AssumeRole returns AccessKeyId, SecretAccessKey, SessionToken, and Expiration. All three secret values must be used together until Expiration. Missing SessionToken makes API calls fail even if key and secret look valid. Expired sessions fail until you AssumeRole again. aws sts assume-role … then export the three fields and call aws sts get-caller-identity to see the assumed-role ARN.
Interview card: What does AssumeRole return? · Download PNG · Open card page
Interview card: AssumeRole vs GetSessionToken?. AssumeRole switches you into a role (different identity). GetSessionToken usually upgrades the same IAM user (often after MFA) without changing to a role. Cross-account and EC2/Lambda patterns use AssumeRole. MFA-protected CLI as the same user often uses GetSessionToken. Trust policy must allow your principal for AssumeRole; GetSessionToken does not need a role trust policy.
Interview card: AssumeRole vs GetSessionToken? · Download PNG · Open card page

More cards: Ravi Sir's Cards.