Java AWS Lambda Example with Serverless and Maven

Last Updated:  January 16, 2024 | Published: May 8, 2020

With this blog post, we'll learn how to use the Serverless framework to develop and deploy AWS Lambda functions using Java and Maven. To provide a realistic use case, we'll develop an AWS Lambda that will generate and store a thumbnail for images as soon as they're uploaded to an S3 bucket. We're going to use Java 11, Serverless v3 and the AWS Java SDK v2.

AWS Lambda Java Maven Serverless Example

AWS Account Setup for Serverless

(If your AWS account is eligible for the free tier, you can mirror this setup in your own account free of cost.)

For deploying our Java function to AWS Lambda we'll use the Serverless framework. This framework simplifies the deployment of functions to different cloud providers (e.g. AWS Lambda, Azure Functions, etc.) with an abstraction layer. Alternative tools to get our Java function deployed to AWS Lambda are AWS SAM, CloudFormation, or the AWS CDK.

The Serverless framework takes care to create all required AWS resources for our AWS Lambda function. This includes the S3 Bucket for the Java code, IAM roles, and the Lambda function configuration. We don't have to fiddle around with long CloudFormation YAML templates at all.

We install the Serverless CLI via npm:

Next, we have to set up the AWS credentials for our AWS account. We can follow the instructions on their documentation for this and create a new IAM user in your AWS account called serverless-admin.

Once we have the access key and secret for this new user ready, we can store these credentials in a dedicated profile:

Maven Java Project Setup for AWS Lambda

Our Maven project is a plain Java 11 project with several AWS-specific dependencies:

As we'll access images in an S3 bucket, we need the official AWS Java SDK for this. Therefore, we include the AWS Java SDK v2 BOM to manage and align our s3 dependency.

Next, the aws-lambda-java-events contains Java classes that represent various events on AWS. This includes the S3Event that we can trigger whenever there's a file upload to a specific bucket. Using this library, we get type-safe access to the payload of the event.

Finally, the aws-lambda-java-core dependency contains the RequestHandler<I, O> we have to implement to create a Java-based Lambda function.

As of now, AWS Lambda provides Java 8 and 11 runtimes, but we can also ship our own container image to run more recent Java code on AWS Lambda.

Furthermore, we have to create a Uber Jar and package all dependencies with the maven-shade-plugin.

With this setup, we'll create an 12 MB .jar large file. As for each new AWS Lambda function deployment we have to upload this file to an S3 bucket (this will be handled by the Serverless framework), we should try to keep the size of our .jar as small as possible.

Creating Image Thumbnails with Java and AWS Lambda

Writing an AWS Lambda function with Java requires implementing the RequestHandler<I, O> interface. This interface takes two parameters: one for the incoming input (I) of the Lambda function and one for the return type (aka. output O).

The input Java class can be any plain old Java object that maps to our input payload. AWS Lambda takes care of serializing the incoming payload using Jackson. The same is true for the return type of our AWS Lambda function. AWS Lambda deserializes our return type to JSON by default. If we don't want to use this default serialization behavior, we can implement the RequestStreamHandler interface instead.

As our Lambda function will be triggered by the S3 ObjectCreated event, we can use the S3Event (from aws-lambda-java-events) as the input type. The return type can be Void in this case, as we don't return anything and just process the uploaded image to create a thumbnail of it.

The incoming S3Event comes with information about which file was uploaded. We can extract this information and use the S3 Java client to get the image. AWS Lambda executes the Java function with the specified IAM role of the Lambda function, we don't have to provide any credentials to construct the client. Just ensure the IAM role has enough rights to perform the S3 operations.

Once we download the uploaded image, we perform the thumbnail generation with the help of BufferedImage and ImageIO. The thumbnail size is obtained from an environment variable we'll configure with Serverless in the next section.

Please note that this is a prototype-ish implementation of this thumbnail generation, there might be more elegant ways of doing it:

Once the image is processed, we'll store the thumbnail in S3 with the following file structure: thumbnails/100x100-nameOfImage.png.

Deploying the AWS Lambda with Serverless

As the final step, we'll configure Serverless to deploy our AWS Lambda function. For this, we'll create a serverless.yml file in the root of our project (next to our pom.xml).

First, we have to configure some information about the provider. Among other things, this includes the name of the cloud provider, the runtime, the region. Furthermore, you can specify the AWS profile you configured your credentials locally (otherwise it will use the default profile).

As the Lambda function needs access to the S3 bucket we'll use to upload images to, we have to also adjust the IAM roles and grant access to also the S3 deployment bucket which contains a ZIP file of our .jar :

Next, we have to tell Serverless where to find our build artifact and define the function:

This points to the .jar file and the handler attribute contains the fully qualified class name of our Java handler class. With the events attribute we specify the way our function is triggered. We limit this to include only the creation of objects in S3 with the uploads/ prefix and .png suffix e.g. uploads/myProfilePicture.png. While specifying the name of the S3 bucket, Serverless also ensures to create the bucket for us.

We can now deploy everything with serverless deploy -v. Make sure to build the Maven project with mvn package first.

Testing Time

Once the Lambda function is deployed, we can upload a .png file to the S3 bucket and should find the thumbnail shortly after:

Please note that the first Lambda execution is a cold start and might require more time.

The first run takes approx. 6 seconds for me, but all subsequent executions finish in 400 – 500ms.

To remove the AWS Lambda and all the supporting infrastructure that the Serverless framework created, make sure the S3 image bucket is empty and run:

For more Java AWS Lambda examples, consider the following blog posts:

The source code for this Java Lambda function using the Serverless framework and Maven is available on GitHub.

Have fun deploying Java functions to AWS Lambda using the Serverless framework,

Philip

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >