Interview Prep/CI/CD/GitHub Actions and GitLab CI in Production

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
All CI/CD questions Roadmap: GitLab CI/CD

More on GitHub Actions and GitLab CI in Production