我使用Amazon模板使用C#Dotnet3.1创建了一个AWS Lambda解决方案
dotnet new serverless.AspNetCoreWebAPI -n MyDotNet.Lambda.Service
这将创建一个lambda函数,其处理程序为mydotnet.lambda.service::mydotnet.lambda.service.lambda.frice.lambdaEntryPoint::FunctionHandlerAsync
以及一些Serverless.Template
文件和aws-lambda-tools-defaults.json
部署它的标准方法是安装amazon.lambda.tools
dotnet tool update -g Amazon.Lambda.Tools
然后运行
dotnet lambda deploy-serverless --profile myawsprofile
请注意,配置文件是可选的,但我在该配置文件下配置了AWS。这将提示输入CloudFormation堆栈名称(例如:foo)和S3 bucket(例如:my-bucket)并将其部署到自定义配置文件myAWSProfile
下配置的“真实”AWS
目前一切正常。现在我刚刚发现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下运行
为了使用LocalStack运行AWS CLI,我安装了包装器https://github.com/LocalStack/awscli-local,这样我就可以执行以下操作
awslocal s3 ls
我太新了,无法理解我所学习的大部分教程。其中一些提到了无服务器框架,但我只是在使用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(2021年3月11日)我已经逐步尝试使用Amazon模板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 , 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, ILambdaContext lambdaContext)
at lambda_method(Closure , Stream , Stream , 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
从Amazon模板创建空示例C#lambda函数
dotnet new lambda.EmptyFunction -n Sample.Lambda.DotNet
汇编出版
dotnet build
dotnet publish -c Release -o publish
Zip 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
替换AWS
,而且我没有指定任何配置文件,但是您可以使用--profile default
或者您的.aws/credential
创建角色
awslocal 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策略附加到角色
awslocal iam attach-role-policy --role-name lambda-dotnet-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
创建lambda
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
在localstack中调用lambda,传递json负载(字符串是有效的json)
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
删除功能
awslocal lambda delete-function --function-name lambda-dotnet-function
对于dotnet工具,等价物是
dotnet lambda invoke-function lambda-dotnet-function --payload "Just Checking If Everything is OK" --profile diegosasw
我尝试用LocalStack运行lambda函数。我安装了awscli-local和localstack pip3安装awscli-local pip3安装--用户localstack--忽略-安装六个 然后我启动了localstack lamdba_executor=Docker本地堆栈启动--Docker 当我现在要创建lambda函数时 aws lambda create-function-
问题内容: 运行Airflow的常规说明不适用于Windows环境: Airflow实用程序在命令行中不可用,我在其他地方找不到要手动添加的实用程序。Airflow如何在Windows上运行? 问题答案: 您可以在Windows中激活,并直接按照本教程进行操作。我能够按照上面的步骤启动并成功运行。 安装完成后,请进行编辑以将所有配置指向Windows系统中的某个位置,而不是lxss(ubuntu)
我已经在Ubuntu 14.04上安装了tensorflow的GPU版本。 我在一个GPU服务器上,在那里Tenorflow可以访问可用的GPU。 我想在CPU上运行tenorflow。 通常我可以使用env CUDA\u VISIBLE\u DEVICES=0在0号GPU上运行。 如何在CPU之间进行选择? 我对用tf重写代码不感兴趣。设备(“/cpu:0”):
刚开始玩jdk9 - 并且有点卡在一开始: < li >下载并解压缩了eclipse-Java-neon-m4a-win32 < li >已安装java 9支持(通过市场中的免打扰) < li >下载并安装了jdk9u99(只有jdk,没有公共jre) 此时,我可以在默认的Java(8u 60)上运行eclipse,既可以使用JRE/JDK(eclipse . ini中的/out vm参数),也可
我正在使用docker compose和气流图像puckel/docker气流设置一个新的气流服务器运行良好,但当我尝试使用DockerOperator时,没有名为“docker”的模块。 我需要如何更改我的docker-compose文件才能在我的docker中使用docker? 我试图在谷歌上查找错误消息,但没有一个建议的解决方案奏效。 这是我正在使用的< code>WebServer的doc
我试图使用localstack对SQS运行一些测试。然而,我一直得到一个502。这适用于同事,但在我的本地机器、我们的管道或docker上不起作用。错误到处都是一样的,所以我发送docker版本,因为它可能更容易显示安装了什么。 基本上,我正在创建一个docker镜像,安装java、python,并从/repo文件夹执行gradle构建,执行集成测试。 我的测试代码如下所示: 故障原因: 有什么想