部署Amazon Lambda使用 .NET 核心 CLI 项目 - Amazon Toolkit for Visual Studio
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

部署Amazon Lambda使用 .NET 核心 CLI 项目

Amazon Toolkit for Visual Studio 包含适用于 Visual Studio 的 Amazon Lambda .NET 内核项目模板。您可以使用 .NET 内核命令行界面 (CLI) 部署 Visual Studio 中构建的 Lambda 函数。

先决条件

在使用 .NET 核心 CLI 部署 Lambda 函数之前,您必须满足以下先决条件:

在使用 .NET 核心 CLI 部署 Lambda 函数时,以下相关主题可能会有所帮助:

列出 .NET 核心 CLI 使用的 Lambda 命令

要列出可通过 .NET 核心 CLI 使用的 Lambda 命令,请执行以下操作。

  1. 打开命令提示符窗口,然后导航到包含 Visual Studio .NET 核心 Lambda 项目的文件夹。

  2. 输入 dotnet lambda --help

C:\Lambda\AWSLambda1\AWSLambda1>dotnet lambda --help Amazon Lambda Tools for .NET Core functions Project Home: https://github.com/aws/aws-lambda-dotnet . Commands to deploy and manage Lambda functions: . deploy-function Deploy the project to Lambda invoke-function Invoke the function in Lambda with an optional input list-functions List all of your Lambda functions delete-function Delete a Lambda function get-function-config Get the current runtime configuration for a Lambda function update-function-config Update the runtime configuration for a Lambda function . Commands to deploy and manage Amazon serverless applications using Amazon CloudFormation: . deploy-serverless Deploy an Amazon serverless application list-serverless List all of your Amazon serverless applications delete-serverless Delete an Amazon serverless application . Other Commands: . package Package a Lambda project into a .zip file ready for deployment . To get help on individual commands, run the following: dotnet lambda help <command>

从 .NET 核心 CLI 发布 .NET 核心 Lambda 项目

以下说明假定您已在 Visual Studio 中创建 Amazon Lambda .NET 内核函数。

  1. 打开命令提示符窗口,然后导航到包含您的 Visual Studio .NET 核心 Lambda 项目的文件夹。

  2. 输入 dotnet lambda deploy-function

  3. 当系统提示时,输入要部署的函数的名称。它可以是新名称或现有函数的名称。

  4. 当系统提示时,输入Amazon区域 (Lambda 函数将部署到的区域)。

  5. 当系统提示时,选择或创建 Lambda 将在执行函数时代入的 IAM 角色。

成功完成后,将显示消息 New Lambda function created (新 Lambda 函数已创建)

C:\Lambda\AWSLambda1\AWSLambda1>dotnet lambda deploy-function Executing publish command ... invoking 'dotnet publish', working folder 'C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish' ... publish: Publishing AWSLambda1 for .NETCoreApp,Version=v1.0 ... publish: Project AWSLambda1 (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing ... publish: Compiling AWSLambda1 for .NETCoreApp,Version=v1.0 ... publish: Compilation succeeded. ... publish: 0 Warning(s) ... publish: 0 Error(s) ... publish: Time elapsed 00:00:01.2479713 ... publish: ... publish: publish: Published to C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish ... publish: Published 1/1 projects successfully Zipping publish folder C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish to C:\Lambda\AWSLambda1\AWSLamb da1\bin\Release\netcoreapp1.0\AWSLambda1.zip Enter Function Name: (Amazon Lambda function name) DotNetCoreLambdaTest Enter Amazon Region: (The region to connect to Amazon services) us-west-2 Creating new Lambda function Select IAM Role that Lambda will assume when executing function: 1) lambda_exec_LambdaCoreFunction 2) *** Create new IAM Role *** 1 New Lambda function created

如果您部署现有函数,则部署函数仅要求Amazon区域。

C:\Lambda\AWSLambda1\AWSLambda1>dotnet lambda deploy-function Executing publish command Deleted previous publish folder ... invoking 'dotnet publish', working folder 'C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish' ... publish: Publishing AWSLambda1 for .NETCoreApp,Version=v1.0 ... publish: Project AWSLambda1 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation. ... publish: publish: Published to C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish ... publish: Published 1/1 projects successfully Zipping publish folder C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish to C:\Lambda\AWSLambda1\AWSLamb da1\bin\Release\netcoreapp1.0\AWSLambda1.zip Enter Function Name: (Amazon Lambda function name) DotNetCoreLambdaTest Enter Amazon Region: (The region to connect to Amazon services) us-west-2 Updating code for existing function

在部署 Lambda 函数后,便可使用该函数。有关更多信息,请参阅 。如何使用 的示例AmazonLambda.

Lambda 会自动替您监控 Lambda 函数,并通过亚马逊报告各项指标 CloudWatch. 要监控 Lambda 函数并排除故障,请参阅诊断和监控AmazonLambda 在亚马逊上使用 CloudWatch.