Skip to content
Visual explainer 3h 30m to learn 5 steps

Deploy to AWS from GitHub Actions with OIDC

Validate and deploy to a lab account from GitHub Actions with a short-lived role limited to trusted branches, approvals and smoke tests.

By Invicly Tech, , in AWS Cloud Engineering. About 11 minutes to read.

Topic 25 of 38

In short

GitHub Actions can deploy to AWS without any stored access keys. Each workflow run asks GitHub for a signed OpenID Connect token, and AWS STS swaps it for short-lived credentials of an IAM role whose trust policy names the exact repository, branch or environment allowed. Add validation before deploy, a required approval for the environment and smoke tests after it, and CI becomes a safe, repeatable release path.

Explained simply

To deploy from CI, the pipeline needs AWS credentials. The old way was to create an IAM user, make an access key and paste it into GitHub secrets. That key never expires, works from anywhere and is easy to leak in a log. If someone steals it, they can use it until a person notices and deletes it.

OpenID Connect, OIDC for short, removes the stored key. When a workflow runs, GitHub can issue it a signed token that says which repository, branch and environment the run belongs to. AWS checks the signature and the details, and if they match a role's rules, it hands back temporary credentials that expire in about an hour.

You set this up once in AWS. Add GitHub as an identity provider in IAM, then create a deploy role whose trust policy says, in effect, only the main branch of example-org/file-service may assume me. The role's permissions say what a deploy may do, such as run CloudFormation.

A good release workflow has stages. Validate on every pull request: lint, unit tests and cdk synth or cdk diff, with read-only access or none. Deploy only from main, inside a GitHub environment that can require a person to approve. After deploying, run smoke tests that call the service and fail the job if something is wrong.

Cost note: GitHub Actions minutes are free for public repositories and limited on private plans; check your plan. On the AWS side, IAM, STS and OIDC have no charge. You can practise the trust policy logic with the IAM policy simulator or by reading the token claims, before you ever run a deploy.

Press Play on a diagram to walk through it one step at a time.

SequenceHow a workflow gets AWS credentials with OIDC
GitHub Actions job
GitHub OIDC provider
AWS STS
Lab account
  1. 1Request ID token (needs id-token: write)
    GitHub Actions job to GitHub OIDC provider: Request ID token (needs id-token: write)
  2. 2Signed JWT with repo, ref and environment claims
    GitHub OIDC provider to GitHub Actions job: Signed JWT with repo, ref and environment claims
  3. 3AssumeRoleWithWebIdentity with the token
    GitHub Actions job to AWS STS: AssumeRoleWithWebIdentity with the token
  4. 4Trust policy checks aud and sub, returns temporary credentials
    AWS STS to GitHub Actions job: Trust policy checks aud and sub, returns temporary credentials
  5. 5cdk deploy, then smoke tests
    GitHub Actions job to Lab account: cdk deploy, then smoke tests
FlowA release pipeline for the lab account
  1. 1
    Pull request

    Lint, tests, cdk synth, no deploy credentials

  2. 2
    Merge to main

    Triggers the deploy workflow

  3. 3
    Approval

    Environment lab requires one reviewer

  4. 4
    Assume role via OIDC

    Trust matches environment:lab

  5. 5
    cdk deploy

    CloudFormation updates the stack

  6. 6
    Smoke test

    Upload sample, read result, fail on error

In depth

Creating the provider and the role

The provider URL is https://token.actions.githubusercontent.com and the audience (client ID) is sts.amazonaws.com. You create it once per account in the IAM console or with aws iam create-open-id-connect-provider. AWS now validates GitHub's tokens using its trusted certificate authorities, so you no longer need to manage a thumbprint by hand. The deploy role has a trust policy with Action sts:AssumeRoleWithWebIdentity, the provider as federated principal, and two conditions: aud must equal sts.amazonaws.com, and sub must match the allowed repository context. Use StringEquals for an exact sub where you can; StringLike with a wildcard is fine only when the wildcard cannot widen the repository part. The role's permission policy should allow only what the deploy needs: with the CDK, permission to assume the bootstrap deploy roles is usually enough.

  • Provider URL: https://token.actions.githubusercontent.com
  • Audience: sts.amazonaws.com
  • Condition on aud and sub, always both
  • Separate roles for validate and deploy

