我正在尝试用C# (.NET核心)编写一个Lambda函数,它将在我的帐户中发生CloudWatch事件时进行处理。我正在使用无服务器应用程序框架( https://www.serverless.com/ ),并且以前已经成功地编写了处理程序代码来响应ApiGateway请求/事件。对于ApiGateway请求处理程序,方法签名始终具有相同的两个参数:
公共APIGatewayProxyResponse SampleHandler(RequestAPIGatewayProxyRequest请求,ILambdaContext上下文)
根据文档( https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html ),第一个参数被定义为"inputType“,并且通常特定于触发函数的事件,第二个参数是通用的Lambda函数上下文信息。目前,我找不到Cloudwatch事件的对应对象类型。
我的无服务器应用程序框架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事件的方法签名中应该使用什么适当的对象类型?
发布于 2020-05-07 07:36:40
在Amazon.Lambda.CloudWatchEvents
NuGet包中,可以使用CloudWatchEvent
类型。诀窍是CloudWatchEvent
是一个依赖于事件源的泛型类。在Amazon.Lambda.CloudWatchEvents
中定义了一些事件详细信息类型,但是根据您的事件类型,您可能需要创建一个自己的POCO来用于泛型参数,其中包含您关心的字段。
发布于 2020-08-05 08:33:53
您只需将您自己想要接受的对象传递到FunctionHandler签名:
云手表事件输入:{"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\"}";
}
https://stackoverflow.com/questions/61647614
复制