AWS Sam软件包命令在buildspec期间失败

时间:2019-10-01 05:59:27

标签: aws-codebuild aws-sam-cli aws-sam

我正在尝试打包/部署用dotnet core编写的AWS SAM Lambda函数。我有3个文件:

pipeline.yml 是一个CloudFormation模板,用于创建CodeBuild项目,设置环境变量并将GitHub webhook连接到特定的buildspec.yml文件。

buildspec.yml 安装所需的软件包,调用dotnet lambda软件包,生成包含.Net打包应用程序的压缩文件。然后调用sam包和sam deploy,这应该使用新的代码库更新Lambda函数。

template.yml 包含Lambda函数的代码,该代码由sam命令打包和部署。

这是我的 pipeline.yml 代码:

AWSTemplateFormatVersion: "2010-09-09"

Parameters: [REMOVED FOR BREVITY]

Resources:
    CodeBuildProject:
        Type: AWS::CodeBuild::Project
        Properties:
            Environment:
                Image: aws/codebuild/dot-net:core-2.1
                EnvironmentVariables:
                    -   Name: S3_DEPLOYMENT_BUCKET ...
                    -   Name: FOLDER ...
                    -   Name: REPO_NAME ...
                    -   Name: ZIPPED_APPLICATION ...
            Name: RoiCalculator-EventPublisher-Master
            Source:
                BuildSpec: RoiCalculator.Serverless.EventPublisher/buildspec.yml
                Location: https://github.com/XXXXXXXXX/RoiCalculator.EventStore
                Type: GITHUB
            Triggers:
                Webhook: true
                FilterGroups:
                    - - Type: EVENT
                        Pattern: PUSH
                      - Type: FILE_PATH
                        Pattern: !Sub ${GitHubTargetName}
                        ExcludeMatchedPattern: false

这是我的 buildspec.yml 文件:

version: 0.2
phases:
    install:
        runtime-versions:
            dotnet: 2.2
        commands:
            - export PATH="$PATH:/root/.dotnet/tools"
            - dotnet tool install -g Amazon.Lambda.Tools
            - pip install aws-sam-cli
    pre_build:
        commands:
            - dotnet restore
    build:
        commands:
            - cd $FOLDER
            - dotnet lambda package --configuration release --framework netcoreapp2.1 -o ./$ZIPPED_APPLICATION
            - sam package --template-file template.yml --s3-bucket $S3_DEPLOYMENT_BUCKET --output-template-file packaged-template.yml --region us-east-2
            - sam deploy --template-file packaged-template.yml --stack-name event-publisher-app --capabilities CAPABILITY_IAM --region us-east-2

这是我的 template.yml 文件:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Resources:
    EventPublisherLambda:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: $REPO_NAME
            Handler: RoiCalculator.Serverless.EventPublisher::RoiCalculator.Serverless.EventPublisher.Function::FunctionHandler
            Role: 
                Fn::ImportValue: 
                    global-lambda-function-execution-arn
            CodeUri: ./$ZIPPED_APPLICATION
            Runtime: dotnetcore2.1

我在CodeBuild输出中遇到此错误:

[Container] 2019/10/01 05:15:48 Phase complete: BUILD State: FAILED 
[Container] 2019/10/01 05:15:48 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: sam package --template-file template.yml --s3-bucket $S3_DEPLOYMENT_BUCKET --output-template-file packaged-template.yml --region us-east-2. Reason: exit status 1 

除了通过pip之外,还有其他方法可以在buildspec中安装aws-sam-cli吗?我的技术是dotnet的核心。是否有特定于dotnet的方式来安装aws-sam-cli?

注意: :如果我将aws s3 cp $ZIPPED_APPLICATION s3://$S3_DEPLOYMENT_BUCKET/$ZIPPED_APPLICATION替换sam package / deploy命令,则该过程有效。因此,似乎与环境变量无关。

我对如何获取sam软件包/部署以与dotnet core应用程序一起工作感到非常困惑。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

“ sam软件包”是“ aws cloudformation软件包”的别名,“ sam deploy”是“ aws cloudformation部署”的别名。如果您在安装/使用SAM cli时遇到问题,可以尝试使用“ aws cloudformation ...”命令代替这些操作。

[1] https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html

[2] https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-package.html