Getting the sub claim right

The sub claim's format depends on what triggered the job. A push to a branch gives repo:example-org/file-service:ref:refs/heads/main. A tag gives ref:refs/tags/v1.2.0. A pull request gives repo:example-org/file-service:pull_request. A job that names an environment gets repo:example-org/file-service:environment:lab, and the branch no longer appears. This is the most common cause of Not authorized to perform sts:AssumeRoleWithWebIdentity errors: the trust policy expects one shape and the token has another. To see the real value, print the claims in a test job or check the CloudTrail event for the failed AssumeRoleWithWebIdentity call. Organisations can customise the sub template through GitHub's API, which changes the format again, so check before copying an example.

MapWhich sub claim does my job send?
Job trigger and settings
  • Push to branch, no environment
    • repo:example-org/file-service:ref:refs/heads/main
  • Job with environment: lab
    • repo:example-org/file-service:environment:lab
  • Pull request
    • repo:example-org/file-service:pull_request
  • Tag push
    • repo:example-org/file-service:ref:refs/tags/v1.2.0

Approvals and branch limits with environments

A GitHub environment, such as lab or prod, is configured in the repository settings. It can require named reviewers before a job that uses it starts, add a wait timer, and restrict which branches or tags may deploy to it. Combine this with the AWS side: trust only the environment sub claim for the deploy role. Then the only way to get deploy credentials is to run a job in that environment, which only main can do and only after approval. Pull request jobs should use a different, read-only role, or none, because code in a pull request is not yet reviewed. Never give pull requests from forks access to deploy roles.

  • Required reviewers pause the job until approved
  • Deployment branch rules limit who can target the environment
  • Trust only environment:prod for the prod role
  • Pull requests: read-only role or no AWS access

Validation before and smoke tests after

Validation catches problems before anything changes: formatting, unit tests, cdk synth (does the app even build a template), cdk-nag or cfn-lint for policy checks, and a cdk diff attached to the run for reviewers. Smoke tests prove the deploy worked: upload a tiny synthetic file to the lab bucket, wait for the worker to write a DynamoDB item, and check the result. Keep smoke tests short, under a couple of minutes, and make them clean up after themselves. If a smoke test fails, the job fails, and the next step is a rollback (redeploy the previous commit or shift an alias back) rather than a hand fix in the console.

Learn it in 5 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Create the GitHub OIDC provider~40m
  2. 2
    Restrict the role trust to a branch~40m
  3. 3
    Assume the role in the workflow~40m
  4. 4
    Require approval for an environment~40m
  5. 5
    Run smoke tests after deploy~40m

Key points

  • No long-lived AWS access keys are stored in GitHub secrets.
  • The role trust policy must check both aud (sts.amazonaws.com) and sub (repository plus branch or environment).
  • A job that uses a GitHub environment gets a sub claim naming the environment, not the branch.
  • Environment protection rules add required reviewers before the deploy job can start.
  • Smoke tests after deploy give quick evidence that the release actually works.

Key terms

Tap a card to flip it. 0/6 known

Examples

yaml
permissions:
  id-token: write
  contents: read
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: lab
    steps:
      - uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: arn:aws:iam::111122223333:role/gha-deploy-lab
          aws-region: eu-west-1
      - run: aws sts get-caller-identity

The minimum for OIDC: id-token write permission and the role to assume.

A trust policy for the lab deploy role

Goal: only jobs running in the lab environment of example-org/file-service can assume the role. Step 1: create the OIDC provider in account 111122223333. Step 2: create the role with this trust policy. Step 3: attach a permissions policy limited to the CDK bootstrap roles. Evidence: a job in environment lab succeeds with aws sts get-caller-identity showing assumed-role/gha-deploy-lab; a job in another repository fails with AccessDenied. Rollback: delete the role and the provider.

