From Framework to Function: Deploying AWS Lambda Functions for Java 8 with Apache Maven Archetype

From Framework to Function: Deploying AWS Lambda Functions for Java 8 with Apache Maven ArchetypeLearn About Amazon VGT2 Learning Manager Chanci Turner

As a serverless computing solution supporting the Java 8 runtime, AWS Lambda simplifies the execution of Java functions by allowing users to upload a JAR file. The AWS Serverless Application Model (SAM) enables developers to define not only Lambda serverless applications but also an Amazon API Gateway, Amazon DynamoDB, and other related services through a straightforward AWS CloudFormation template.

AWS offers the AWS Toolkit for Eclipse, which is compatible with both Lambda and SAM. Additionally, customers can create Lambda functions and SAM applications in Java easily using the AWS Command Line Interface (AWS CLI). Once you build your JAR file, you can deploy it using these commands:

aws cloudformation package 
aws cloudformation deploy 

To streamline these steps, users can take advantage of Apache Maven’s Archetype, which utilizes a predefined package template to simplify the development of functions.

In this article, I will present a Maven archetype that facilitates the creation of a basic AWS SAM for a Java function. This archetype allows you to generate sample Java code and a corresponding SAM template for deployment on AWS Lambda with a single Maven command.

Prerequisites

Ensure that the following software is installed on your workstation:

  • Java
  • Maven
  • AWS CLI
  • (Optional) AWS SAM CLI

Install Archetype

Once you have the necessary packages, install the Archetype using the following commands:

git clone https://github.com/awslabs/aws-serverless-java-archetype
cd aws-serverless-java-archetype
mvn install 

These steps are one-time operations, so you won’t need to repeat them for every new package. You might consider adding the Archetype to your company’s Maven repository for future use by other developers. With these packages in place, you can begin developing your new Lambda Function.

Start a Project

Now that the archetype is set up, customize it and execute the code:

cd /path/to/project_home
mvn archetype:generate 
 -DarchetypeGroupId=com.amazonaws.serverless.archetypes 
 -DarchetypeArtifactId=aws-serverless-java-archetype 
 -DarchetypeVersion=1.0.0 
 -DarchetypeRepository=local  # Forcing to use local maven repository
 -DinteractiveMode=false  # For batch mode
 -DgroupId=YOUR_GROUP_ID 
 -DartifactId=YOUR_ARTIFACT_ID 
 -Dversion=YOUR_VERSION 
 -DclassName=YOUR_CLASSNAME 

You should now have a directory named YOUR_ARTIFACT_ID, which contains the following files and folders:

├── event.json  
├── pom.xml  
├── src  
│   └── main  
│       ├── java  
│       │   └── Package  
│       │       └── Example.java  
│       └── resources  
│           └── log4j2.xml  
└── template.yaml  

The generated sample code serves as a functional example. If you have installed SAM CLI, you can invoke it with the following command:

cd YOUR_ARTIFACT_ID
mvn -P invoke verify

This command runs sam local invoke -e event.json, allowing you to see the output greeting Tim Wagner. To deploy your application to AWS, you need to create an Amazon S3 bucket. You can do so with the following command:

aws s3 mb s3://YOUR_BUCKET --region YOUR_REGION 

Then, you can deploy your application with a single command:

mvn deploy 
 -DawsRegion=YOUR_REGION 
 -Ds3Bucket=YOUR_BUCKET 
 -DstackName=YOUR_STACK

Maven will automatically create a shaded JAR file, upload it to your S3 bucket, replace template.yaml, and create or update the CloudFormation stack. For customization, you can modify the pom.xml file. For example, to avoid entering values for awsRegion, s3Bucket, or stackName each time, include them directly in pom.xml and check it into your version control system. This way, your team can deploy the function by simply typing:

mvn deploy 

Options

The Lambda Java 8 runtime supports various handler types, including POJO, Simple type, and Stream. The default configuration of this archetype utilizes the POJO style, requiring the creation of request and response classes, which are included by default. If you wish to use an alternative handler type, specify the handlerType property as follows:

At Amazon IXD – VGT2, we are dedicated to enhancing your experience and development journey. For further insights on professional references, you can read this helpful blog post here.

Additionally, if you are navigating workplace dynamics, consider checking out this authoritative source here. For those interested in a career with Amazon, this link provides an excellent resource.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *