问题描述
DotNet3.1 AWS Lambda
我使用 Amazon 模板使用 C# DotNet3.1 创建了一个 AWS Lambda 解决方案
dotnet new serverless.AspNetCoreWebAPI -n MyDotNet.Lambda.Service
这将创建一个 lambda 函数,其处理程序为 MyDotNet.Lambda.Service::MyDotNet.Lambda.Service.LambdaEntryPoint::FunctionHandlerAsync
加上一些 serverless.template
文件和 aws-lambda-tools-defaults.json
部署 DotNet3.1 AWS Lambda 的标准方法
部署它的标准方法是安装 Amazon.Lambda.Tools
dotnet tool update -g Amazon.Lambda.Tools
然后运行
dotnet lambda deploy-serverless --profile myawsprofile
请注意,配置文件是可选的,但我已经在该配置文件下配置了 AWS。
这将提示输入 CloudFormation 堆栈名称(例如:foo)和一个 S3 存储桶(例如:my-bucket)
并将其部署到自定义配置文件下配置的“真实”AWS myawsprofile
LocalStack 作为 docker 容器运行
到目前为止一切都很好。现在我刚刚发现 https://github.com/localstack/localstack 这是在本地运行 AWS 平台的好方法,所以我使用 docker-compose 文件 localstack-compose.yml
来启动容器
version: '3.8'
services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
image: localstack/localstack-full
network_mode: bridge
ports:
- "4566:4566"
- "4571:4571"
- "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
environment:
- SERVICES=${SERVICES- }
- DEBUG=${DEBUG- }
- data_dir=${data_dir- }
- PORT_WEB_UI=${PORT_WEB_UI- }
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
- KInesIS_ERROR_PROBABILITY=${KInesIS_ERROR_PROBABILITY- }
- DOCKER_HOST=unix:///var/run/docker.sock
- HOST_TMP_FOLDER=${TMPDIR}
volumes:
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
像这样:
docker-compose -f localstack-compose.yml up
它运行在4566端口
AWS 本地
为了使用 LocalStack 运行 AWS CLI,我安装了包装器 https://github.com/localstack/awscli-local 以便我可以执行诸如
awslocal s3 ls
如何在本地部署 AWS Lambda?
我太新,无法理解我遵循的大部分教程。其中一些是指无服务器框架,但我只是使用 localstack 作为 docker 容器。我还安装了 SAM CLI,以备不时之需(虽然我还不明白是为了什么)
我尝试将其部署到本地堆栈
dotnet lambda deploy-serverless --profile default
这将是等效的,我想,但我明白
Error uploading to MyDotNet.Lambda.Service/AspNetCoreFunction-CodeUri-Or-ImageUri-637509113851513062-637509113886357582.zip in bucket foo: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint
虽然我有一个桶 s3://foo in localstack
要找到一个我可以用我的 AWS 基本知识水平来遵循的示例真的很复杂。 是否有我遗漏的任何说明,或者有关如何逐步实现我想要的内容的不错链接/教程?谢谢
更新 1 (11/3/2021) 我已经使用亚马逊模板 https://gitlab.com/sunnyatticsoftware/sandbox/localstack-sandbox/-/tree/master/02-lambda-dotnet-webapi
创建的 web api 项目逐步尝试但我发现问题。
步骤: 首先我为 lambda 执行创建角色
awslocal iam create-role --role-name lambda-dotnet-webapi-ex --assume-role-policy-document file://trust-policy.json
将策略附加到角色以授予执行权限
awslocal iam attach-role-policy --role-name lambda-dotnet-webapi-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
创建 Lambda 函数
awslocal lambda create-function --function-name lambda-dotnet-webapi-function --zip-file fileb://function.zip --handler Sample.Lambda.DotNet.WebApi::Sample.Lambda.DotNet.WebApi.LambdaEntryPoint::FunctionHandlerAsync --runtime dotnetcore3.1 --role arn:aws:iam::000000000000:role/lambda-dotnet-webapi-ex
使用 base64 实用程序调用 AWS Lambda 来解码日志
awslocal lambda invoke --function-name lambda-dotnet-webapi-function out --log-type Tail --query 'LogResult' --output text | base64 -d
它返回:
iptables v1.4.21: can't initialize iptables table `nat': iptables who? (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
[information] Microsoft.Hosting.Lifetime: Application started. Press Ctrl+C to shut down.
[information] Microsoft.Hosting.Lifetime: Hosting environment: Production
[information] Microsoft.Hosting.Lifetime: Content root path: /var/task
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /var/task
START RequestId: a5eb1d2d-d908-15f6-ace3-d4d0e01a0066 Version: $LATEST
Could not load file or assembly 'system.io.pipelines,Version=4.0.2.1,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
: FileNotFoundException
at Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2.FunctionHandlerAsync(TREQUEST request,ILambdaContext lambdaContext)
at System.Runtime.CompilerServices.AsyncmethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2.FunctionHandlerAsync(TREQUEST request,ILambdaContext lambdaContext)
at lambda_method(Closure,Stream,LambdaContextInternal )
END RequestId: a5eb1d2d-d908-15f6-ace3-d4d0e01a0066
REPORT RequestId: a5eb1d2d-d908-15f6-ace3-d4d0e01a0066 Init Duration: 2305.87 ms Duration: 33.29 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 0 MB
Starting daemons...
ImportError: No module named site
有人有一个可行的例子吗?
更新 2 有趣的是,我已经尝试过针对真正的 AWS(AWS 凭证中的不同配置文件),但我也遇到了错误,但情况有所不同。
创建角色
aws iam create-role --role-name lambda-dotnet-webapi-ex --assume-role-policy-document file://trust-policy.json --profile diegosasw
列出角色
aws iam list-roles --profile diegosasw
附加政策
aws iam attach-role-policy --role-name lambda-dotnet-webapi-ex
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole --profile diegosasw
创建 lambda
aws lambda create-function --function-name lambda-dotnet-webap
i-function --zip-file fileb://function.zip --handler Sample.Lambda.DotNet.WebApi::Sample.Lambda.DotNet.WebApi.LambdaEntryPoint::FunctionHa
ndlerAsync --runtime dotnetcore3.1 --role arn:aws:iam::308309238958:role/lambda-dotnet-webapi-ex --profile diegosasw
aws lambda invoke --function-name lambda-dotnet-webapi-function --profile diegosasw out --log-type Tail --query 'LogResult' --output text | base64 -d
它回来了
START RequestId: 7d77489f-869b-4e4d-87a0-ac800d71eb2d Version: $LATEST
warn: Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction[0]
Request does not contain domain name information but is derived from APIGatewayProxyFunction.
[Warning] Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction: Request does not contain domain name information but is derived from APIGatewayProxyFunction.
Object reference not set to an instance of an object.: NullReferenceException
at Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction.MarshallRequest(InvokeFeatures features,APIGatewayProxyRequest apiGatewayRequest,ILambdaContext lambdaContext)
at Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2.FunctionHandlerAsync(TREQUEST request,LambdaContextInternal )
END RequestId: 7d77489f-869b-4e4d-87a0-ac800d71eb2d
REPORT RequestId: 7d77489f-869b-4e4d-87a0-ac800d71eb2d Duration: 755.06 ms Billed Duration: 756 ms Memory Size: 128 MB Max Memory Used: 87 MB Init Duration: 462.09 ms
解决方法
我让它同时适用于 AWS 和 LocalStack(即:awslocal)。以下是仅使用 AWS CLI 的步骤。这是回购示例 https://gitlab.com/sunnyatticsoftware/sandbox/localstack-sandbox/-/tree/master/03-lambda-dotnet-empty
使用 AWS CLI 在本地堆栈中创建 AWS lambda
AWS
从 Amazon 模板创建空示例 C# lambda 函数
dotnet new lambda.EmptyFunction -n Sample.Lambda.DotNet
编译发布
dotnet build
dotnet publish -c Release -o publish
压缩 lambda 文件
cd publish
zip -r ../function.zip *
创建角色
aws --profile diegosasw iam create-role --role-name lambda-dotnet-ex --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow","Principal": {"Service": "lambda.amazonaws.com"},"Action": "sts:AssumeRole"}]}'
将 AWSLambdaBasicExecutionRole 策略附加到角色
aws --profile diegosasw iam attach-role-policy --role-name lambda-dotnet-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
创建 lambda
aws --profile diegosasw lambda create-function --function-name lambda-dotnet-function --zip-file fileb://function.zip --handler Sample.Lambda.DotNet::Sample.Lambda.DotNet.Function::FunctionHandler --runtime dotnetcore3.1 --role arn:aws:iam::308309238958:role/lambda-dotnet-ex
调用 lambda
aws --profile diegosasw lambda invoke --function-name lambda-dotnet-function --payload "\"Just Checking If Everything is OK\"" response.json --log-type Tail
本地堆栈
对于 localStack 是类似的,但是将 aws
替换为 awslocal
,当然,我没有指定任何配置文件,但您可以使用 --profile default
或任何您拥有的 {{1} }} 在
创建角色
.aws/credentials
将 AWSLambdaBasicExecutionRole 策略附加到角色
awslocal iam create-role --role-name lambda-dotnet-ex --assume-role-policy-document '{"Version": "2012-10-17","Action": "sts:AssumeRole"}]}'
创建 lambda
awslocal iam attach-role-policy --role-name lambda-dotnet-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
在 localstack 中调用 lambda 并传递一个 json 负载(字符串是有效的 JSON)
awslocal lambda create-function --function-name lambda-dotnet-function --zip-file fileb://function.zip --handler Sample.Lambda.DotNet::Sample.Lambda.DotNet.Function::FunctionHandler --runtime dotnetcore3.1 --role arn:aws:iam::000000000000:role/lambda-dotnet-ex
查看功能
awslocal lambda invoke --function-name lambda-dotnet-function --payload "\"Just Checking If Everything is OK again\"" response.json --log-type Tail
删除函数
awslocal lambda list-functions
Dotnet 工具
使用 dotnet 工具,相当于
awslocal lambda delete-function --function-name lambda-dotnet-function