How do I use OIDC ID tokens in GitLab CI to authenticate with AWS?

By Melih Toksari, developer of MergeBell · Updated · 6 min read

Short answer

Add GitLab as an OIDC identity provider in AWS IAM, create a role whose trust policy allows sts:AssumeRoleWithWebIdentity for your project's sub claim, then give the job an id_tokens entry with the matching aud and call aws sts assume-role-with-web-identity with that token. The job gets temporary credentials, so no AWS access key is stored in GitLab. ID tokens work on every tier.

Declare an ID token in the job with id_tokens, then trade it for temporary AWS credentials with aws sts assume-role-with-web-identity:

assume role:
  id_tokens:
    GITLAB_OIDC_TOKEN:
      aud: https://gitlab.example.com
  script:
    - >
      aws_sts_output=$(aws sts assume-role-with-web-identity
      --role-arn ${ROLE_ARN}
      --role-session-name "GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}"
      --web-identity-token ${GITLAB_OIDC_TOKEN}
      --duration-seconds 3600
      --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]'
      --output text)
    - export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" $aws_sts_output)
    - aws sts get-caller-identity

No AWS access key is stored in GitLab. AWS trusts the token because you registered your GitLab instance as an OIDC identity provider and wrote a role that only accepts tokens from your project. ID tokens are available on the Free, Premium and Ultimate tiers, on GitLab.com, Self-Managed and Dedicated.

What is a GitLab ID token?

An ID token is a JSON web token (JWT) that GitLab CI/CD generates for a job that asks for one with the id_tokens keyword. You name the token, set its aud (audience), and use it in script, before_script or after_script:

job_with_id_tokens:
  id_tokens:
    FIRST_ID_TOKEN:
      aud: https://first.service.com
    SECOND_ID_TOKEN:
      aud: https://second.service.com
  script:
    - first-service-authentication-script.sh $FIRST_ID_TOKEN
    - second-service-authentication-script.sh $SECOND_ID_TOKEN

A job can hold several tokens with different audiences. A service can reject any token whose aud isn’t its own, so one leaked token can’t be replayed against another service.

The token is encoded with RS256 and signed with a dedicated private key. It expires at the job’s timeout if the job sets one, or after 5 minutes if it doesn’t. The older CI_JOB_JWT_V2 variable was removed in GitLab 17.0; id_tokens replaces it.

Which claims can a trust policy check?

The claims describe the job: which project, which ref, who started it. These are the ones trust policies lean on most:

Claim What it holds
iss The domain of the GitLab instance
sub By default project_path:{group}/{project}:ref_type:{type}:ref:{branch_name}
aud What you set in id_tokens; the instance domain by default
project_id, namespace_id IDs of the project and namespace running the job
ref, ref_type, ref_protected The Git ref, branch or tag, and whether it’s protected
environment, environment_protected Present when the job specifies an environment
pipeline_source How the pipeline started
user_login, user_email The user who ran the job
runner_environment gitlab-hosted or self-hosted

GitLab 19.5 added source_project_id, source_project_path, source_namespace_id and source_namespace_path, which in a merge request pipeline point at the merge request’s source project.

To see exactly what your job sends, decode the token in the job:

echo $GITLAB_OIDC_TOKEN | cut -d '.' -f2 | base64 -d | jq .

How do I set up AWS to trust GitLab?

Two things in AWS IAM: an OIDC identity provider, and a role.

1. Add the identity provider. Create GitLab as an IAM OIDC provider with:

  • Provider URL: the address of your GitLab instance, such as https://gitlab.com. It must be publicly accessible.
  • Audience: the logical name of the service the token is meant for. GitLab’s docs note this is often sts.amazonaws.com or your instance URL; whatever you choose must match the aud in the job’s id_tokens.

2. Create the role and its trust policy. Set the action to sts:AssumeRoleWithWebIdentity and add conditions that limit which jobs may assume it. This example pins the role to one group, project and branch on GitLab.com:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::AWS_ACCOUNT:oidc-provider/gitlab.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "gitlab.com:sub": "project_path:mygroup/myproject:ref_type:branch:ref:main",
          "gitlab.com:namespace_id": "12345",
          "gitlab.com:project_id": "67890"
        }
      }
    }
  ]
}

Then attach a permissions policy to the role for the AWS services the job needs, store the role’s ARN as ROLE_ARN, and run the job at the top of this page.

Should the trust policy use the project path or the project ID?

Use both where you can. sub is path-based, so it changes if the group or project is renamed. project_id is globally unique and stays the same for the project’s whole life, including renames and transfers. namespace_id changes when the project is transferred to another namespace, which deliberately invalidates a policy pinned to it.

What AWS can check depends on where GitLab runs:

