簡體   English   中英

C# .NET 核心 AWS Lambda Function Cloudwatch 事件處理程序簽名

[英]C# .NET Core AWS Lambda Function Handler Signature for Cloudwatch Event

我正在嘗試在 C# (.NET Core) 中編寫一個 Lambda function ,它將在我的帳戶中發生 CloudWatch 事件時進行處理。 我正在使用無服務器應用程序框架 ( https://www.serverless.com/ ),並且之前成功編寫了處理程序代碼以響應 ApiGateway 請求/事件。 對於 ApiGateway 請求處理程序,方法簽名始終具有相同的兩個參數:

公共 APIGatewayProxyResponse SampleHandler(RequestAPIGatewayProxyRequest 請求,ILambdaContext 上下文)

Per the docs ( https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html ), the first parameter is defined as the "inputType" and is typically specific to the event that trips the function第二個參數是通用的 Lambda function 上下文信息。 目前,我未能成功找到 Cloudwatch 事件的相應 object 類型。

我的無服務器應用程序框架 YAML 文件具有這樣的連接事件:

functions:
  NewRevision:
    handler:  CsharpHandlers::AwsDotnetCsharp.Handlers::NewDataExchangeSubscriptionRevision
    memorySize: 1024 # optional, in MB, default is 1024
    timeout: 20 # optional, in seconds, default is 6
    events:
      - cloudwatchEvent:
          event:
            source:
              - 'aws.dataexchange'
            detail-type:
              - 'Revision Published To Data Set'

我的問題是,有誰知道應該在 CloudWatch 事件的方法簽名中使用什么合適的 object 類型?

Amazon.Lambda.CloudWatchEvents NuGet package 中,您可以使用CloudWatchEvent類型。 訣竅是CloudWatchEvent是一個通用的 class ,具體取決於事件源。 Amazon.Lambda.CloudWatchEvents中定義了一些事件詳細信息類型,但根據您的事件類型,您可能必須創建自己的 POCO 用於具有您關心的字段的通用參數。

您可以將自己想要接受的 object 傳遞到 FunctionHandler 簽名中:

Cloudwatch 事件輸入:{"RegionId":"EMEA"}

我接受 object 的代碼:

public class Payload
{
    public string RegionId { get; set; }
}

private static async Task Main(string[] args)
{
        Func<Payload, ILambdaContext, Task<string>> func = FunctionHandler;
        using (var handlerWrapper = HandlerWrapper.GetHandlerWrapper(func, new JsonSerializer()))
        using (var bootstrap = new LambdaBootstrap(handlerWrapper))
        {
            await bootstrap.RunAsync();
        }
    }
}

public async static Task<string> FunctionHandler(Payload payload, ILambdaContext context)
{
    // Do something
    return "{\"status\":\"Ok\"}";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM