Deploying a Java package to AWS Lambda with Maven

Michael Madden
The Startup
Published in
5 min readJun 25, 2019
Picture by: Valve Software [CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0)]

Deploying code to AWS Lambda can be a walk in the park, especially when you’re using a language that can be written inside Lambda’s internal text editor, such as Node.js or Python, and not Java.

In order to deploy a function to Lambda in Java, you first need to package it up in either a jar or zip file. Although this is a seemingly simple task, it can be challenging for a Java developer who lacks experience with Maven and Gradle.

Now, that’s fine. The issue, however, is that in order to get it in the proper .zip or .jar format, a package manager such as Maven or Gradle needs to be used. (A plugin for Eclipse that utilizes the AWS SDK is another option).

In this tutorial, I’ll walk you through the process with Maven using the command line.

Getting Started:

  • Installing the Needed Software
  • Creating a New Project to Test Lambda

Configuring the Project for Maven:

  • Making sure the Project can Compile with Maven
  • Adding the AWS dependencies for Maven

Finishing Up

  • Compiling the Project
  • Uploading the Project to Lambda

Getting Started

Before we need can deploy our package to lambda, we first need to make sure that we have maven installed, and that we have a Java project that can interface with AWS Lambda.

Installing the Needed Software

Before you do anything, install maven onto your computer. Once you have it installed, go into your terminal and check that it’s configured correctly by running mvn --version

You should get a response similar to:

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5...; 2018-10-24T14:41:47-04:00)

Creating a New Project to Test Lambda

In your favorite Java IDE (mine is Intellij), create a new maven project. In your src directory, create a package named example. Then inside example, create a new Java class called Hello. Delete the code that was auto-generated, and add in the code below (taken from the AWS documentation):

package example;import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.Context;
public class Hello implements RequestStreamHandler{
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
int letter;
while((letter = inputStream.read()) != -1)
{
outputStream.write(Character.toUpperCase(letter));
}
}
}

In this tutorial, I’ll be using Git Bash for Windows. Any terminal will work, however. Once you have your terminal open, navigate to the root directory of the project you want to deploy on Lambda with cd <path to directory>.

NOTE: if mvn isntall says you’re pointing to the wrong path, set the JAVA_HOME environment system variable to the jdk in program files

Configuring the Project for Maven

If you created the project initially as a maven project, you should be all set to compile it and push it up to AWS Lambda!

Otherwise, follow the steps below to configure it as one.

Making sure the Project can Compile with Maven

First, you’ll need to add a pom.xml (Project Object Model) in the main directory of your project. The pom.xml contains the metadata for your project, such as what class should be treated as the project entry point, and what dependencies the project needs.

You can find a list of pom.xml examples here on the maven website. You can also use the code snippet below as a template to build your pom.xml file off of.

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>

To add this file using the command line, navigate to your main project directory, the one containing the src folder, and run touch pom.xml

This will create a pom.xml file. Next, type in vim pom.xml to open the file in the vim editor. Press the key i to enter insert mode, and then press <shift> <insert> at the same time to paste the above code from your clipboard.

Press <escape> to exit Vim’s insert mode, and then enter wq! and press <enter> to save and exit vim

Adding the AWS dependencies for Maven

In order to interface with AWS lambda, you need to import the RequestStreamHandler in your code.

With Maven, any imports you have need to be specified in the pom.xml file, where they are referred to as dependencies.

The specific dependency you need for the RequestStreamHandler is the aws-lambda-java-core dependency. To add this to your pom.xml file, open it up in Vim again, and add the below code inside the <project>...</project> tags.

<dependencies>    
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

Individual dependencies are contained in <dependency> tags, and those dependencies are contained in <dependencies> tags.

When the project is compiled with maven, these dependency listings will be used to download the corresponding source code from online repositories into your project.

Finishing Up

Once you have a project set up and ready to integrate with AWS Lambda, it’s time to compile it with Maven!

Compiling Your Project

In your terminal, navigate to the main project directory. This is the directory that contains your pom.xml and src directory.

Run mvn install

You should see an output like this (Fig. 1)with build success if your code was compiled successfully:

Fig. 1. Maven built successfully (Picture by: Michael Madden)

Uploading the Project to Lambda

Once your build is compiled successfully, it’s time to upload the .jar file maven created.

This file will be located in the target directory that maven created. The path to this file will be

<Your Project directory>/target/<NameOfProject-SNAPSHOT.jar>

Open up the Lambda console in AWS, go to the function where you want to upload this project, and select upload zip or .jar file as in Fig 2.

Fig. 2. Upload the .jar file (Picture by: Michael Madden)

Congratulations! You’ve successfully deployed your Java package to AWS Lambda!

--

--

Michael Madden
The Startup

Computer Science student at Penn State | Developer for Regavi CMS