Condition key GitLab.com Self-Managed and Dedicated
sub, aud Yes Yes
namespace_id, project_id Yes No
user_id, user_login, user_email, user_access_level Yes No
ref_protected, pipeline_source Yes No

On Self-Managed and Dedicated, scope the policy with sub (for example gitlab.example.com:sub), optionally with aud. Don’t rely on user_login or user_email as the only condition, because a user can change them.

Why does the job fail to assume the role?

Read the error from the aws sts command in the job log; each one points somewhere different.

Error Cause Fix
Not authorized to perform sts:AssumeRoleWithWebIdentity The trust policy doesn’t match the token Compare the decoded sub with the policy; use StringLike, not StringEquals, with a wildcard
Couldn't retrieve verification key from your identity provider AWS can’t reach the instance’s .well_known URL or jwks_uri, or a firewall blocks it Make them publicly reachable, or use the non-public workaround below
Could not connect to openid configuration of provider The instance’s certificate chain is out of order or has extra certificates Fix the chain: domain, intermediate, then root
401 from the external service A deprecated token, a mismatched aud, or no id_tokens block in the job Declare id_tokens and match the audience
ID token issuance is disabled in CI because a project path in the sub claim was previously used by a different project The project path was used by another project before Set ci_id_token_sub_claim_components to start with project_id, then update the trust policy

For the last one, the projects API takes "ci_id_token_sub_claim_components": ["project_id", "ref_type", "ref"], and the subject becomes project_id:<id>:ref_type:<type>:ref:<ref>. GitLab blocks issuance here so a new project can’t inherit the trust an earlier project of the same path had.

A branch restriction is the usual cause of the first error: a role pinned to ref:main rejects the same job on a feature branch. When a deploy fails this way, MergeBell shows the failed job’s log on your phone, so you can read the STS error and retry the job from the right branch.

What if my GitLab instance isn’t public?

AWS has to fetch your instance’s OpenID configuration and public keys, so a private instance doesn’t work by default. Putting /.well-known/openid-configuration behind a separate public hostname doesn’t work either, because the issuer and jwks_uri values inside it still point at the instance URL.

The documented workaround is to host both files yourself at a public location such as an S3 bucket:

  1. Copy /.well-known/openid-configuration and edit its issuer: and jwks_uri: to the public location.

  2. Copy the keys from /oauth/discovery/keys to that location.

  3. Set the issuer for ID tokens, for a Linux package install in /etc/gitlab/gitlab.rb:

    gitlab_rails['ci_id_tokens_issuer_url'] = '<public_url_with_openid_configuration_and_keys>'
  4. Reconfigure GitLab, then run the ci:validate_id_token_configuration Rake task.

GitLab flags this as an advanced option: a badly secured bucket could let someone take over every cloud account that trusts the provider.

Checklist before the first deploy

  • IAM OIDC provider created with your instance URL
  • Provider audience equals the job’s aud
  • Role action is sts:AssumeRoleWithWebIdentity
  • Trust policy conditions match the decoded sub (and IDs on GitLab.com)
  • Wildcards use StringLike
  • ROLE_ARN holds the role’s ARN
  • aws sts get-caller-identity prints the role in the job log
  • Old long-lived AWS keys removed from CI/CD variables

Frequently asked questions

What is an ID token in GitLab CI/CD?

A JSON web token that GitLab generates for a job when the job declares the id_tokens keyword. It is signed with RS256 and carries claims such as the project, ref and user, so a cloud provider or secrets store can verify which job is calling and grant it access without a stored credential.

How long does a GitLab ID token last?

Until the job's timeout if the job sets one, or 5 minutes if it doesn't.

Why do I get "Not authorized to perform sts:AssumeRoleWithWebIdentity"?

The role's trust policy doesn't match the token. Either OIDC isn't configured for the project, the role is restricted to another branch or tag, or the policy uses StringEquals with a wildcard where it needs StringLike.

Can I still use CI_JOB_JWT_V2?

No. CI_JOB_JWT_V2 was removed in GitLab 17.0. Declare id_tokens in the job instead; using a deprecated token is one of the listed causes of a 401 Unauthorized from the external service.

Does this work with a private GitLab Self-Managed instance?

Not by default, because AWS has to reach the instance's OpenID configuration and public keys. The workaround is to host copies of both at a public location such as an S3 bucket and set ci_id_tokens_issuer_url to that address.

Sources

Checked against these pages on 23 November 2026. GitLab changes quickly — if something here no longer matches, tell us and we will fix it.

Do this from your phone with MergeBell

Free for the whole review loop: the To-Do inbox, diffs and line comments, approve and merge, pipelines with retry, and push alerts for your To-Dos. Works with gitlab.com and self-managed GitLab 16.0+.

Get MergeBell for iPhoneGet MergeBell for Android

On the App Store for iOS 26 or later and on Google Play for Android 12 or later. An independent app, not affiliated with GitLab Inc.