Unable to execute AWS Pipeline Error: "An error occurred (AccessDenied) when calling the PutObject operation: Access Denied"

Unable to execute AWS Pipeline Error: "An error occurred (AccessDenied) when calling the PutObject operation: Access Denied"

一直在尝试按照此处的教程设置 AWS 管道:https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html

但管道不断失败并显示以下错误日志:

以下是我已经尝试过的一些操作:

  1. 已授予 S3 对与 Cloud Formation 和代码管道服务角色关联的“cfn-lambda-pipeline”角色的完全访问权限。

  1. 允许 public ACL 访问 S3 存储桶。

下面是我的buildspec.yml

version: 0.2
phases:
  install:
    runtime-versions:
        nodejs: 12
  build:
    commands:
      - npm install
      - export BUCKET=xx-test
      - aws cloudformation package --template-file template.yaml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
artifacts:
  type: zip
  files:
    - template.yml
    - outputtemplate.yml

下面是我的template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  helloWorld
  DZ Bank API Gateway connectivity helloWorld
  
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

"cfn-lambda-pipeline" role associated with Cloud Formation and Code Pipeline Service Role.

S3权限应该与CodeBuild(CB)关联,因为CB要运行buildspec.yml。因此CB需要能够访问S3。

根据 Update the build stage role 部分中链接的教程,AmazonS3FullAccess 应该添加 codebuild-lamba-pipeline-build-service-role 角色,而不是 cfn-lambda-pipeline 也不是 CodePipeline 的角色。

该错误实际上与 CodeBuild 相关,与 CodePipeline 无关。 CodeBuild 似乎没有对其附加服务角色的有效权限。

通过执行以下操作,您可以从控制台找到附加的服务角色:

  • 转到 CodeBuild 控制台
  • 点击左侧菜单中的“构建项目”
  • 单击您正在使用的构建项目旁边的单选按钮,然后在顶部菜单中单击“编辑”和select,然后单击“编辑源”选项。
  • 页面底部将是标题为“服务角色权限”的部分,其下方是 Arn。

这个 IAM 角色需要被授予它所需的权限(在您的例子中是“s3:PutObject”),如果它们还没有的话。

AWS 在 Create a CodeBuild service role 文档中提供了完整的政策。