How do you set up OIDC from GitLab CI to AWS?
IntermediateProductionGitHub Actions and GitLab CI in Production
Answer
Create an IAM OIDC provider for your GitLab instance (https://gitlab.com or your self-managed URL), a role trusting it with a condition on sub (such as project_path:acme/shop-api:ref_type:branch:ref:main), then request an ID token in the job and exchange it.
deploy:
id_tokens:
GITLAB_OIDC_TOKEN:
aud: sts.amazonaws.com
script:
- >
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s"
$(aws sts assume-role-with-web-identity
--role-arn $ROLE_ARN --role-session-name gitlab-$CI_JOB_ID
--web-identity-token $GITLAB_OIDC_TOKEN --duration-seconds 3600
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' --output text))
- aws sts get-caller-identity
More on GitHub Actions and GitLab CI in Production
How do you organise reusable workflows for an organisation on GitHub?IntermediateHow do you reduce GitHub Actions minutes cost on private repos?IntermediateHow do you secure self-hosted runners?AdvancedHow do you design the GitHub Actions OIDC trust policy for AWS safely?AdvancedHow do you handle approvals and deployments across environments in GitHub Actions?IntermediateHow do you handle required checks when workflows use path filters or matrices?Advanced