从 AWS CodePipeline 操作调用 C# .NET Core Lambda

Ron*_*Ron 3 .net-core aws-lambda aws-codepipeline

AWS CodePipeline 允许您从操作调用自定义 Lambda,如下所述:https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.htmltion

我无法确定应如何定义 C# Lambda 函数才能访问管道中的输入数据。

我尝试了很多次,认为它会类似于下面的内容。我还尝试创建自己的 C# 类,输入 JSON 数据将被反序列化到该类。

公共无效FunctionHandler(Amazon.CodePipeline.Model.Job CodePipeline,ILambdaContext上下文)

Ron*_*Ron 7

我能够找到解决方案。最初有帮助的第一步是将 lambda 函数的输入参数更改为 Stream。然后我能够将流转换为字符串并准确确定发送给我的内容,例如

    public void FunctionHandler(Stream input, ILambdaContext context)
    {
 ....
    }
Run Code Online (Sandbox Code Playgroud)

然后,根据输入数据,我能够将其映射到包装 AWS SDK Amazon.CodePipeline.Model.Job 类的 C# 类。它必须映射到 json 属性“CodePipeline.job”。下面的代码有效,我能够检索所有输入值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.CodePipeline;
using Newtonsoft.Json;
using System.IO;

// 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 lambdaEmptyFunction
{
    public class Function
    {
        public class CodePipelineInput
        {
            [JsonProperty("CodePipeline.job")]
            public Amazon.CodePipeline.Model.Job job { get; set; }
        }

        public void FunctionHandler(CodePipelineInput input, ILambdaContext context)
        {
            context.Logger.LogLine(string.Format("data {0} {1} {2}", input.job.AccountId, input.job.Data.InputArtifacts[0].Location.S3Location.BucketName, input.job.Id));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)