我正在尝试打包/部署用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?
[NOTE:如果将sam package / deploy命令替换为aws s3 cp $ZIPPED_APPLICATION s3://$S3_DEPLOYMENT_BUCKET/$ZIPPED_APPLICATION
,则该过程有效。因此,似乎与环境变量无关。
我完全迷住了如何获取sam包/部署以与dotnet核心应用程序一起工作。任何帮助表示赞赏。
'sam软件包'是'aws cloudformation软件包'的别名,'sam deploy'是'aws cloudformation部署'的别名。如果您在安装/使用SAM cli时遇到问题,可以尝试使用“ aws cloudformation ...”命令代替这些操作。