aws lambda_开发在AWS Lambda上运行的Net Core App

aws lambda

关于这篇文章 (About this post)

This post explains the following ways:

这篇文章解释了以下方法:

  1. How to develop AWS Lambda function using .NET Core

    如何使用.NET Core开发AWS Lambda函数
  2. How to develop ASP.NET Core app running on Lambda

    如何开发在Lambda上运行的ASP.NET Core应用
  3. How to enable your existed ASP.NET Core app to run on Lambda

    如何使现有的ASP.NET Core应用程序在Lambda上运行

1.如何使用.NET Core开发Lambda函数 (1. How to develop Lambda function using .NET Core)

In this section, I explain how to develop a Lambda function using .NET Core.

在本节中,我将说明如何使用.NET Core开发Lambda函数。

安装项目模板 (Installing project templates)

Firstly, you install Amazon.Lambda.Templates package as a project template of .NET Core by the following command:

首先,通过以下命令将Amazon.Lambda.Templates包安装为.NET Core的项目模板:

dotnet new -i Amazon.Lambda.Templates

This command enables you to use the following templates:

此命令使您可以使用以下模板:

  • lambda.SQS (Lambda function triggered by SQS)

    lambda.SQS(由SQS触发的Lambda函数)
  • lambda.SNS (Lambda function triggered by SNS)

    lambda.SNS(由SNS触发的Lambda函数)
  • lambda.S3 (Lambda function triggered by S3 events)

    lambda.S3(由S3事件触发的Lambda函数)
  • lambda.Kinesis (Lambda function triggered by Kinesis Stream

    lambda.Kinesis(由Kinesis Stream触发的Lambda函数
  • lambda.KinesisFirehose (Lambda function triggered by Kinesis Firehose)

    lambda.KinesisFirehose(由Kinesis Firehose触发的Lambda函数)
  • lambda.DynamoDB (Lambda function triggered by DynamoDB Stream)

    lambda.DynamoDB(由DynamoDB Stream触发的Lambda函数)
  • lambda.SimpleApplicationLoadBalancerFunction (Lambda function triggered by Application Load Balancer)

    lambda.SimpleApplicationLoadBalancerFunction(由应用程序负载平衡器触发的Lambda函数)
  • serverless.AspNetCoreWebApp (ASP.NET Core Razor Pages app running on Lambda)

    serverless.AspNetCoreWebApp(在Lambda上运行的ASP.NET Core Razor Pages应用)
  • serverless.AspNetCoreWebAPI (ASP.NET Core Web API app running on Lambda)

    serverless.AspNetCoreWebAPI(在Lambda上运行的ASP.NET Core Web API应用)

You can confirm all templates you installed by the following command:

您可以通过以下命令确认已安装的所有模板:

dotnet new lambda --list

使用模板创建项目 (Creating a project using the template)

For example, You can create a Lambda function triggered by queue messages of SQS by the following command. This command will create a project (it contains test codes) using the template.

例如,您可以通过以下命令创建由SQS队列消息触发的Lambda函数。 此命令将使用模板创建一个项目(包含测试代码)。

dotnet new lambda.SQS --name {Project name} --region {AWS region you want to use} --profile {AWS profile name}# Example: dotnet new lambda.SQS --name mysqsfunc --region ap-northeast-1 --profile default

I think the above command generated a Lambda function code (Function.cs) like the below code. (I removed comments of the function code for high visibility). If you didn’t modify any code after creating a new project (by default), The FunctionHandler method will be called when the Lambda function is executed.

我认为上面的命令生成了一个Lambda函数代码(Function.cs),类似于下面的代码。 (我删除了功能代码的注释,以提高可见性)。 如果在创建新项目后(默认情况下)未修改任何代码,则在执行Lambda函数时将调用FunctionHandler方法。

Function.cs
Function.cs

“aws-lambda-tools-defaults.json” is the definition file of deploying the Lambda function. I recommend you to confirm that .NET Core version indicated at Project File(.csproj) and .NET Core version indicated at this file (“framework”, “function-runtime”) are same.

“ aws-lambda-tools-defaults.json”是部署Lambda函数的定义文件。 我建议您确认项目文件(.csproj)指示的.NET Core版本和此文件指示的.NET Core版本(“框架”,“功能运行时”)是否相同。

aws-lambda-tools-defaults.json
aws-lambda-tools-defaults.json

将项目部署到AWS (Deploying the project to AWS)

You can deploy the project easily using Amazon.Lambda.Tools .NET Core Global Tool that is one of .NET Core tools. You can install this tool by the following command:

您可以使用.NET Core工具之一的Amazon.Lambda.Tools .NET Core全局工具轻松部署项目。 您可以通过以下命令安装此工具:

dotnet tool install -g Amazon.Lambda.Tools

After installing this tool, you can deploy your project to AWS by executing the following command at same directory with .NET Core project file (.csproj):

安装此工具后,可以通过在与.NET Core项目文件(.csproj)相同的目录中执行以下命令,将项目部署到AWS:

dotnet lambda deploy-function {Lambda function's name}# Example: dotnet lambda deploy-function samplesqsapp

When you create a new Lambda function using this tool, you will be asked which IAM role do you want to attach to the Lambda function. At that time, you need to select the IAM role you want to attach to the Lambda function by inputting a number, or “Create new IAM Role” to create a new IAM role for this Lambda function.

使用此工具创建新的Lambda函数时,将询问您要附加到Lambda函数的哪个IAM角色。 那时,您需要通过输入数字来选择要附加到Lambda函数的IAM角色,或“创建新IAM角色”来为此Lambda函数创建新的IAM角色。

Select IAM Role that to provide AWS credentials to your code:1) RoleA2) RoleB...X) *** Create new IAM Role ***

When you can see the message “New Lambda function created”, deploying the Lambda function is already completed. However, it was just a deploying code to AWS as a Lambda function. You need to configure a trigger of the Lambda function by yourself.

当您看到消息“创建了新的Lambda函数”时,说明Lambda函数的部署已完成。 但是,它只是作为Lambda函数将代码部署到AWS。 您需要自己配置Lambda函数的触发器。

2.如何开发在Lambda上运行的ASP.NET Core应用 (2. How to develop ASP.NET Core app running on Lambda)

In this section, I explain how to develop an ASP.NET Core app running on Lambda.

在本节中,我将说明如何开发在Lambda上运行的ASP.NET Core应用程序。

使用模板创建项目 (Creating a project using the template)

You can easily create an ASP.NET Core app that can run as a Lambda function by installing Amazon.Lambda.Templates package and using the following templates:

通过安装Amazon.Lambda.Templates包并使用以下模板,您可以轻松创建可以作为Lambda函数运行的ASP.NET Core应用程序:

  • serverless.AspNetCoreWebApp (ASP.NET Core Razor Pages app running on Lambda)

    serverless.AspNetCoreWebApp(在Lambda上运行的ASP.NET Core Razor Pages应用)
  • serverless.AspNetCoreWebAPI (ASP.NET Core Web API app running on Lambda)

    serverless.AspNetCoreWebAPI(在Lambda上运行的ASP.NET Core Web API应用)
dotnet new serverless.AspNetCoreWebApp --name {Project name} --region {AWS region you want to use} --profile {AWS profile name}# Example: new serverless.AspNetCoreWebApp --name myaspnetapp --region ap-northeast-1 --profile default

将项目部署到AWS (Deploying the project to AWS)

You can deploy the project you created using Amazon.Lambda.Tools .NET Core Global Tool that you installed at the below. The following command will deploy your ASP.NET Core app to AWS as a Lambda function. Unlike “deploy-function” command, “deploy-serverless” command deploy your ASP.NET Core app using CloudFormation, its stack contains not only a Lambda function but also Amazon API Gateway resources (like a Stage), an IAM role, etc..

您可以部署使用下面安装的Amazon.Lambda.Tools .NET Core全局工具创建的项目。 以下命令会将您的ASP.NET Core应用程序作为Lambda函数部署到AWS。 与“ deploy-function”命令,“ deploy-serverless”命令使用CloudFormation部署您的ASP.NET Core应用程序不同,其堆栈不仅包含Lambda函数,而且还包含Amazon API Gateway资源(如Stage),IAM角色等。 。

dotnet lambda deploy-serverless

You need to indicate a CloudFormation stack name and an S3 bucket name which is used for storing your application code. You can define those at aws-lambda-tools-defaults.json to skip those input.

您需要指定一个CloudFormation堆栈名称和一个S3存储桶名称,用于存储您的应用程序代码。 您可以在aws-lambda-tools-defaults.json上定义这些内容,以跳过这些输入。

3.如何使您现有的ASP.NET Core应用程序在Lambda上运行 (3. How to enable your existed ASP.NET Core app to run on Lambda)

