Deploy your Java Lambda easily with Microlam!

Frank Afriat
5 min readFeb 4, 2022

Disclaimer: I am the author of the Java micro-framework Microlam.

Why a new Java Framework ?

While excellent frameworks, like Micronaut and Quarkus, are already available for that, I found that it is often difficult to enter into their worlds, because their scope is so big that a beginner may be overwhelmed with the amount of knowledge you need to start with them.

On the contrary, Microlam is a Java micro-framework, which means that there is a very small learning curve before being able to use its services and this is because its objective is only to help every Java developers to target AWS Serverless… no more.

While Microlam targets beginners, experienced users will also enjoy the simplicity of advanced features, like compiling from any system, Linux, MacOS or Windows, your Java 8, Java 11 or Java 17 code to target AWS Lambda with the architecture you choose amd64 or arm64

In case you are new to AWS Cloud, it is time to begin!

AWS (Amazon Web Services) is the leader of all cloud providers. A revolution in the cloud was to offer Serverless Services which means that they are managed services and you pay only what you consume, giving this wonderful opportunity to begin your dreamed project without any upfront costs and very easily. The main Serverless Service is AWS Lambda giving you the possibility to run a given piece of code when a given event is occurring, for example, you may have a Web site or an App using a REST API, created with ease with AWS API Gateway, which redirect the work to AWS Lambda. In case you don’t have already an account, you may create it in 2 minutes and get a one year free offer including 1.000.000 lambda requests / month !!!!

Last point: AWS S3 is the AWS Serverless Service for storing files inside an AWS bucket (which work like a folder). You will need to use one bucket for storing the AWS Lambda deployment package containing your code.

What you need to start ?

  • Create an AWS Account, install the AWS CLI and configure an aws named profile, then install also the AWS CDK CLI.
  • Install Docker (just in case you want to compile natively)
  • Be a Java developer with an IDE, a JDK 11+ installed, maven installed, but this you should use them already. Note that AWS is providing a free supported OpenJDK build named Corretto for Java 11 or Java 17… that is used inside the AWS Java Lambda Runtime, so it is a good idea to choose it.

Let’s begin!

For a complete presentation, see the complete getting started guide.

From your Terminal run:

mvn archetype:generate -DarchetypeGroupId=io.microlam -DarchetypeArtifactId=microlam-lambda-quickstart -DarchetypeVersion=0.8.4

You will have to answer a few questions:

  • Choose a groupId, artifactId, version and package for your project.
  • Choose your awsBucket : this is the name of the S3 bucket where the lambda artifacts will be deployed
  • Choose your awsProfile : this is the name of your AWS named profile
  • Choose your awsRegion: this is the name of an AWS region (ex: eu-west-1), choose one next to your users.
  • Choose your lambdaName: this is the name of your Lambda (must be also suitable as the name of the Java class for the Lambda).

That’s all !

Your project is ready to be imported in your favorite IDE and deployed to AWS !

Take some time to read the code, look at the Readme, and if you have any question refer the guide.

This is the architecture of the project:

AWS Architecture of the getting started project

Let’s compile the Java code and generate a Java Lambda Package:

mvn package -Pjava

You will find a new artifact with the suffix aws-lambda.jar in your target/ folder, this is the code package for your Lambda.

Deployment with AWS CDK

The CDK allow you to deploy your architecture and your code using Java code. The getting started example includes already a test class, named CreateApp you can use to deploy to AWS:

cdk deploy --profile <your named profile>

where you will have replaced <your named profile> with your named profile.

If you never used the CDK before, you will need to bootstrap the CDK, only the first time, with:

cdk bootstrap --profile <your named profile>

If everything is successful, you will see in the console this:

Outputs:
microlam-lambda.microlamlambdaapiEndpointXXXXX = https://xxxxxxxxx.execute-api.<region>.amazonaws.com/prod/

Which gives you the URL for accessing your new API!

Let’s test the new API with curl

curl -d "{\"arguments\":[2, 4, 6]}" -H "Content-Type: application/json" https://xxxxxxxxx.execute-api.<region>.amazonaws.com/prod/sum

You should get this response:

{"result":12}

How to clean your AWS Account

After playing with the API and checking the result in AWS console (check API Gateway, Lambda, S3 in your region), you may want to clean your AWS account.

Just use this command:

cdk destroy --profile <your named profile>

Remarks on using a Java Lambda

The only problems we could find on using a Java Lambda are the problem of the cold start and the requirement on memory.

But this should not be a problem for most of your projects.

Also to mitigate the cold start problem, Microlam is using a proxy Lambda, which means that only one lambda is effectively deployed to AWS, hoping that every call to the API will keep your Lambda warm, and the call is redirected internally to the good Lambda.

The real solution to this problem is to require a native compilation using GraalVM native-image tool and to deploy your code using Lambda Custom Runtime.

Hopefully Microlam is easing also this process and you are only one command away from it which I let you discover in the Microlam Get Started steps or in the complete Getting Started Guide. It would be the subject of my next story…

Conclusion

Even if you begin with Cloud development, I hope you will find a precious help using Microlam micro-framework.

This project is very new and if you find it useful (or not), please give me your feed back:

--

--

Frank Afriat

Founder and CTO of Solusoft.Tech, Java Expert & Architect, AWS Serverless happy user and flutter early adopter.