Micronaut AWS Lambda and S3 Event

Learn how to generate thumbnails for images uploaded to an S3 bucket with AWS Lambda and the Micronaut framework

Authors: Sergio del Amo

Micronaut Version: 3.8.2

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:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

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 --build=gradle --lang=java
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.

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

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

4.1. Enable annotation Processing

If you use Java or Kotlin and IntelliJ IDEA, make sure to enable annotation processing.


4.2. Thumbnail Configuration

Add the following configuration:

  width: 256
  height: 256

Create an interface to encapsulate configuration:

package example.micronaut;

import io.micronaut.core.annotation.NonNull;

public interface ThumbnailConfiguration {

    Integer getWidth();

    Integer getHeight();
package example.micronaut;

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.annotation.NonNull;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@ConfigurationProperties("thumbnail") (1)
public class ThumbnailConfigurationProperties implements ThumbnailConfiguration {

    @NotNull (2)
    private Integer width;

    @NotNull (2)
    private Integer height;

    public Integer getWidth() {
        return width;

    public void setWidth(@NonNull Integer width) {
        this.width = width;

    public Integer getHeight() {
        return height;

    public void setHeight(@NonNull Integer height) {
        this.height = height;
1 The @ConfigurationProperties annotation takes the configuration prefix.
2 You can use validation constraints in the @ConfigurationProperties objects.

4.3. Thumbnail Generation

Create a contract for thumbnail generation. We leverage the @Pattern annotation to accept only jpg and png files.

package example.micronaut;

import io.micronaut.core.annotation.NonNull;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.InputStream;
import java.util.Optional;

public interface ThumbnailGenerator {

    Optional<byte[]> thumbnail(@NonNull InputStream inputStream,
                               @NonNull @NotBlank @Pattern(regexp = "jpg|png") String format);

Add a dependency to Thumbnailator - a thumbnail generation library for Java.


Create an implementation of ThumbnailGenerator which uses Thumbnailator.

package example.micronaut;

import io.micronaut.core.annotation.NonNull;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.inject.Singleton;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

@Singleton (1)
public class ThumbnailatorThumbnailGenerator implements ThumbnailGenerator {
    private static final Logger LOG = LoggerFactory.getLogger(ThumbnailatorThumbnailGenerator.class);
    private final ThumbnailConfiguration thumbnailConfiguration;

    public ThumbnailatorThumbnailGenerator(ThumbnailConfiguration thumbnailConfiguration) { (2)
        this.thumbnailConfiguration = thumbnailConfiguration;

    public Optional<byte[]> thumbnail(@NonNull InputStream inputStream, @NonNull @NotBlank @Pattern(regexp = "jpg|png") String format) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
                    .size(thumbnailConfiguration.getWidth(), thumbnailConfiguration.getHeight())
            return Optional.of(byteArrayOutputStream.toByteArray());

        } catch (IOException e) {
            LOG.warn("IOException thrown while generating the thumbnail");
        return Optional.empty();
1 Use jakarta.inject.Singleton to designate a class as a singleton.
2 Use constructor injection to inject a bean of type ThumbnailConfiguration.

4.4. Handler

When you select the feature aws-lambda-s3-event-notification, the application build includes the following dependency:


To be able to inject an S3Client Add the following dependencies:


The handler receives an S3 notification event and for each S3 event of type ObjectCreated creates a thumbnail if the S3 object is a PNG or JPG in the thumbnails folder of the same S3 bucket which triggered the event.

package example.micronaut;

import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification;
import io.micronaut.serde.annotation.Serdeable;
import io.micronaut.function.aws.MicronautRequestHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import jakarta.inject.Inject;
import java.util.Locale;

public class FunctionRequestHandler
        extends MicronautRequestHandler<S3EventNotification, Void> { (1)
    private static final Logger LOG = LoggerFactory.getLogger(FunctionRequestHandler.class);
    private static final String SLASH = "/";
    private static final String THUMBNAILS = "thumbnails";
    public static final String OBJECT_CREATED = "ObjectCreated";
    private static final String JPG = "jpg";
    private static final String PNG = "png";
    private static final char DOT = '.';

    public S3Client s3Client; (2)

    public ThumbnailGenerator thumbnailGenerator; (3)

    public Void execute(S3EventNotification input) {
        for (S3EventNotification.S3EventNotificationRecord record : input.getRecords()) {

            LOG.info("event name: {}" , record.getEventName());
            if (record.getEventName().contains(OBJECT_CREATED)) { (4)
                S3EventNotification.S3Entity s3Entity = record.getS3();
                String bucket = s3Entity.getBucket().getName();
                String key = s3Entity.getObject().getKey();
                int index = key.lastIndexOf(DOT);
                if (index != -1) {
                    String format = key.substring(index + 1).toLowerCase(Locale.ENGLISH);
                    if (format.equals(PNG) || format.equals(JPG)) {
                                .build()), format)
                                .ifPresent(bytes -> s3Client.putObject(PutObjectRequest.builder()
                                        .build(), RequestBody.fromBytes(bytes)));
        return null;

    private static String thumbnailKey(String key) {
        int index = key.lastIndexOf(SLASH);
        String fileName = index != -1 ? key.substring(index + SLASH.length()) : key;
        return THUMBNAILS + SLASH + fileName;
1 We want to handle S3EventNotification events.
2 Injection for S3Client.
3 Injection for ThumbnailGenerator.
4 Extra verification that the event is a ObjectCreated event.

5. S3

Create an S3 bucket.

Inside the bucket create two folders thumbnails and images.

6. Lambda

Create a Lambda Function. As a runtime, select Java 11 (Correto).

create function

6.1. IAM Role

Grant permissions to access the S3 bucket to the IAM role associated with the Lambda.

6.2. Triggers

Create two S3 trigger one for PNGs and one for JPGs:


Configure the trigger to:

  • Listen only to PUT events.

  • Only for files uploaded to the images folder

  • Only for file suffixed jpg

6.3. Upload Code

Create an executable jar including all dependencies:

./gradlew shadowJar

Upload it:

upload function code

6.4. Handler

In the AWS Console, as Handler, set:


6.5. Test

You can test it easily; upload a JPG file to the images folder of the S3 bucket you created. You can do this directly in the AWS Console. In a few seconds, you will see a thumbnail saved in the thumbnails folder of the bucket.

Read more about:

7. Help with the Micronaut Framework

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