json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "Federated": "arn:aws:iam::111122223333:oidc-provider/token.actions.githubusercontent.com" },
    "Action": "sts:AssumeRoleWithWebIdentity",
    "Condition": {
      "StringEquals": {
        "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
        "token.actions.githubusercontent.com:sub": "repo:example-org/file-service:environment:lab"
      }
    }
  }]
}

Deploy job with a smoke test

This job runs after tests pass on main. It waits for approval via the lab environment, assumes the role, deploys, then uploads a synthetic file and checks that the worker wrote a result. Evidence: the job log shows the caller identity, CloudFormation UPDATE_COMPLETE and the smoke test item. If the smoke test fails, the job goes red and you roll back by re-running the workflow on the previous good commit.

yaml
deploy:
  needs: test
  if: github.ref == 'refs/heads/main'
  environment: lab
  permissions: { id-token: write, contents: read }
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v4
    - uses: aws-actions/configure-aws-credentials@v6
      with: { role-to-assume: "arn:aws:iam::111122223333:role/gha-deploy-lab", aws-region: eu-west-1 }
    - run: npm ci && npx cdk deploy FileServiceLab --require-approval never
    - run: |
        aws s3 cp tests/sample.txt s3://example-lab-uploads/smoke-$GITHUB_RUN_ID.txt
        sleep 20
        aws dynamodb get-item --table-name file-results --key '{"file_id":{"S":"smoke-'$GITHUB_RUN_ID'.txt"}}' | grep -q processed

Quick reference

  • Workflow needs permissions: id-token: write to request an OIDC token.
  • Provider: token.actions.githubusercontent.com; audience: sts.amazonaws.com.
  • Trust policy: condition on both aud and sub, prefer StringEquals.
  • Jobs with an environment send sub repo:ORG/REPO:environment:NAME.
  • Use aws-actions/configure-aws-credentials with role-to-assume and aws-region.
  • Pull requests get a read-only role or none; deploy roles only for main and environments.
  • Debug AssumeRoleWithWebIdentity failures by checking the sub claim in CloudTrail.

Common mistakes

  • A trust policy with sub set to repo:example-org/* lets any repository in the organisation deploy.
  • Forgetting id-token: write, which gives a could not fetch token error.
  • Matching sub on ref:refs/heads/main while the job uses an environment, so every assume fails.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A deploy job fails with Could not load credentials and no token. What is most likely missing?

Question 2

2Your trust policy allows sub repo:example-org/file-service:ref:refs/heads/main, but the job uses environment: lab and gets AccessDenied. Why?

Question 3

3Which trust condition is too broad?

Question 4

4How long do credentials from AssumeRoleWithWebIdentity last by default with configure-aws-credentials?

Question 5

5What is the job of a smoke test after deploy?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write the sub claim value for: a push to main without an environment, a job with environment prod, and a pull request, for repository example-org/file-service.

  2. Exercise 2medium

    Create the GitHub OIDC provider and a deploy role in a sandbox account from the CLI, then confirm the role trust with get-role.

  3. Exercise 3hard

    Design a two-workflow setup: PR checks with no AWS deploy rights, and a main-branch deploy to lab with approval, smoke test and an automatic redeploy of the previous commit on failure.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I still need AWS access keys in GitHub secrets?
No. With OIDC, you store only the role ARN, which is not secret. Delete old access keys once the OIDC role works.
Can I use OIDC with GitHub Enterprise Server or other CI tools?
Yes, the same pattern works with any CI that issues OIDC tokens, such as GitLab. The provider URL and claim names differ, so read that tool's docs.
Why does my role assume work in one workflow but not another?
The sub claim differs between triggers and environments. Compare the claim each job sends with the trust policy conditions.

Go deeper

ShareXLinkedInWhatsApp