Skip to content
DevOpsCommand

Deploying to AWS from GitHub Actions Without Long-Lived Access Keys

Replace stored AWS access keys in your CI with short-lived credentials using OpenID Connect.

By DevOpsCommand Engineering · · 2 min read

Deploying to AWS from GitHub Actions Without Long-Lived Access Keys — illustration

Storing an AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as repository secrets works, but those keys never expire, are easy to over-privilege and are a high-value target if your CI is ever compromised. GitHub Actions supports OpenID Connect (OIDC), which lets a workflow exchange a signed identity token for temporary AWS credentials that last only for the job.

How it works

  1. GitHub issues the workflow a signed JSON Web Token describing the repository, branch and environment.
  2. AWS STS verifies that token against an IAM OIDC identity provider you register once.
  3. If the token's claims match the trust policy of an IAM role, STS returns temporary credentials for that role.

1. Register GitHub as an identity provider

In IAM, add an OpenID Connect provider with the URL https://token.actions.githubusercontent.com and the audience sts.amazonaws.com.

2. Create a role with a tight trust policy

{
  "Effect": "Allow",
  "Principal": { "Federated": "arn:aws:iam::123456789012: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:your-org/your-repo:environment:production"
    }
  }
}

The sub condition is the important part. Restrict it to a specific repository and, ideally, a protected GitHub environment. A wildcard such as repo:your-org/* lets any repository in the organisation assume the role.

3. Request the token in your workflow

permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    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: eu-west-1
      - run: aws sts get-caller-identity

The id-token: write permission is required; without it the workflow cannot request an OIDC token.

4. Remove the old keys

Once deployments succeed with the role, delete the repository secrets and deactivate the IAM user's access keys. Leaving them in place means you still carry the original risk.

Further hardening

  • Grant the role only the permissions the deployment needs.
  • Require reviewers on the production environment in GitHub.
  • Pin third-party actions to a full commit SHA.

This pattern is part of our Secure CI/CD Pipelines work. Azure and Google Cloud support the same approach through workload identity federation.

Let's talk

Want an engineer to set this up for you?

A 30-minute call. No pressure, no sales pitch — just an engineer looking at what you actually have and telling you what it needs.