1. Home
  2. Articles
  3. Guide

Guide

Deploying to AWS from GitHub Actions without access keys

Long-lived AWS access keys stored in GitHub are one of the most common ways cloud accounts get compromised. OpenID Connect (OIDC) replaces them with short-lived credentials that GitHub requests for each run.

Short answer: Add GitHub (token.actions.githubusercontent.com) as an OIDC identity provider in IAM, create a role whose trust policy checks the audience and your repository’s subject, give the workflow id-token: write permission, and use aws-actions/configure-aws-credentials. Each run then gets short-lived credentials, and no access keys are stored anywhere.

01 · Why

Why drop access keys

An access key stored as a GitHub secret works until someone copies it, a workflow leaks it in a log, or the person who created it leaves. It doesn’t expire unless someone rotates it, and it usually has more permissions than the pipeline needs.

With OIDC, each workflow run asks GitHub for a signed token that says which repository, branch or environment it is running from. AWS checks that token against rules you set, and hands back temporary credentials for one IAM role. Nothing long-lived is stored anywhere.

02 · Step 1

Step 1: add GitHub as an identity provider in AWS

In IAM, add an OpenID Connect identity provider with:

  • Provider URL: https://token.actions.githubusercontent.com
  • Audience: sts.amazonaws.com

You do this once per AWS account. The console fills in a certificate thumbprint for you, but IAM verifies GitHub’s certificate against its own library of trusted root certificate authorities, and only falls back to the thumbprint if it can’t. You don’t need to maintain it by hand.

03 · Step 2

Step 2: create a role that only your repository can use

The role’s trust policy is where the security lives. It checks the token’s audience and its subject, which names the repository and the branch or environment:

trust-policy.json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "Federated": "arn:aws:iam::ACCOUNT_ID: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:ref:refs/heads/main"
      }
    }
  }]
}

The subject depends on what triggered the job:

JobSubject (sub)
Runs on a branchrepo:your-org/your-repo:ref:refs/heads/main
Runs on a tagrepo:your-org/your-repo:ref:refs/tags/v1.2.0
Uses a GitHub environmentrepo:your-org/your-repo:environment:production
Triggered by a pull requestrepo:your-org/your-repo:pull_request

If a job names an environment, the subject is the environment form, even when it runs on main. Tying production to a GitHub environment is usually the better choice, because environments can require a reviewer before the job runs.

The same role in Terraform, reading the provider you created in step 1:

github-oidc.tf
data "aws_iam_openid_connect_provider" "github" {
  url = "https://token.actions.githubusercontent.com"
}

resource "aws_iam_role" "deploy_prod" {
  name = "github-actions-deploy-prod"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect    = "Allow"
      Principal = { Federated = data.aws_iam_openid_connect_provider.github.arn }
      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"
        }
      }
    }]
  })
}

Don’t leave the subject open.

Without a sub condition, or with a wildcard like repo:your-org/*, any repository or branch that matches can assume the role, including a pull request branch.

04 · Step 3

Step 3: give the role only what the pipeline does

Attach a policy for exactly what the job needs: for a static site, writing to one bucket and invalidating one CloudFront distribution. Use separate roles for staging and production, so a staging workflow can never touch production.

05 · Step 4

Step 4: request the token in your workflow

The workflow needs permission to request an OIDC token, and then uses the official action to assume the role:

.github/workflows/deploy.yml
permissions:
  id-token: write   # lets the job request an OIDC token
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::ACCOUNT_ID:role/github-actions
          aws-region: ap-southeast-2
      - run: aws s3 sync dist/ s3://your-bucket/

Once that works, delete the old access keys from GitHub and deactivate them in IAM. Before deleting them, check in IAM when each key was last used, so you don’t break another pipeline that shares it.

06 · Troubleshooting

When it doesn’t work

SymptomUsual cause
The action can’t get a token at allThe workflow or job is missing permissions: id-token: write. Setting permissions at job level replaces the workflow-level list, so add it where the job is.
Not authorized to perform sts:AssumeRoleWithWebIdentityThe token’s subject doesn’t match the trust policy. Most often the job uses an environment, so the subject is …:environment:production rather than …:ref:refs/heads/main.
Works on main, fails on pull requestsPull request jobs have the subject …:pull_request. That’s usually correct: pull requests shouldn’t be able to deploy. Give them a separate, read-only role if they need AWS at all.
Access denied on a specific actionThe token worked, and the role’s permissions policy is missing that action. Check CloudTrail for the denied call.
07 · Mistakes

Common mistakes

  • A wildcard subject. repo:your-org/* lets every repository in the organisation assume the role, including ones created later.
  • Checking only the audience. The audience is the same for every GitHub repository in the world. Without a subject condition, the role is far more open than it looks.
  • One role for staging and production. Use separate roles, ideally in separate accounts, so a staging workflow can never touch production.
  • AdministratorAccess on the deploy role. Short-lived credentials limit how long a leak lasts, not what it can do. Scope the permissions to the job.
08 · Example

A working example

This website deploys this way: staging and production workflows both request a token with id-token: write and assume a role with aws-actions/configure-aws-credentials. The workflows use no stored AWS keys. See how devopsplant.com.au is built and deployed.

09 · Sources

Sources

See also AWS security consulting, faster deployments and a minimum AWS security baseline.

Next step

Still have access keys in your pipelines?

On a 30-minute call we’ll look at where they are and what replacing them would take.