Sub topic 7
Working with temporary credentials and session duration
This chapter assumes you can read IAM policy JSON, know what an ARN is, and have the AWS CLI installed. You should also have a trust policy and a caller policy in place from the earlier chapters, so that sts:AssumeRole succeeds.
What AssumeRole returns
A successful AssumeRole call returns a set of temporary credentials. They have four parts:
| Field | Purpose |
|---|---|
| `AccessKeyId` | Identifies the session. Temporary key IDs start with `ASIA`. |
| `SecretAccessKey` | Signs requests, like a long-term secret key. |
| `SessionToken` | Must accompany every request. Without it, AWS rejects the key pair. |
| `Expiration` | The UTC time after which the credentials stop working. |
The call from the previous chapter looks like this:
aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/ReadOnlyAuditor \
--role-session-name audit-sessionThe --role-session-name value appears in CloudTrail and in the assumed-role ARN. Choose a name that identifies the person or job, so you can trace actions later.
Using the credentials by hand
The CLI does not store these values for you. To use them, export all three as environment variables:
export AWS_ACCESS_KEY_ID=ASIAEXAMPLEKEYID
export AWS_SECRET_ACCESS_KEY=exampleSecretAccessKey
export AWS_SESSION_TOKEN=exampleSessionToken
aws sts get-caller-identityThe last command returns an ARN of the form arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-session. If it shows your original identity, the variables were not picked up.
Session duration
By default, a session lasts one hour (3600 seconds). You can request a different length with --duration-seconds:
aws sts assume-role \
--role-arn arn:aws:iam::222222222222:role/ReadOnlyAuditor \
--role-session-name long-audit \
--duration-seconds 7200Two limits apply to the value you request:
- The minimum is 900 seconds (15 minutes).
- The maximum is the role's maximum session duration setting. This setting defaults to one hour and can be raised to 12 hours (43200 seconds).
If you request more than the role allows, the call fails with a ValidationError. The fix is to raise the role's maximum session duration in the trusting account, or to request a shorter session. Setting a higher maximum does not lengthen sessions by itself, because callers must still ask for the longer duration.
What happens at expiration
Credentials cannot be extended. After Expiration passes, requests fail with an ExpiredToken error. You then call AssumeRole again to get a fresh set. Calling it again does not revoke the earlier session, so both remain valid until each expires.
Choosing a duration
Shorter sessions limit the damage if credentials leak. Longer sessions suit jobs that run for hours without a way to refresh. A reasonable approach:
- Keep the default hour for interactive work.
- Raise the role's maximum only for roles that back long batch jobs.
- Prefer a profile (next chapter) for interactive use, because the CLI renews the session for you.
Common errors
ExpiredToken: the session ended. Assume the role again.ValidationErroronDurationSeconds: the request exceeds the role's maximum session duration.InvalidClientTokenIdafter exporting variables: theAWS_SESSION_TOKENis missing or was truncated when pasted.
Exercise
You request --duration-seconds 14400 (4 hours) for a role whose maximum session duration is the default. What happens, and what two changes could make the request succeed?
Show answer
The call fails with a ValidationError, because 14400 seconds exceeds the role's one-hour maximum. You can either lower the requested duration to 3600 seconds or less, or raise the role's maximum session duration to at least 4 hours in the account that owns the role.
Exercise
After exporting the three temporary credential variables, aws sts get-caller-identity --profile auditor still shows the assumed-role ARN from your manual session instead of the profile's. Why?
Show answer
Environment variables take precedence over profiles. Unset AWSACCESSKEYID, AWSSECRETACCESSKEY, and AWSSESSIONTOKEN so the CLI falls back to the profile.