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

AWS Cloud

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.

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

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.

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

AWS STS is the Security Token Service. It creates temporary credentials when an authorized caller starts a session. Track three actors in an example. The original caller signs the AssumeRole request. STS checks that the caller may assume the target role. The temporary session signs a later request to the AWS service using the role’s granted access.

The response includes an access key ID, secret access key, and session token that must travel as one set. Expiration tells you when that set stops being valid. In your lab, inspect only the account and ARN from get-caller-identity. Before assuming, it identifies the original caller. Afterwards, it should identify the assumed role and session. If the STS call is denied you never got a new session. If S3 rejects the new session’s read, check the allowed resources for that role.

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. Changing a role’s trust can prevent future assumptions. To cut off existing sessions for a supported IAM role, use AWS’s role-session revocation control. It adds an explicit denial for sessions issued before the selected time and can interrupt every current user of that role.
  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.

Use the session, then check which principal it represents

Keep AccessKeyId, SecretAccessKey and SessionToken as a complete bundle. Inspect the Expiration time and use get-caller-identity in the new session. Missing the session token can look like a bad key even when the first two values match.

Interview check What is role chaining?

Using one assumed-role session to assume another role is role chaining. Its session is bounded by the one-hour role-chaining limit even when the target role normally allows longer.

Reference documentation

Practise this chapter

Interview questions and answers

Where can an STS role workflow fail even though your IAM user can sign in?

The original caller may not be allowed to assume the target role or fail a condition in its trust. After a successful exchange, the temporary session can still lack resource access. I would compare identities before and after the exchange and diagnose each denied operation independently.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Which values from an STS credential set must be used together?

Answer and explanation

A. Access key ID, secret access key and session token

The token is part of the temporary credential set. Leaving it out can make the signed request invalid.

2. Which output tells you whether the new request uses the intended role?

Answer and explanation

B. The account and ARN from get-caller-identity

The identity call describes the principal that actually signed it without exposing the credential values.

3. Changing the trust policy can stop future role assumptions. What about already-issued sessions?

Answer and explanation

B. They require appropriate session revocation or permission controls to be cut off early

Trust controls new assumptions. AWS has a separate role-session revocation mechanism for supported roles.

Read an STS role session without exposing credentials

Before you start

  • Use a read-only AWS CLI profile if available, or the sample ARNs supplied here. Do not copy a secret key into the exercise.
  1. Inspect two synthetic caller identities under account 111122223333: arn:aws:iam::111122223333:user/Reader and arn:aws:sts::111122223333:assumed-role/LabReader/lab-run. Label the persistent identity and temporary role session.

    Expected result: The iam user ARN identifies Reader. The sts assumed-role ARN identifies the LabReader role session named lab-run.

  2. Write down the three required values in the credential set without writing any real values. Alongside them, document why the expiration needs checking.

    Expected result: The access key ID, secret key and session token work together. Expiration tells you when this temporary set stops being valid.

  3. If you have an approved named profile, run aws sts get-caller-identity --profile lab using your actual profile name. Compare its account and ARN with the expected signer before attempting other calls.

    Expected result: An identity response reports the actual principal. An unexpected signer calls for profile troubleshooting rather than additional permissions.

If the result differs

  • If an expected temporary request fails, check that the same credential set includes its token and has not expired. Do not print credentials into support output.

Finish and clean up

  • Close a dedicated lab shell after using it. No resources were created.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

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

Always confirm the account and identity before running changes.

Show the account ID and ARN of the identity your CLI is using.

  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?

Picking a VPC size needs this count.

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

  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

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

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

  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

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

Decode the authorization message stored in the shell variable $MSG.

  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

The trust policy says WHO may take the role.

Write a trust policy that lets ec2.amazonaws.com assume the role.

  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

The trust policy says WHO may take the role.

Write a trust policy that lets ecs-tasks.amazonaws.com assume the role.

  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

The trust policy says WHO may take the role.

Write a trust policy that lets lambda.amazonaws.com assume the role.

  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

Cross-account roles are safer than sharing access keys.

Write a trust policy that lets account 111122223333 assume this role.

  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

Shorter sessions lower the risk if keys leak.

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

  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

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

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

  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.