如何使用 Mono 为 AWS Lambda 函数创建 .NET 函数代码 zip 文件?

How to create a .NET function code zip file for AWS Lambda function using Mono?

总的来说,我是 Mono 和 .NET 生态系统的新手,the official guide 似乎不适用。

由于您是"Mono and .NET eco system"的新手,强烈建议您使用.NET Core.

.NET Core 生态系统能够部署自包含环境,这是大多数云服务所需要的,例如 AWS Lambda(您可以在提供的指南中阅读)。

使用 Visual Studio 2017 时,您可以立即开始 .NET Core 个项目,nuget 上有可用的模板包。

可以在此处找到更多信息,在 AWS docs

实际上指南确实适用,但我必须先安装 dotnet-cli (see how),OS X 上的 Mono 发行版没有。

我还需要一个函数 zip 文件本身,而不是创建函数的能力,这当然不是通常或推荐的工作流程。

从 docker 容器构建这样的 zip:

FROM mono
RUN curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin
COPY src /src
WORKDIR /src
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT 1
RUN /root/.dotnet/dotnet publish LambdaTest/LambdaTest.csproj
RUN zip -r -j dotnet.zip LambdaTest/bin/Debug/netcoreapp2.1/publish/

文件结构:

src/LambdaTest
├── Function.cs
└── LambdaTest.csproj

Function.cs

    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.Json.JsonSerializer))]

    namespace dotnet
    {
        public class Function
        {

            /// <summary>
            /// A simple function that takes a string and does a ToUpper
            /// </summary>
            /// <param name="input"></param>
            /// <param name="context"></param>
            /// <returns></returns>
            public string FunctionHandler(string input, ILambdaContext context)
            {
                return input?.ToUpper();
            }
        }
    }

LambdaTest.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <AWSProjectType>Lambda</AWSProjectType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.4.0" />
  </ItemGroup>

</Project>