1
votes

I'm trying to package/deploy my AWS SAM Lambda function written with dotnet core. I have 3 files:

pipeline.yml is a CloudFormation template creating a CodeBuild project, sets environmental variables, and wires up GitHub webhook to a specific buildspec.yml file.

buildspec.yml installs needed packages, calls dotnet lambda package which generates a zipped file containing .Net packaged app. Then calls sam package and sam deploy which is supposed to update the Lambda function with new codebase.

template.yml contains the code for Lambda function which gets packaged and deployed by sam commands.

Here's my pipeline.yml code:

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

Here's my buildspec.yml file:

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

Here's my template.yml file:

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

I'm getting this error in CodeBuild output:

[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 

Is there another way to install aws-sam-cli in buildspec other than through pip? My tech is dotnet core. Is there a dotnet specific way to install aws-sam-cli?

NOTE: if I replace sam package/deploy commands with aws s3 cp $ZIPPED_APPLICATION s3://$S3_DEPLOYMENT_BUCKET/$ZIPPED_APPLICATION the process works. So, it does not appear to be an issue with environmental variables.

I'm completely stumped at how to get sam package/deploy to work with dotnet core app. Any help is appreciated.

1
Out of curiosity what features are you hoping to take advantage of in sam? If you aren't I'm wondering why you aren't just calling "dotnet lambda deploy-serverless" instead of the sam package and sam deploy commands.Norm Johanson
My objective is to update lambda function with new build residing in S3 bucket. sam commands handle naming the code in S3 bucket and updates Lambda function with new name for each subsequent build. I was having difficulty installing sam application in install phase of my buildspec because I'm a dotnet core environment. Changing from sam commands to aws cloudformation worked.Tom Schreck

1 Answers

1
votes

'sam package' is an alias to 'aws cloudformation package', 'sam deploy' is an alias to 'aws cloudformation deploy'. If you having trouble installing/using SAM cli, can you try using the 'aws cloudformation ...' command instead for these actions.

[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