AuthenticationGitLab · AWS
Keyless deploys from GitLab CI to AWS with OIDC
A GitLab CI/CD job can retrieve temporary credentials from AWS with OpenID Connect, and no AWS secret is stored in GitLab. The job presents an ID token, and AWS returns credentials that last one hour by default.

How the exchange works
A job that declares id_tokens gets an ID token as a CI/CD variable. The ID token is a JSON web token generated by GitLab CI/CD, and its claims include the project path, the ref and the pipeline ID. The job calls the AWS Security Token Service (STS) with that token. AWS verifies the token with GitLab's public key, validates the audience and the conditions on the role, and returns a temporary credential.
CI_JOB_JWT and CI_JOB_JWT_V2 were removed in GitLab 17.0. GitLab's documentation says to use ID tokens.
1. Add GitLab as an identity provider
Create GitLab as an IAM OIDC provider in AWS. GitLab's tutorial points to AWS's instructions for this. With the AWS CLI, the command is create-open-id-connect-provider:
aws iam create-open-id-connect-provider \
--url https://gitlab.com \
--client-id-list sts.amazonaws.com--url is the address of your GitLab instance, and it must be publicly accessible. --client-id-list holds the audience. GitLab's page names two values for the audience, sts.amazonaws.com and the GitLab instance URL, and calls the instance URL semantically misleading. This guide uses sts.amazonaws.com.
The thumbprint list is optional. When it is left out, IAM retrieves and uses the top intermediate certificate authority thumbprint of the provider's server certificate.
2. Create the role and trust policy
The role is a web identity role, and the action in its trust policy is sts:AssumeRoleWithWebIdentity. This is GitLab's documented trust policy for GitLab.com. Replace the account ID, the project path and the two IDs with your own, and save it as trust-policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111122223333: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"
}
}
}
]
}The policy pins trust to one group, one project and one branch. Create the role under a name of your choice:
aws iam create-role --role-name gitlab-deploy \
--assume-role-policy-document file://trust-policy.jsonAfter the role is created, attach a policy that defines its permissions to the AWS services the job uses.
How the sub claim limits access
By default the sub claim has the format project_path:{group}/{project}:ref_type:{type}:ref:{branch_name}. GitLab documents four filters:
- any branch of a project:
project_path:mygroup/myproject:ref_type:branch:ref:* - one project, main branch:
project_path:mygroup/myproject:ref_type:branch:ref:main - all projects under a group:
project_path:mygroup/*:ref_type:branch:ref:main - a Git tag:
project_path:mygroup/*:ref_type:tag:ref:1.0
A condition with a wildcard goes under StringLike. GitLab lists StringEquals used with a wildcard among the causes of the error Not authorized to perform sts:AssumeRoleWithWebIdentity.
On GitLab.com, AWS supports additional condition keys, among them namespace_id and project_id. AWS and GitLab both recommend conditions on these IDs, because trust policies that reference them are not affected by group or project renames. project_id remains the same for the entire lifetime of a project, across renames and transfers. namespace_id changes when the project is transferred to a different namespace.
These additional keys are available only for the gitlab.com provider. For GitLab Self-Managed and GitLab Dedicated, AWS supports only sub and aud as condition keys.
3. Request the token in the job
This is the job from GitLab's tutorial, with two changes. aud holds the audience from step 1. The export command is folded over three lines, which YAML joins back into the single line GitLab documents.
assume role:
id_tokens:
GITLAB_OIDC_TOKEN:
aud: sts.amazonaws.com
script:
# this is split out for correct exit code handling
- >
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-identityaud is a required keyword under each token name. AWS requires the aud claim to match the audience set on the IAM OIDC provider. ROLE_ARN is the ARN of the role from step 2. The script runs the aws command, so run the job where the AWS CLI is installed.
--duration-seconds ranges from 900 seconds to the role's maximum session duration, and its default is 3600. The last command, aws sts get-caller-identity, returns details about the role whose credentials made the call.
Self-managed GitLab instances
For GitLab Self-Managed, the provider URL is the instance address, and the condition key takes its host name, as in gitlab.example.com:sub.
If the instance is not publicly accessible, OpenID Connect in AWS is not possible by default. GitLab 18.1 introduced a workaround for GitLab Self-Managed: host the OpenID configuration and the public keys at a publicly available location, such as S3 files, and set a custom issuer with ci_id_tokens_issuer_url. GitLab's warning: if that S3 bucket is left unsecured, any cloud account tied to the OpenID Connect identity could be taken over.
If the job is refused
GitLab's troubleshooting section gives these causes for Not authorized to perform sts:AssumeRoleWithWebIdentity:
- the project has not been configured to use OIDC with GitLab
- the role's restrictions exclude the branch or tag the job runs on
StringEqualsis used where a wildcard needsStringLike
To compare the condition with the token, read two claims. ref_type is branch or tag. In a merge request pipeline, ref is the source branch.
For Couldn't retrieve verification key from your identity provider, GitLab lists an instance whose OpenID configuration and jwks_uri are inaccessible from the public internet, and a custom firewall that blocks the requests.
OIDC removes the need to store secrets in the GitLab group or project. When the job works, delete the AWS access keys stored there.
Sources
- Configure OpenID Connect in AWS to retrieve temporary credentials
- Connect to cloud services
- OpenID Connect (OIDC) Authentication Using ID Tokens
- CI/CD YAML syntax reference
- Create an OpenID Connect (OIDC) identity provider in IAM
- IAM and AWS STS condition context keys
- assume-role-with-web-identity
- create-open-id-connect-provider
Want a second pair of eyes on yours? We do this work for a living.
More guides

Keyless deploys to AWS and Google Cloud with OIDC
Authentication · 2 min read
The trust policy for AWS and the provider setting for Google Cloud, with the workflow for each.

AWS STS and AssumeRole: how short-lived credentials work
Authentication · 4 min read
Trust policy, permissions policy, session duration, the external ID and the one-hour limit on role chaining.

OAuth 2.0 flows: which grant fits which client
Authentication · 4 min read
Which OAuth 2.0 grant to use for a web app, a single-page app, a mobile app, a CLI and a service.