.NET Core CLI
借助 .NET Core CLI,您可以通过跨平台方式创建基于 .NET 的 Lambda 应用程序。本部分假定您已安装 .NET Core CLI。如果您尚未按照,请参阅 Microsoft 网站上的下载 .NET
在 .NET CLI 中,您可以使用 new
命令从命令行创建 .NET 项目。如果要在 Visual Studio 之外创建项目,这种做法非常有用。要查看可用项目类型的列表,请打开命令行并导航到您安装 .NET Core 运行时的位置,然后运行以下命令:
dotnet new -all Usage: new [options] ... Templates Short Name Language Tags ---------------------------------------------------------------------------------------------------------------------------- Console Application console [C#], F#, VB Common/Console Class library classlib [C#], F#, VB Common/Library Unit Test Project mstest [C#], F#, VB Test/MSTest xUnit Test Project xunit [C#], F#, VB Test/xUnit ... Examples: dotnet new mvc --auth Individual dotnet new viewstart dotnet new --help
Lambda 通过 Amazon.Lambda.Templates
dotnet new -i Amazon.Lambda.Templates
一旦安装完成,Lambda 模板将作为 dotnet new
的一部分显示。要检查有关模板的详细信息,请使用 help
选项。
dotnet new lambda.EmptyFunction --help
lambda.EmptyFunction
模板支持以下选项:
-
--name
– 函数的名称 -
--profile
– Amazon SDK for .NET 凭证文件中配置文件的名称 -
--region
– 要在其中创建函数的Amazon区域
这些选项将保存到名为 aws-lambda-tools-defaults.json
的文件中。
使用 lambda.EmptyFunction
模板创建一个函数项目。
dotnet new lambda.EmptyFunction --name MyFunction
在 src/myfunction
目录下,检查以下文件:
-
aws-lambda-tools-defaults.json:这是您部署 Lambda 函数时指定命令行选项的位置。例如:
"profile" : "default", "region" : "us-east-2", "configuration" : "Release", "function-runtime":"dotnet6", "function-memory-size" : 256, "function-timeout" : 30, "function-handler" : "MyFunction::MyFunction.Function::FunctionHandler"
-
Function.cs:您的 Lambda 处理程序函数代码。它是一个 C# 模板,该模板包含默认
Amazon.Lambda.Core
库和默认LambdaSerializer
属性。有关序列化要求和选项的更多信息,请参阅序列化 Lambda 函数。它还包含一个示例函数,您可以编辑该函数以应用您的 Lambda 函数代码。using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Amazon.Lambda.Core; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace MyFunction { public class Function { public string FunctionHandler(string input, ILambdaContext context) { return input.ToUpper(); } } }
-
MyFunction.csproj:列出构成您的应用程序的文件和程序集的 MSBuild
文件。 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> <AWSProjectType>Lambda</AWSProjectType> <!-- Makes the build directory similar to a publish directory and helps the Amazon .NET Lambda Mock Test Tool find project dependencies. --> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <!-- Generate ready to run images during publishing to improve cold start time. --> <PublishReadyToRun>true</PublishReadyToRun> </PropertyGroup> <ItemGroup> <PackageReference Include="Amazon.Lambda.Core" Version="2.1.0 " /> <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.2.0" /> </ItemGroup> </Project>
-
Readme:使用此文件记录您的 Lambda 函数。
在 myfunction/test
目录下,检查以下文件:
-
myFunction.Tests.csproj 下:如前所述,这是一个 MSBuild
文件,其中列出了构成您的测试项目的文件和程序集。另请注意,它包含 Amazon.Lambda.Core
库,因此您可以无缝集成测试函数所需的任何 Lambda 模板。<Project Sdk="Microsoft.NET.Sdk"> ... <PackageReference Include="Amazon.Lambda.Core" Version="2.1.0 " /> ...
-
FunctionTest.cs:
src
目录中包含的相同 C# 代码模板文件。编辑此文件,以镜像您函数的生产代码并对其进行测试,然后将您的 Lambda 函数上载到生产环境。using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Xunit; using Amazon.Lambda.Core; using Amazon.Lambda.TestUtilities; using MyFunction; namespace MyFunction.Tests { public class FunctionTest { [Fact] public void TestToUpperFunction() { // Invoke the lambda function and confirm the string was upper cased. var function = new Function(); var context = new TestLambdaContext(); var upperCase = function.FunctionHandler("hello world", context); Assert.Equal("HELLO WORLD", upperCase); } } }
您的函数一通过其测试,您就可以使用 Amazon.Lambda.Tools .NET Core Global Tool
dotnet tool install -g Amazon.Lambda.Tools
如果您已安装该工具,请确保该工具是使用以下命令的最新版本:
dotnet tool update -g Amazon.Lambda.Tools
有关 Amazon.Lambda.Tools .NET Core Global 的更多信息,请参阅 GitHub 上的Amazon适用于 .NET CLI 的扩展程序
安装 Amazon.Lambda.Tools 之后,您可以使用以下命令部署函数:
dotnet lambda deploy-function MyFunction --function-role role
部署完成后,您可以使用以下命令在生产环境中对其进行重新测试,并将不同的值传递到您的 Lambda 函数处理程序:
dotnet lambda invoke-function MyFunction --payload "Just Checking If Everything is OK"
如果所有操作成功,您将看到以下内容:
dotnet lambda invoke-function MyFunction --payload "Just Checking If Everything is OK" Payload: "JUST CHECKING IF EVERYTHING IS OK" Log Tail: START RequestId:
id
Version: $LATEST END RequestId:id
REPORT RequestId:id
Duration: 0.99 ms Billed Duration: 1 ms Memory Size: 256 MB Max Memory Used: 12 MB