Sub topic 10
Assuming the role with configure-aws-credentials
This chapter assumes you know basic GitHub Actions workflows, IAM roles and policies, and have already created the OIDC provider and a role with a trust policy. It covers the step that connects the workflow to that role.
What the action does
aws-actions/configure-aws-credentials is the official GitHub Action that turns a GitHub OIDC token into AWS credentials. When you give it a role-to-assume and no access keys, it does four things:
- Requests an OIDC token from GitHub, with
sts.amazonaws.comas the audience by default. - Calls
sts:AssumeRoleWithWebIdentitywith that token and your role ARN. - Receives temporary credentials (access key ID, secret key, and session token).
- Exports them as environment variables, so later steps and the AWS CLI use them automatically.
Configuring the step
Add the action after checkout and before any step that calls AWS:
name: assume-role-demo
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
check:
runs-on: ubuntu-latest
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
- name: Show the assumed identity
run: aws sts get-caller-identityThe inputs you will use most often:
| Input | Purpose |
|---|---|
| `role-to-assume` | ARN of the role to assume. Required for OIDC. |
| `aws-region` | Region exported to later steps. Required. |
| `role-session-name` | Name shown in CloudTrail for this session. |
| `role-duration-seconds` | Session length. The default is 3600 seconds. |
| `audience` | Token audience. Change it only if your provider uses a different one. |
Checking that it worked
aws sts get-caller-identity returns an Arn like arn:aws:sts::123456789012:assumed-role/github-deploy/GitHubActions. The assumed-role part confirms the job is using the role and not a stored key. The action also masks the credentials in logs.
Common mistakes
- Putting
id-token: writeonly on the workflow when a job sets its ownpermissionsblock. A job-level block replaces the workflow-level one, so repeat the permission there. - Setting
contents: readaside when you definepermissions. Once you list any permission, unlisted ones becomenone, andactions/checkoutneedscontents: readin private repositories. - Using the wrong region or a role ARN from another account. The role must be in the account that holds the OIDC provider.
- Running the workflow from a branch or event that the trust policy's
subcondition does not allow. The token'ssubmust match exactly what the role permits. - Triggering from a fork's pull request. Forks do not get
id-token: write, so the step fails.
Exercise
A workflow has a top-level permissions: id-token: write block, but the deploy job defines permissions: contents: read. The action fails before calling AWS. Why, and how do you fix it?
Show answer
A job-level permissions block replaces the workflow-level one, so the deploy job no longer has id-token: write and cannot request a token. Add id-token: write to the job's permissions block.