Sub topic 10
Assuming the role with configure-aws-credentials
This chapter assumes you can read a GitHub Actions workflow and know what an IAM role and its trust policy are. It also assumes the OIDC provider and trust policy from earlier chapters already exist. Here you will see what the aws-actions/configure-aws-credentials action does between the start of a job and your first AWS command.
What the action does
The action turns a GitHub-issued token into temporary AWS credentials that later steps can use. It does four things:
- It reads two environment variables that GitHub injects into the job when
id-token: writeis granted:ACTIONS_ID_TOKEN_REQUEST_URLandACTIONS_ID_TOKEN_REQUEST_TOKEN. - It requests an OIDC token from GitHub for a given audience,
sts.amazonaws.comby default. - It calls
sts:AssumeRoleWithWebIdentitywith that token and the role ARN you supplied. - It exports the returned credentials as environment variables and masks them in the log.
STS validates the token's signature against the identity provider you registered, then evaluates the role's trust policy against the token's claims. If any condition fails, no credentials are issued and the step fails.
The minimal step
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1Two inputs are all you need. role-to-assume is the ARN of the role whose trust policy accepts your repository. aws-region sets the default region for later commands. Because no access key inputs are given and the job has an OIDC token available, the action uses the web identity path automatically.
Inputs worth knowing
| Input | Default | Purpose |
|---|---|---|
| `role-to-assume` | none | ARN of the role to assume. |
| `aws-region` | none (required) | Region for the AWS CLI and SDKs. |
| `role-session-name` | `GitHubActions` | Name of the session. It appears in the assumed-role ARN and in CloudTrail. |
| `role-duration-seconds` | `3600` | Requested session length. The role's maximum session duration caps it. |
| `audience` | `sts.amazonaws.com` | Audience requested for the GitHub token. It must match the `aud` condition in the trust policy. |
| `role-chaining` | `false` | Allows assuming a second role using the credentials from a first one. |
Leave audience alone unless you work in a partition with a different audience, such as AWS China regions. In that case the provider's client ID, the trust policy condition, and this input must all agree.
What the step leaves behind
After the step succeeds, these environment variables are set for the rest of the job:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKENAWS_REGIONandAWS_DEFAULT_REGION
The AWS CLI, the SDKs, and tools such as Terraform and the CDK read these variables without extra configuration. The action also registers the key, secret, and session token as secrets, so GitHub replaces them with *** if they appear in logs.
The step also has an output, aws-account-id. You can use it to build ARNs without hard-coding the account number:
- uses: aws-actions/configure-aws-credentials@v4
id: aws
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
role-session-name: deploy-${{ github.run_id }}
- name: Confirm the session
run: |
echo "Account: ${{ steps.aws.outputs.aws-account-id }}"
aws sts get-caller-identityThe get-caller-identity call returns an ARN of the form arn:aws:sts::123456789012:assumed-role/github-deploy/deploy-1234567890. The part after the role name is the session name. Running this call right after the action is a cheap way to confirm the role you are using.
Choosing a session name
The default session name, GitHubActions, is the same for every run. Setting role-session-name to include github.run_id makes each run identifiable in CloudTrail and lets you trace an API call back to a specific workflow run. Session names allow letters, digits, and the characters _=,.@-, up to 64 characters. The action sanitizes invalid characters, but keep the name simple so you can search for it later.
Scope of the credentials
The environment variables belong to the job. Every later step in that job sees them, including third-party actions. Two consequences follow:
- Credentials do not carry over to other jobs. A job that needs AWS access must run the action itself, and each job gets its own session.
- Any step in the job can use the role. Pin third-party actions to a commit SHA, or keep them out of jobs that hold AWS credentials.
Credentials also expire. If a job runs longer than the session duration, later AWS calls fail with ExpiredToken. Raise role-duration-seconds for long jobs, and raise the role's maximum session duration to match. Otherwise STS rejects the request. Split long jobs where you can instead of requesting the maximum.
Chaining to a second role
Sometimes the OIDC role is a gateway into a more specific role, for example in another account. After the first step, call the action again with role-chaining: true:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-gateway
aws-region: us-east-1
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::210987654321:role/prod-deploy
aws-region: us-east-1
role-chaining: trueRole chaining has a hard AWS limit: a chained session lasts at most one hour, whatever the role's maximum session duration says. The second role's trust policy must also trust the first role, not GitHub.
Common errors
| Message or symptom | Likely cause |
|---|---|
| `Could not get ID token request URL` or similar about `ACTIONS_ID_TOKEN_REQUEST_URL` | The job lacks `permissions: id-token: write`. Pull requests from forks also never receive the token. |
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | A trust policy condition does not match the token. Check `aud`, then `sub`, including branch or environment spelling. |
| `No OpenID Connect provider found in your account` | The provider is missing, or the role's `Federated` principal points to a different account or URL. |
| `Could not load credentials from any providers` | `role-to-assume` is empty. This happens when a secret or variable it reads is not defined in this context. |
| `ExpiredToken` partway through a job | The job outlasted the session duration. |
When the trust policy rejects the call, the error does not say which condition failed. Compare the token's sub to your condition value character by character. A job that uses an environment produces repo:my-org/my-repo:environment:production, not a ref: value, so a branch-only condition will not match it.
Pinning the action version
The examples use @v4, a major-version tag that moves as fixes are released. For stricter control, pin to a full commit SHA and let a dependency update tool propose upgrades. This matters here because the action handles credentials for every job that uses it.
Exercise
A job runs aws-actions/configure-aws-credentials@v4 and fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". The workflow has id-token: write, and the trust policy requires sub to equal repo:my-org/my-repo:ref:refs/heads/main. The job declares environment: production. What is the most likely cause, and how do you fix it?
Show answer
When a job uses an environment, the token's sub becomes repo:my-org/my-repo:environment:production, so the branch-based condition no longer matches. Either change the condition to the environment form, or add the environment-based value to the trust policy. Using the environment form lets the environment's protection rules apply before AWS access is granted.
Exercise
Why does a second job in the same workflow fail with "Unable to locate credentials" even though the first job ran configure-aws-credentials successfully?
Show answer
The action exports credentials as environment variables for its own job only. Each job runs on a separate runner, so the second job must run the action itself to get its own session.