Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

For example, for a Lambda function triggered by an S3 event, AWS invokes the function and passes an S3Event object. Similarly, it would pass an SNSEvent object to a function triggered by an SNS message.

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?

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...

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.

This question was also asked here: What is the parameter type passed to a Lambda function by a CloudWatch Events - Schedule trigger? and someone commented suggesting using Object and printing the class name. Turned out to be a LinkedHashMap. Looks to be as correct of an answer as I am going to get...

like image 380
Brooks Avatar asked Jul 09 '16 21:07

Brooks


People also ask

Can Lambda trigger CloudWatch event?

To make a CloudWatch event automatically trigger a Lambda function, we need to set up a cloudwatch rule . Regardless of the event we are handling or what we are doing with the event, our Lambda function that receives the event will have the same basic structure.

What is AWS CloudWatch event rule?

Amazon CloudWatch Events delivers a near real-time stream of system events that describe changes in Amazon Web Services (AWS) resources. Using simple rules that you can quickly set up, you can match events and route them to one or more target functions or streams.


3 Answers

com.amazonaws.services.lambda.runtime.events.ScheduledEvent is the current answer.

I can see that in 2.0.2 version of aws-lambda-java-events library this is available. Code is here and more details on 2.0 version are here

like image 124
sandeepkunkunuru Avatar answered Oct 17 '22 16:10

sandeepkunkunuru


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:

<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>
like image 42
meeza Avatar answered Oct 17 '22 16:10

meeza


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. For instance, CloudWatchEvent can be described as:

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.

To know the structure you can specify type "Map" and printout it to log:

  public void eventHandler(Map event, Context context) {
        log.debug(event); // or System.out....
  }
like image 1
Andremoniy Avatar answered Oct 17 '22 14:10

Andremoniy