【发布时间】:2020-01-30 09:28:02
【问题描述】:
我正在尝试打包/部署使用 dotnet 核心编写的 AWS SAM Lambda 函数。我有 3 个文件:
pipeline.yml 是一个 CloudFormation 模板,用于创建 CodeBuild 项目、设置环境变量并将 GitHub webhook 连接到特定的 buildspec.yml 文件。
buildspec.yml 安装所需的包,调用 dotnet lambda 包,该包会生成一个包含 .Net 打包应用程序的压缩文件。然后调用 sam package 和 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 core。是否有特定于 dotnet 的方法来安装 aws-sam-cli?
注意:如果我将 sam package/deploy 命令替换为 aws s3 cp $ZIPPED_APPLICATION s3://$S3_DEPLOYMENT_BUCKET/$ZIPPED_APPLICATION,则该过程有效。因此,这似乎不是环境变量的问题。
我完全不知道如何让 sam 包/部署与 dotnet 核心应用程序一起工作。任何帮助表示赞赏。
【问题讨论】:
-
出于好奇,您希望在 sam 中利用哪些功能?如果你不是,我想知道你为什么不只是调用“dotnet lambda deploy-serverless”而不是 sam package 和 sam deploy 命令。
-
我的目标是使用驻留在 S3 存储桶中的新构建来更新 lambda 函数。 sam 命令处理命名 S3 存储桶中的代码,并为每个后续构建使用新名称更新 Lambda 函数。我在构建规范的安装阶段安装 sam 应用程序时遇到了困难,因为我是一个 dotnet 核心环境。从 sam 命令更改为 aws cloudformation 有效。
标签: aws-codebuild aws-sam-cli aws-sam