The way I explained above(#2) is how to create a new ASP.NET Core app that can run as a Lambda function. In this section, I explain how to enable your EXISTED ASP.NET Core app to run on Lambda.

我上面解释的方法(#2)是如何创建可以作为Lambda函数运行的新ASP.NET Core应用程序。 在本节中,我将说明如何使现有的ASP.NET Core应用程序在Lambda上运行。

安装Nuget软件包 (Installing a Nuget package)

Firstly, you need to install a Nuget package Amazon.Lambda.AspNetCoreServer to your project that you want to deploy to AWS as a Lambda function.

首先,您需要将Nuget包Amazon.Lambda.AspNetCoreServer安装到要作为Lambda函数部署到AWS的项目。

dotnet add package Amazon.Lambda.AspNetCoreServer

创建LambdaEntryPoint类 (Creating LambdaEntryPoint class)

Next, please refer to the following code and create the LambdaEntryPoint class:

接下来,请参考以下代码并创建LambdaEntryPoint类:

LambdaEntryPoint.cs
LambdaEntryPoint.cs

If you want to call ASP.NET Core app via API Gateway, you need to use APIGatewayProxyFunction abstraction class as the parent class of LambdaEntryPoint. If you want to call it via Application Load Balancer, you need to use ApplicationLoadBalancerFunction abstraction class. When

如果要通过API网关调用ASP.NET Core应用,则需要使用APIGatewayProxyFunction抽象类作为LambdaEntryPoint的父类。 如果要通过Application Load Balancer调用它,则需要使用ApplicationLoadBalancerFunction抽象类。 什么时候

LambdaEntryPoint class inherits those classes, you need to develop Init method. So please make Init method to indicate Startup class (Startup.cs).

LambdaEntryPoint类继承了这些类,您需要开发Init方法。 因此,请使用Init方法指示启动类(Startup.cs)。

You will indicate FunctionHandlerAsync method of LambdaEntryPoint class as a handler of the Lambda function you will deploy. (You don’t need to develop FunctionHandlerAsync method by yourself because this method is already developed at AbstractAspNetCoreFunction class that is a parent of a parent of LambdaEntryPoint class)

您将把LambdaEntryPoint类的FunctionHandlerAsync方法指示为将要部署的Lambda函数的处理程序。 (您不需要自己开发FunctionHandlerAsync方法,因为该方法已经在AbstractAspNetCoreFunction类中开发 ,该类是LambdaEntryPoint类的父级)

创建用于定义部署的文件 (Creating files for the definition of the deployment)

Then, please create an “aws-lambda-tools-defaults.json” file and a “serverless.template” file for deploying your existed ASP.NET Core app to AWS.

然后,请创建一个“ aws-lambda-tools-defaults.json”文件和一个“ serverless.template”文件,以将您现有的ASP.NET Core应用程序部署到AWS。

aws-lambda-tools-defaults.json
aws-lambda-tools-defaults.json
serverless.template
无服务器模板

“serverless.template” is a template of AWS Serverless Application Model (an extension of AWS CloudFormation). You can modify “serverless.template” according to this reference.

“ serverless.template”是AWS无服务器应用程序模型(AWS CloudFormation的扩展)的模板。 您可以根据此参考资料修改“ serverless.template”。

将项目部署到AWS (Deploying the project to AWS)

Finally, as you did in the above section, you can deploy your existed ASP.NET Core app to AWS as a Lambda function by executing the following command.

最后,如上一节所述,您可以通过执行以下命令将现有的ASP.NET Core应用程序作为Lambda函数部署到AWS。

dotnet lambda deploy-serverless

This deployment uses CloudFormation too, so Lambda function and other required resources like API Gateway will be deployed as CloudFormation stack.

此部署也使用CloudFormation,因此Lambda函数和其他必需的资源(如API Gateway)将作为CloudFormation堆栈进行部署。

…Created CloudFormation stack sampleaspnetcoreappTimestamp Logical Resource Id Status— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —2020/07/06 14:00 sampleaspnetcoreapp CREATE_IN_PROGRESS2020/07/06 14:00 AspNetCoreFunctionRole CREATE_IN_PROGRESS2020/07/06 14:00 AspNetCoreFunctionRole CREATE_IN_PROGRESS2020/07/06 14:00 AspNetCoreFunctionRole CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunction CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunction CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunction CREATE_COMPLETE2020/07/06 14:01 ServerlessRestApi CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApi CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApi CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunctionProxyResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiDeploymentcfb7a37fc3 CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunctionProxyResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunctionRootResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 AspNetCoreFunctionRootResourcePermissionProd CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiDeploymentcfb7a37fc3 CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiDeploymentcfb7a37fc3 CREATE_COMPLETE2020/07/06 14:01 ServerlessRestApiProdStage CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiProdStage CREATE_IN_PROGRESS2020/07/06 14:01 ServerlessRestApiProdStage CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunctionProxyResourcePermissionProd CREATE_COMPLETE2020/07/06 14:01 AspNetCoreFunctionRootResourcePermissionProd CREATE_COMPLETE2020/07/06 14:01 sampleaspnetcoreapp CREATE_COMPLETEStack finished updating with status: CREATE_COMPLETEOutput Name Value— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —ApiURL https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/

After completing the deployment, you can see the URL that enables us to access your web application.

完成部署后,您可以看到使我们能够访问您的Web应用程序的URL。

翻译自: https://medium.com/@mahiya/developing-net-core-app-run-on-aws-lambda-5a342f3cacb9

aws lambda

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值