Micronaut AWS Lambda and a Cron Job

Learn how to a scheduled expression with Event Bridge trigger to execute a Micronaut AWS Lambda every 5 minutes

Authors: Sergio del Amo

Micronaut Version: 4.6.3

1. Getting Started

In this guide, we will create a Micronaut application written in Java.

2. What you will need

To complete this guide, you will need the following:

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

4. Writing the App

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-function-app example.micronaut.micronautguide \
    --features=aws-lambda-scheduled-event \
    --build=gradle \
    --lang=java
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

If you use Micronaut Launch, select serverless function as application type and add aws-lambda-scheduled-event feature.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

4.1. Handler

Micronaut Launch/Micronaut CLI generates the following Handler when you select the feature aws-lambda-scheduled-event. The handler receives a Event Bridge (Cloud Watch Events) notification event. Add a log statement.

src/main/java/example/micronaut/FunctionRequestHandler.java
package example.micronaut;
import io.micronaut.function.aws.MicronautRequestHandler;
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FunctionRequestHandler extends MicronautRequestHandler<ScheduledEvent, Void> {
    private static final Logger LOG = LoggerFactory.getLogger(FunctionRequestHandler.class);
    @Override
    public Void execute(ScheduledEvent input) {
        LOG.info("input: {}", input);
        return null;
    }
}

4.2. Logback

Replace src/main/resources/logback.xml with the following content to get a more verbose output:

src/main/resources/logback.xml
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %X{AWSFunctionName} %X{AWSFunctionVersion} %X{AWSFunctionArn} %X{AWSFunctionMemoryLimit}  %X{AWSFunctionRemainingTime} %X{AWS-XRAY-TRACE-ID} %-5p %c{1} - %m%n</pattern>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

5. Lambda

Create a Lambda Function. As a runtime, select Java 17.

create function

5.1. Triggers

Create trigger which fires every 5 minutes:

eventbridgetrigger

5.2. Upload Code

Create an executable jar including all dependencies:

./gradlew shadowJar

Upload it:

upload function code

5.3. Handler

In the AWS console, as Handler, set:

example.micronaut.FunctionHandler

5.4. Test

After you upload the code, wait for 5 minutes and you should see in Cloud Watch logs such as:

TRACE c.s.e.f.d.Handler - {version=0, id=ed9bd6b2-0429-9b03-742b-fe294946f272, detail-type=Scheduled Event, source=aws.events, account=1234567899, time=2021-05-19T09:03:02Z, region=us-east-1, resources=[arn:aws:events:us-east-1:1234567899:rule/5minutes], detail={}}
2021-05-19 09:03:31 b4e05b4e-ae9f-4844-b7fb-5e70016b41fd eurorates-daily-java11 $LATEST arn:aws:lambda:us-east-1:1234567899:function:micronautguide-java11 512 14990 1-60a4d463-1fa098426b0ff44e24a69bf8 TRACE c.s.e.f.d.Handler - {version=0, id=ed9bd6b2-0429-9b03-742b-fe294946f272, detail-type=Scheduled Event, source=aws.events, account=1234567899, time=2021-05-19T09:03:02Z, region=us-east-1, resources=[arn:aws:events:us-east-1:1234567899:rule/5minutes], detail={}}

Read more about:

6. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

7. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).