This tutorial will show you how to create handler for AWS Lambda for monitoring SNS Event, S3 Event and Scheduled Event
Create the following folder structure:

pom.xml: (if using Gradle, replace pom.xml with build.gradle)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>max.java</groupId> <artifactId>AWSLambda</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-events</artifactId> <version>3.11.2</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-joda</artifactId> <version>2.16.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <source>21</source> <target>21</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
plugins { id 'java' id 'com.github.johnrengelman.shadow' version "8.1.1" } java { sourceCompatibility = 21 targetCompatibility = 21 } group 'net.maxjava' version '1.0' repositories { mavenCentral() } dependencies { implementation 'com.amazonaws:aws-lambda-java-events:3.11.2' implementation 'com.amazonaws:aws-lambda-java-core:1.2.3' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-joda:2.16.2' }
Some notes about pom.xml:
- aws-lambda-java-core and aws-lambda-java-events are needed for core AWS Lambda and the SNS and scheduled event types
- jackson-datatype-joda is for converting Java object to JSON for logging
- Since AWS Lambda is looking for a single jar, we need to include all the dependent jars into a single jar, maven-shade-plugin is used to achieve this purpose
- build with mvn clean package
Some notes about build.gradle:
- aws-lambda-java-core and aws-lambda-java-events are needed for core AWS Lambda and the SNS and scheduled event types
- jackson-datatype-joda is for converting Java object to JSON for logging
- Since AWS Lambda is looking for a single jar, we need to include all the dependent jars into a single jar, com.github.johnrengelman.shadow plugin is used to achieve this purpose
- build with gradle shadowJar
Monitoring of S3 Event:
MyS3EventHandler.java:
package handler; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.S3Event; import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.joda.JodaModule; public class MyS3EventHandler implements RequestHandler<S3Event, String> { @Override public String handleRequest(S3Event s3Event, Context context) { LambdaLogger logger = context.getLogger(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JodaModule()); try { String string = objectMapper.writeValueAsString(s3Event); logger.log("EVENT: " + string); } catch (JsonProcessingException e) { logger.log("ERROR " + e.getMessage()); } for (int count = 0; count < s3Event.getRecords().size(); count++) { S3EventNotification.S3EventNotificationRecord s3EventNotificationRecord = s3Event.getRecords().get(count); // Process necessary business logic for the bucket and the object logger.log("bucket name: " + s3EventNotificationRecord.getS3().getBucket().getName() + " object name: " + s3EventNotificationRecord.getS3().getObject().getKey()); } return "success"; } }
Some notes about MyS3EventHandler.java::
- The class has to implement the com.amazonaws.services.lambda.runtime.RequestHandler interface, providing the input and output class type
- For handling S3 Events, S3Event will be used as the input type, with String as the output, but this example will not make the use of that, return "success" would be enough
- Override the function public String handleRequest(S3Event s3Event, Context context) to handle the S3Event
How to set up AWS Lambda function to use this jar file for handling S3 Event:
- Login to AWS console and go to the Lambda console and select "Create a function" in the main page
- In the left menu panel, select "Functions" and then click "Create function" in the "Functions" page
- Enter a function name, e.g. myS3Function and select Java 21 as the Runtime and then click "Create function"
- In the "Code" tab, select "Upload from" and upload the jar file (use the jar file which contains dependency jars) and click "Save"
- In the "Runtime settings", click "Edit" and change the value of "Handler" to handler.MyS3EventHandler::handleRequest and click "Save"
- Now the AWS Lambda function is set up, but we still have to configure which S3 folder will trigger this Lambda. Click the "Add trigger" button on top of the page and select S3
- In the "Trigger configuration" page, select the Bucket and the folder (if any, in the "Prefix" section) that will trigger this Lambda function. Leave other settings as default and click "Add"
- Now the AWS Lambda function is set up for monitoring that S3 folder. Try upload a file to the folder and it will trigger the Lambda
- To see the result, go to CloudWatch console. In the left menu panel, select "logs" then "Log groups" and select /aws/lambda/myS3Function
- Click on the latest log stream and you should be able to see the logs
Monitoring of SNS Event:
MySNSEventHandler.java:
package handler; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.SNSEvent; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.joda.JodaModule; public class MySNSEventHandler implements RequestHandler<SNSEvent, String> { @Override public String handleRequest(SNSEvent snsEvent, Context context) { LambdaLogger logger = context.getLogger(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JodaModule()); try { String string = objectMapper.writeValueAsString(snsEvent); logger.log("EVENT: " + string); } catch (JsonProcessingException e) { logger.log("ERROR " + e.getMessage()); } snsEvent.getRecords().stream().forEach((SNSEvent.SNSRecord snsRecord) -> { //process each record logger.log("SNSRecord subject" + snsRecord.getSNS().getSubject() + " message: " + snsRecord.getSNS().getMessage()); }); return "success"; } }
Some notes about MySNSEventHandler.java::
- The class has to implement the com.amazonaws.services.lambda.runtime.RequestHandler interface, providing the input and output class type
- For handling SNS Events, SNSEvent will be used as the input type, with String as the output, but this example will not make the use of that, return "success" would be enough
- Override the function public String handleRequest(SNSEvent snsEvent, Context context) to handle the SNSEvent
How to set up AWS Lambda function to use this jar file for handling SNS Event:
- Login to AWS console and go to the Lambda console and select "Create a function" in the main page
- In the left menu panel, select "Functions" and then click "Create function" in the "Functions" page
- Enter a function name, e.g. mySNSFunction and select Java 21 as the Runtime and then click "Create function"
- In the "Code" tab, select "Upload from" and upload the jar file (use the jar file which contains dependency jars) and click "Save"
- In the "Runtime settings", click "Edit" and change the value of "Handler" to handler.MySNSEventHandler::handleRequest and click "Save"
- Now the AWS Lambda function is set up, but we still have to configure which SNS event will trigger this Lambda. Click the "Add trigger" button on top of the page and select SNS
- Select the SNS topic which this Lambda will monitor. (If nothing shows up in the SNS topic dropdown menu, go to the SNS console and make sure the SNS topic is created and then click the reload button to reload). Click the "Add" button
- Now the AWS Lambda function is set up for monitoring that SNS topic. Try publish a message to this topic and it will trigger the Lambda
- To see the result, go to CloudWatch console. In the left menu panel, select "logs" then "Log groups" and select /aws/lambda/mySNSFunction
- Click on the latest log stream and you should be able to see the logs
Monitoring of Scheduled Event:
MyScheduledEventHandler.java:
package handler; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.ScheduledEvent; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.joda.JodaModule; public class MyScheduledEventHandler implements RequestHandler<ScheduledEvent, String> { @Override public String handleRequest(ScheduledEvent scheduledEvent, Context context) { LambdaLogger logger = context.getLogger(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JodaModule()); try { String string = objectMapper.writeValueAsString(scheduledEvent); logger.log("EVENT: " + string); } catch (JsonProcessingException e) { logger.log("ERROR " + e.getMessage()); } // Process necessary business logic for the scheduled event logger.log("ScheduledEvent ID " +scheduledEvent.getId()); return "success"; } }
Some notes about MyScheduledEventHandler.java::
- The class has to implement the com.amazonaws.services.lambda.runtime.RequestHandler interface, providing the input and output class type
- For handling scheduled Events, ScheduledEvent will be used as the input type, with String as the output, but this example will not make the use of that, return "success" would be enough
- Override the function public String handleRequest(ScheduledEvent scheduledEvent, Context context) to handle the SNSEvent
How to set up AWS Lambda function to use this jar file for handling scheduled Event:
- Login to AWS console and go to the Lambda console and select "Create a function" in the main page
- In the left menu panel, select "Functions" and then click "Create function" in the "Functions" page
- Enter a function name, e.g. myScheduledFunction and select Java 21 as the Runtime and then click "Create function"
- In the "Code" tab, select "Upload from" and upload the jar file (use the jar file which contains dependency jars) and click "Save"
- In the "Runtime settings", click "Edit" and change the value of "Handler" to handler.MyScheduledEventHandler::handleRequest and click "Save"
- Now the AWS Lambda function is set up, but we still have to configure a scheduled event will trigger this Lambda. Click the "Add trigger" button on top of the page and select EventBridge (CloudWatch Events)
- Select "Create a new rule" (or using "Existing rules if one already exists), enter a Rule name e.g. myFirstRule. Select "Schedule expression" and enter your own schedule expression e.g. rate(1 minute) and click "Add"
- Now the AWS Lambda function is set up to be triggered per the scheduled pattern
- To see the result, go to CloudWatch console. In the left menu panel, select "logs" then "Log groups" and select /aws/lambda/myScheduledFunction
- Click on the latest log stream and you should be able to see the logs