Lambda Function to schedule with Amazon EventBridge using Java.

Swapnil Watkar
3 min readJun 27, 2022

Amazon EventBridge is a serveless event bus service.We will be creating events through Amazon EventBridge rules which trigger our Lambda Function.

Aim: To set up an Amazon EventBridge rule to run an AWS Lambda function for specific schedule.

Steps:

Step1: First create a Lambda function.
Step2: Write a handler in Java for events.
Step3: Schedule Lambda function using AWS EventBridge.
Step4: Test your scheduler.

First create a Lambda function:

Go to your AWS management console. Type Lambda service.
Select the Lambda service from list and open it.
Next click the Create function to generate new lambda function.
Provide Basic information like function Name,Runtime (Java 8).
Default Advanced settings.
Click Create function.

Write a handler in Java for events:

We need to create a jar for our lambda function and upload to lambda service.

Steps to create a jar:

  1. Create maven project using quickstart in any IDE (Eclipse,IntelliJ).
  2. Provide dependencies for aws-lambda-java-core, aws-lambda-java-events in POM.xml

3. Write a code below in the handler class.

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

public class ScheduleLambda
{
public void requestHandler(ScheduledEvent event, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Lambda Schedule occured! ");
logger.log("Lambda event occure "+event.getId().toString());
logger.log("Lambda event Detail "+event.getDetailType().toString());
logger.log("Lambda event "+event.toString());

}
}

4 .Build the project.
5. Generate the jar.

mvn clean package

Open the lambda function and go to the code section click on Upload on button.
Select the jar created for lambda.
upload it.

Edit runtime settings in code section
edit the handler name <package><classname>::handleRequestName

Save changes.

Schedule Lambda function using AWS EventBridge:

Go to your AWS management console. Type Amazon Eventbridge service.
Select the Amazon Eventbridge service from list and open it.

Click on Create rule.
Give the name and description for your rule.
For the rule type select schedule.
Click on Next.

Select the target here. Choose the lambda function and will select the name which we want to run. And Click on Next.

Now wait for 1 min to test our program.

Go to Lambda function click the monitor tab, click the “View logs in Cloudwatch” button to see our log.

we are able to see our lambda triggered.

--

--

Swapnil Watkar

Hi ! I am a Software Engineer, experienced in Mobile and Web applications Also AWS Certified Solutions Architect — Associate