You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI执行aws sts assume-role-with-web-identity报InvalidIdentityToken错误

解决GitLab CI调用aws sts assume-role-with-web-identity的InvalidIdentityToken错误

错误原因

这个错误是因为AWS验证OIDC令牌的Issuer字段时,发现其未以https://开头。核心问题通常出在AWS IAM角色的信任策略配置或OIDC提供商的Issuer设置上,而非GitLab CI脚本本身。

解决方案

1. 修正IAM角色的信任策略

登录AWS IAM控制台,找到${ROLE_ARN}对应的目标角色,编辑其信任关系:

  • 确保Principal.Federated指向正确的GitLab OIDC提供商ARN(格式为arn:aws:iam::你的AWS账号ID:oidc-provider/git.mydomain.com)
  • 确保Condition中的aud值与GitLab CI配置的aud: https://git.mydomain.com完全一致
  • 确保Issuer相关条件(若存在)使用https://git.mydomain.com作为前缀

示例信任策略:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::123456789012:oidc-provider/git.mydomain.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "git.mydomain.com:aud": "https://git.mydomain.com"
        },
        "StringLike": {
          "git.mydomain.com:sub": "project_path:你的项目路径:*"
        }
      }
    }
  ]
}

2. 验证AWS OIDC提供商配置

在IAM控制台「身份提供商」页面,找到对应GitLab的OIDC提供商:

  • 检查提供商的Issuer地址是否为https://git.mydomain.com,如果不是,删除现有提供商并重新创建,创建时必须输入带https://的正确地址。

3. 确认GitLab CI配置无误

你的.gitlab-ci.yml中id_tokens的aud配置是正确的,无需修改脚本中的$GITLAB_OIDC_TOKEN变量(GitLab会自动注入合法的OIDC令牌)。

验证

修改完成后重新触发GitLab CI流水线,若aws sts get-caller-identity能正常返回身份信息,说明配置成功。

内容的提问来源于stack exchange,提问作者Eduardo Basílio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 11:52:23