Skip to main content

OIDC for CI/CD

OIDC for CI/CD lets a pipeline exchange a short-lived identity token for temporary cloud credentials, so no long-lived access keys need to be stored as secrets.

Understanding OIDC for CI/CD

What Is OIDC in CI/CD and Why Replace Stored Keys

OpenID Connect (OIDC) lets your CI platform prove who a job is without a stored password. GitHub Actions or GitLab CI issues a signed token for each job, and the cloud provider (AWS, GCP, Azure) exchanges it for temporary credentials limited to a role you configured. Static access keys never expire, need manual rotation, and can leak in logs. OIDC tokens and the credentials they produce expire in minutes to an hour.

Token Exchange Flow

sequenceDiagram participant J as CI job participant G as CI platform participant A as AWS STS J->>G: Request OIDC token G-->>J: Signed token (repo, branch) J->>A: AssumeRoleWithWebIdentity A-->>J: Temporary credentials J->>A: Push to ECR, deploy

The token carries claims such as the issuer, the audience, and a subject like repo:razorpay/payment-api:ref:refs/heads/main. The cloud trust policy checks those claims before issuing credentials.

GitHub Actions to AWS

YAML
permissions:
id-token: write # required to request the 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::123456789:role/github-deploy-role
aws-region: ap-south-1
role-session-name: github-actions-${{ github.run_id }}
- run: aws sts get-caller-identity

IAM Trust Policy

JSON
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789: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:razorpay/payment-api:ref:refs/heads/main"
}
}
}
]
}

Use StringLike with a wildcard only where several branches should be allowed, for example staging roles. Keep production restricted to the main branch or a named environment.

GitLab CI to AWS

YAML
deploy:
image: amazon/aws-cli:latest
id_tokens:
AWS_OIDC_TOKEN:
aud: sts.amazonaws.com
script:
- >
CREDS=$(aws sts assume-role-with-web-identity
--role-arn arn:aws:iam::123456789:role/gitlab-deploy-role
--role-session-name gitlab-ci-$CI_JOB_ID
--web-identity-token "$AWS_OIDC_TOKEN"
--query 'Credentials' --output json)
- export AWS_ACCESS_KEY_ID=$(echo "$CREDS" | jq -r .AccessKeyId)
- export AWS_SECRET_ACCESS_KEY=$(echo "$CREDS" | jq -r .SecretAccessKey)
- export AWS_SESSION_TOKEN=$(echo "$CREDS" | jq -r .SessionToken)
- aws sts get-caller-identity

One-Time AWS Setup

Bash
# Create the GitHub OIDC provider in the account
aws iam create-open-id-connect-provider \
--url https://token.actions.githubusercontent.com \
--client-id-list sts.amazonaws.com
# Create the role and attach least-privilege permissions
aws iam create-role --role-name github-deploy-role \
--assume-role-policy-document file://trust-policy.json

Older guides pass a thumbprint list. AWS now validates GitHub's certificate itself, so check the current AWS documentation for your CLI version.

Troubleshooting

Symptom Check What to Look For
Not authorized to assume role Trust policy sub Repo, branch, or environment mismatch
Cannot get token Workflow permissions Missing id-token: write
Invalid principal OIDC provider Provider not created in the account
Credentials expire mid-job Role max session Raise the role's maximum duration
Tip

Use one role per purpose, such as ECR push and EKS deploy, each with its own narrow trust and permissions policy.

Remember

The trust policy decides which pipelines can assume the role. The permissions policy decides what they can do afterwards. Scope both.

Security

A sub condition that trusts a whole organisation lets any repository in it assume the role. Pin the repository and branch or environment.

Common Mistake

Creating the IAM role before the OIDC provider exists. The trust policy references the provider ARN, so create the provider first.

Frequently Asked Questions

What problem does OIDC solve compared with stored cloud keys?

Stored keys are long-lived, so a leak or former employee's access stays useful until someone rotates them. With OIDC the job gets a short-lived credential tied to a trust relationship, so there is nothing durable to steal.

What is the most common OIDC trust misconfiguration?

A subject condition that is too broad, such as every repo in an organisation. Restrict it to the specific repository and branch or environment that needs access.

Does OIDC work outside GitHub and AWS?

Yes. GitLab CI, CircleCI, and others issue OIDC tokens, and GCP workload identity federation and Azure federated credentials accept them in the same way.

Found a mistake, an outdated step or a broken link?