AWS CodePipeline、CodeBuild、SAM和Lambda

5

我有一个关于Lambda(NodeJS)自动部署的管道,来自CodeCommit。

我的buildspec.yml文件:

version: 0.1
phases:
 install:
  commands:
    - npm install
    - aws cloudformation package --template-file samTemplate.yaml --s3-bucket codepipeline-551 --output-template-file outputSamTemplate.yaml
artifacts:
  type: zip
  files:
    - samTemplate.yaml
    - outputSamTemplate.yaml

我的samTemplate.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
  KashIoTLambda:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda.handler
      Runtime: nodejs8.10
      CodeUri: ./
      Description: ''
      MemorySize: 128
      Timeout: 3
      Role: 'arn:aws:iam::1234:role/abc-backend'
      Events:
        Api1:
          Type: Api
          Properties:
            Path: '/{proxy+}'
            Method: OPTIONS
        Api2:
          Type: Api
          Properties:
            Path: /MyResource
            Method: ANY
      Environment:
        Variables:
          REGION: ap-south-1

CodeBuild相关的角色所附加的策略提供了对所有资源和所有S3命令的访问权限。

但是,我从构建日志中得到以下错误信息

[Container] 2019/01/03 13:25:39 Running command npm install
added 122 packages in 3.498s
 [Container] 2019/01/03 13:25:44 Running command aws cloudformation package --template-file samTemplate.yaml --s3-bucket codepipeline-551 --output-template-file outputSamTemplate.yaml
 Unable to upload artifact ./ referenced by CodeUri parameter of ABCLambda resource.
An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
 [Container] 2019/01/03 13:25:46 Command did not exit successfully aws cloudformation package --template-file samTemplate.yaml --s3-bucket codepipeline-551 --output-template-file outputSamTemplate.yaml exit status 255
[Container] 2019/01/03 13:25:46 Phase complete: INSTALL Success: false
[Container] 2019/01/03 13:25:46 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: aws cloudformation package --template-file samTemplate.yaml --s3-bucket codepipeline-551 --output-template-file outputSamTemplate.yaml. Reason: exit status 255

你是否先使用了sam package命令,如下: sam package
--template-file template.yaml
--output-template-file packaged.yaml
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
- varnit
请参考自述文件:https://github.com/varnitgoyal95/IT-Academy-/tree/master/lambda/It-Academy-Lambda - varnit
@varnit,从日志中可以看出,执行的命令是 aws cloudformation package --template-file samTemplate.yaml --s3-bucket codepipeline-551 --output-template-file outputSamTemplate.yaml - Huzaib Shafi
这与CodeBuild无关,您必须在~/.awsconfig/credential文件中设置一些用户,并且该用户必须具有访问您的S3存储桶的权限,因此在调用Cloud Formation Package命令之前,您需要将此文件放置在适当的AWS凭据环境中。 - varnit
代码构建使用一些基于Linux的机器来运行您的构建,在该Linux机器上,您正在使用一个名为AWS CLI的工具,该工具需要凭据文件。 - varnit
显示剩余3条评论
1个回答

2
检查存储桶本身的策略/权限。

S3 Bucket Policy

如果您和我一样,在第一次设置管道时选择了“默认位置”选项作为工件存储库,则S3存储桶本身可能存在阻止访问的策略。

AWS Pipeline Settings

对我来说,一旦删除了存储桶策略,无法上传工件错误就得到解决,我的构建过程也顺利完成了。
希望这可以帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接