Sub topic 15
Credential lifetime and session duration
This chapter covers how long the temporary AWS credentials from a GitHub Actions job stay valid, which settings control that, and what to do when a job outlives its credentials. It assumes you can already deploy with aws-actions/configure-aws-credentials, have written a role trust policy, and can read a basic IAM policy document.
Three lifetimes, not one
Three separate clocks run during a deployment, and mixing them up is the usual source of confusion.
| Item | Who sets it | What it limits |
|---|---|---|
| GitHub OIDC token | GitHub | How long AWS will accept the token in the exchange. It is valid for a few minutes. |
| Requested session duration | The workflow (`role-duration-seconds`) | How long the AWS credentials last once issued. The action's default is 3600 seconds. |
| Role maximum session duration | The IAM role (`MaxSessionDuration`) | The upper bound on any request. It can be set from 1 to 12 hours, and the default is 1 hour. |
The OIDC token matters only at the moment of the exchange. After AssumeRoleWithWebIdentity succeeds, AWS no longer looks at it. The credentials it produced stay valid until they expire, even if the token has expired and even if the job has finished.
What the action does with the credentials
configure-aws-credentials exchanges the token once, then exports AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN as environment variables for the later steps in the same job. It also masks the values in the logs. Credentials are not shared between jobs. Each job that needs AWS access must run its own configure-aws-credentials step, and each one gets a fresh session with its own clock.
Most deployments finish well inside one hour, so the defaults are fine. Lifetime becomes a concern in two cases: a long job (large builds, slow CloudFormation waits, big s3 sync runs), and a security review that asks why a credential should outlive its job.
Extending the session for long jobs
If a step fails late in a job with ExpiredToken or The security token included in the request is expired, the session ran out. There are two fixes.
The first is to request a longer session. This takes two changes: raise the role's maximum, then ask for more in the workflow. The role's maximum is an IAM setting, not part of the trust policy:
aws iam update-role \
--role-name github-deploy \
--max-session-duration 7200Then request the longer session in the workflow:
name: long-deploy
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 110
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
role-duration-seconds: 7200
- name: Long-running deployment
run: aws cloudformation deploy --stack-name my-stack --template-file template.yamlThe order matters. If role-duration-seconds is larger than the role's MaxSessionDuration, STS rejects the request and the step fails before any deployment work starts. The failure is a validation error about the duration, not a trust policy error.
The second fix is to re-authenticate midway. Split the work so the second half runs after another configure-aws-credentials step, or run it as a separate job. The new step requests a new token and gets a new session. This keeps each session short, and the job can run as long as GitHub allows.
Prefer the second approach when the job is long because it waits on something slow. A longer session widens the window in which a leaked credential is usable. Raise the duration only as far as the real job needs, and keep a margin for retries.
Limits worth knowing
- Minimum duration. STS accepts at least 900 seconds (15 minutes) for a session.
- Role chaining. If the job assumes a second role using the first role's credentials, AWS caps the chained session at one hour. This cap applies no matter what
MaxSessionDurationsays on the second role. This affects the multi-account pattern covered later, where a deployment role in one account assumes a role in another. Plan long cross-account jobs around it. - Job timeout. GitHub's own job limits are separate from AWS. Set
timeout-minutesto something close to your expected runtime so a stuck job does not hold valid credentials for hours. - Services that outlive the session. A CloudFormation deployment continues running in AWS after the credentials that started it have expired. Only your job's polling calls need valid credentials. If a poll fails with
ExpiredToken, the stack operation may still be progressing, so check the stack status before you re-run anything.
Cutting a session short
You cannot delete an issued STS session. If you suspect a leak, you can make existing sessions useless by attaching a deny policy that applies to every session issued before a given time. This is what the IAM console's "Revoke active sessions" button does:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"DateLessThan": {
"aws:TokenIssueTime": "2026-12-04T15:00:00Z"
}
}
}
]
}Set the timestamp to the current time. Sessions issued before it are denied everywhere, and new sessions issued afterwards are unaffected. This is also why short durations help. A one-hour session needs little revoking, because it expires on its own.
Choosing a duration
- Start with the default of one hour. Measure the job's real runtime across several runs.
- If the job is consistently over 40 minutes, either split it into jobs that each authenticate, or raise the duration to the runtime plus a margin.
- Keep the role's
MaxSessionDurationclose to the largest value any workflow actually requests. A role that allows 12 hours but is only ever used for 10 minutes gives an attacker more room than it needs. - Use separate roles for fast, narrow jobs and for slow, broad ones, so only the second has the higher maximum.
Exercise
A deployment job requests role-duration-seconds: 14400 (4 hours) from a role whose maximum session duration is the default. What happens, and what two changes would make the job work if it really needs about 3 hours?
Show answer
The role assumption fails with a validation error about the duration, because the request exceeds the role's maximum of 3600 seconds (1 hour by default). The job stops at the configure-aws-credentials step. To fix it, either raise the role's maximum with aws iam update-role --max-session-duration to at least 10800 and request a matching role-duration-seconds, or split the work into jobs (or steps preceded by another configure-aws-credentials) so each session stays within one hour. Splitting is the lower-risk option.
Exercise
A job assumes github-deploy and then, using those credentials, assumes account-b-deploy in another account. You set role-duration-seconds: 7200 on the second assumption and the role allows it. How long can the second session last?
Show answer
At most one hour. Role chaining caps the second session at 3600 seconds regardless of the role's maximum session duration, so a longer request is rejected.