简体   繁体   English

AWS Lambda - CloudWatch事件类型

[英]AWS Lambda - CloudWatch Event type

When writing an AWS Java Lambda function that's triggered by Cloudwatch scheduled events, which event object gets passed to the Lambda handler function? 在编写由Cloudwatch预定事件触发的AWS Java Lambda函数时,哪个事件对象会传递给Lambda处理函数?

For example, for a Lambda function triggered by an S3 event, AWS invokes the function and passes an S3Event object. 例如,对于由S3事件触发的Lambda函数,AWS调用该函数并传递S3Event对象。 Similarly, it would pass an SNSEvent object to a function triggered by an SNS message. 类似地,它会将SNSEvent对象传递给由SNS消息触发的函数。

public class LambdaHandler {

    public void eventHandler(S3Event event, Context context) {
    }

OR 要么

public class LambdaHandler {

    public void eventHandler(SNSEvent event, Context context) {
    }

For a Cloudwatch Scheduled Event driven function, what would be in place of SNSEvent / S3Event? 对于Cloudwatch计划事件驱动的功能,将取代SNSEvent / S3Event?

public class LambdaHandler {

    public void eventHandler(__________ event, Context context) {
    }

I can't for the life of me find any examples of AWS Lambda functions written in Java that are triggered by Cloudwatch Scheduled events... 我无法为我的生活找到任何由Java编写的AWS Lambda函数的示例,这些函数由Cloudwatch Scheduled事件触发...

Bonus points for a sample function. 样本函数的加分点。

EDIT 1 There is no correct answer to this yet (though I don't know that AWS has released a proper 'event' object in the SDK that would be passed to the Lambda function), so there may not actually be an answer that I was looking for. 编辑1目前还没有正确答案(虽然我不知道AWS已经在SDK中发布了一个适当的'事件'对象,将被传递给Lambda函数),所以实际上可能没有答案我正在寻找。

This question was also asked here: What is the parameter type passed to a Lambda function by a CloudWatch Events - Schedule trigger? 此处还询问了这个问题: CloudWatch Events - Schedule触发器传递给Lambda函数的参数类型是什么? and someone commented suggesting using Object and printing the class name. 有人评论建议使用Object并打印类名。 Turned out to be a LinkedHashMap. 原来是LinkedHashMap。 Looks to be as correct of an answer as I am going to get... 看起来像我要得到的答案一样正确......

com.amazonaws.services.lambda.runtime.events.ScheduledEvent is the current answer. com.amazonaws.services.lambda.runtime.events.ScheduledEvent是当前的答案。

I can see that in 2.0.2 version of aws-lambda-java-events library this is available. 我可以看到在2.0.2版本的aws-lambda-java-events库中这是可用的。 Code is here and more details on 2.0 version are here 代码在这里 ,2.0版本的更多细节在这里

Following is the boilerplate code for it. 以下是它的样板代码。

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;


public class CollectionLambda {
    public void eventHandler(ScheduledEvent event, Context context) {
        // todo
    }
}

Add following dependencies for maven: 为maven添加以下依赖项:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-core</artifactId>
    <version>1.2.0</version>
</dependency>

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-events</artifactId>
    <version>2.2.2</version>
</dependency>

Unfortunately there is no specific class for this type of events. 不幸的是,这类事件没有特定的类。

But you can freely create your own POJOs and specify them as class of event parameter. 但您可以自由创建自己的POJO并将它们指定为事件参数类。 For instance, CloudWatchEvent can be described as: 例如,CloudWatchEvent可以描述为:

public class CloudWatchEvent {

    private String version;
    private String id;
    private String detailType;
    private String source;
    private String account;
    private Date time;
    private String region;
    private List<String> resources;
    ...   
    // getters and setters
}

AWS Lambda engine automatically tries to serialize input into the object of the given class. AWS Lambda引擎自动尝试将输入序列化为给定类的对象。

To know the structure you can specify type "Map" and printout it to log: 要知道结构,您可以指定类型“Map”并打印输出到日志:

  public void eventHandler(Map event, Context context) {
        log.debug(event); // or System.out....
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM