Creating an AWS Lambda Deployment JAR Using Maven
Bringing your Java to the cloud? Here are a couple of ways you can get a deployment JAR up and ready for AWS Lambda functions.
Join the DZone community and get the full member experience.
Join For FreeOne key aspect of AWS Lambda functions (with Java) is creating deployment packages (JAR or ZIP files) for uploading/deploying on the AWS Lambda service. In this post, you will learn different ways in which you can create a deployment JAR file for AWS Lambda projects using Maven. We will look at:
- Deployment JAR using Maven and Eclipse IDE
- Deployment JAR using Maven and the command prompt
I recommend using the "Maven and command prompt" technique for creating your deployment JAR package, but we will cover both methods.
Before getting started, download the AWS Toolkit for Eclipse from the Eclipse Marketplace. Here is the information on getting set up with AWS Toolkit for Eclipse.
Deployment JAR using Maven and Eclipse IDE
The following are steps required to create an AWS Lambda deployment JAR file using Eclipse IDE and Maven:
- Create an AWS Lambda Java Project using Eclipse > New > AWS Lambda Java Project
- You will be asked to provide project details, including input type — which is an Amazon services trigger used to invoke the Lambda function.
- Create your function. The following is a sample AWS Lambda function whose input type is an SNS event.
public class LambdaFunctionHandler implements RequestHandler<SNSEvent, String> { @Override public String handleRequest(SNSEvent event, Context context) { context.getLogger().log("Received event: " + event); String message = event.getRecords().get(0).getSNS().getMessage(); context.getLogger().log("From SNS: " + message); return message; } }
- Follow the steps mentioned in Creating a Deployment JAR Package Using Maven and Eclipse IDE to create the deployable JAR file. Primarily, the following needs to be done:
- From the context menu for the project in Package Explorer, click Run As > Maven Build…. In the Edit Configuration window, type package in the Goals text field and submit by clicking RUN.
- This step is needed for creating a standalone JAR file that contains the compiled customer code and the resolved dependencies from the pom.xml. Add the maven-shade-plugin plugin by right clicking on pom.xml and clicking on Maven > Add Plugin and adding following details:
- Group Id: org.apache.maven.plugins
- Artifact Id: maven-shade-plugin
- Version: 2.3
- From the context menu for the project in Package Explorer, click Run As > Maven Build…. In the Edit Configuration window, type package shade:shade in the Goals text field and submit by clicking RUN.
- You would see two JAR files based on your artifactId name and version. For example, if your artifactId name is demo, you would see demo-1.0.0.jar and original-demo-1.0.0.jar.
- Pick the file named as “demo-1.0.0.jar” for deploying as your AWS Lambda deployment JAR file.
- Upload the appropriate JAR file, as mentioned above in AWS S3 (recommended).
- Provide the link to the JAR file when creating Lambda functions.
Deployment JAR Using Maven Only (Without Eclipse IDE)
- Place the following maven-shade-plugin detail in your pom.xml:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin>
- Execute:
mvn package
- You should see two JAR files based on your artifactId name and version. For example, if your artifactId name is demo, you would see demo-1.0.0.jar and original-demo-1.0.0.jar.
- Pick the file named as “demo-1.0.0.jar” file for deployment on AWS Lambda.
- Upload the appropriate JAR file in AWS S3 (recommended).
- Provide the link to the JAR file when creating Lambda functions.
Further Reading/References
- Creating a Deployment JAR Using Maven without any IDE (Java)
- Creating a Deployment JAR Package Using Maven and Eclipse IDE
Summary
In this post, you learned about creating a deployment JAR package for AWS Lambda using Maven.
Did you find this article useful? Do you have any questions or suggestions about this article in relation to techniques used for creating deployment JARs for AWS Lambda? Leave a comment and ask your questions, and I shall do my best to address your queries.
Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments