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

GitHub触发AWS CodeBuild推送ECR镜像时提示区域缺失

AWS CodeBuild推送ECR时提示区域未定义的报错问题

我尝试通过GitHub推送触发AWS CodeBuild,将镜像推送到ECR,但每次构建都卡在登录ECR步骤报错。已经给CodeBuild的IAM角色配置了足够高的权限,但始终提示区域未定义。

以下是我的buildspec.yml文件内容:

version: 0.2
phases:
  install:
    runtime-versions:
      docker: 18
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin ****.dkr.ecr.us-east-2.amazonaws.com
      - REPOSITORY_URI=****.dkr.ecr.us-east-2.amazonaws.com/****
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t $REPOSITORY_URI:latest .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - echo Writing image definitions file...
      - printf '[{\"name\":\"****\",\"imageUri\":\"%s\"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
  files: imagedefinitions.json

相关报错截图:
报错截图1
报错截图2

解决建议

  • 确认CodeBuild项目所在区域为us-east-2,如果项目在其他区域,要么迁移项目区域,要么修改buildspec中的区域参数匹配ECR所在区域。
  • 在buildspec.yml中添加环境变量定义默认区域,避免aws命令无法识别区域:
    version: 0.2
    env:
      variables:
        AWS_DEFAULT_REGION: us-east-2
    phases:
      # 后续原有phases内容不变
    
  • 检查aws ecr get-login-password命令中的--region参数是否拼写正确,确保没有空格或拼写错误。
  • 再次验证IAM角色权限,确保包含ECR操作的必要权限:ecr:GetAuthorizationToken、ecr:PutImage、ecr:InitiateLayerUpload等相关权限,避免因权限不足导致的隐性报错。

内容的提问来源于stack exchange,提问作者Wx_Trader

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 05:38:12