- Add the identity provider
- Configure a role and trust
- Retrieve temporary credentials
- Working example
- Troubleshooting
Configure OpenID Connect in AWS to retrieve temporary credentials
In this tutorial, we’ll show you how to use a GitLab CI/CD job with a JSON web token (JWT) to retrieve temporary credentials from AWS without needing to store secrets. To do this, you must configure OpenID Connect (OIDC) for ID federation between GitLab and AWS. For background and requirements for integrating GitLab using OIDC, see Connect to cloud services.
To complete this tutorial:
Add the identity provider
Create GitLab as a IAM OIDC provider in AWS following these .
Include the following information:
After you create the identity provider, configure a .
For the full list of supported filtering types, see Connect to cloud services.
After the role is created, attach a policy defining permissions to an AWS service (S3, EC2, Secrets Manager).
After you configure the OIDC and role, the GitLab CI/CD job can retrieve a temporary credential from .
See this Troubleshooting
This error can occur for multiple reasons:
https://gitlab.com
or http://gitlab.example.com
.
https://gitlab.com
or http://gitlab.example.com
.
https://
.
Configure a role and trust
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::AWS_ACCOUNT:oidc-provider/gitlab.example.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"gitlab.example.com:sub": "project_path:mygroup/myproject:ref_type:branch:ref:main"
}
}
}
]
}
Retrieve temporary credentials
assume role:
script:
- >
STS=($(aws sts assume-role-with-web-identity
--role-arn ${ROLE_ARN}
--role-session-name "GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}"
--web-identity-token $CI_JOB_JWT_V2
--duration-seconds 3600
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]'
--output text))
- export AWS_ACCESS_KEY_ID="${STS[0]}"
- export AWS_SECRET_ACCESS_KEY="${STS[1]}"
- export AWS_SESSION_TOKEN="${STS[2]}"
- aws sts get-caller-identity
CI_JOB_JWT_V2
: Predefined variable.
ROLE_ARN
: The role ARN defined in this step.
Working example
An error occurred (AccessDenied) when calling the AssumeRoleWithWebIdentity operation: Not authorized to perform sts:AssumeRoleWithWebIdentity