Maven Archetype for creating minimal Microlam Lambda Quickstart project
To use it:
mvn archetype:generate -Dfilter=io.microlam:microlam-lambda-quickstart
- Choose the the number corresponding to 'microlam-lambda-quickstart'
- Choose a
groupId
,artifactId
,version
andpackage
. - Choose your
awsBucket
: this is the name of an S3 bucket where the lambda artifacts will be deployed (new or existing bucket) - Choose your
awsProfile
: this is the name of your AWS profile - Choose your
awsRegion
: this is the name of an AWS region (ex:eu-west-1
) - Choose your
lambdaName
: this is the name of your Lambda must be also suitable as the name of the Java class for the Lambda.
There are 2 API endpoints defined in our API via API Gateway:
POST /mult
POST /sum
They are connected to the same lambda via Proxy integration.
Both receive a json body of the form:
{
"arguments" : [ 2, 4, 6]
}
The expected response will be a json of the form:
{
"result" : 12
}
The result will be the sum or the product of the arguments depending on the respective endpoints /sum
or /mult
.
The project will generate a Java 21 lambda and also a Custom Runtime Lambda using GraalVM compilation of the same java code with native-image.
This project implements the Lambda code with the Microlam Simple architecture.
mvn package
the Java deployment package is in target/
folder with the name [xxx]-aws-lambda.jar
Note: -Pjava not needed anymore as the profile java is activated by default. If you want to disable it you may provide -P-java to disable it.
You have 3 options:
- Create the API and the Lambda automatically from the command line using AWS CDK
- Create the API and the Lambda automatically from the command line using AWS SAM
- Create the API and the Lambda manually via the AWS Console
Deploy the App with the CDK:
cdk deploy
Info: if something go wrong, or if you want to clean the AWS Account, you can delete everything related to the project in AWS with the command cdk destroy
Deploy the App with SAM:
sam deploy
Info: if something go wrong, or if you want to clean the AWS Account, you can delete everything related to the project in AWS with the command sam delete
- Using the
profile
,region
andlambdaName
you specified. - Using the Java 21 (Corretto) Lambda Runtime
- Set the handler to
[package].lambda.[lambdaName]
- Create the API in API Gateway as described before in the Project Description
Then Deploy your AWS Lambda Java Deployment Package:
Run the Junit Test in class in
[xxx].devops.UploadAndUpdateLambda
Currently AWS has no official support for Java 20/19 but a Custom Runtime is provided for both amd64 and arm64 as a Lambda Layer for Java 20 or Lambda Layer for Java 19.
First modify the pom.xml property:
<release.version>20</release.version>
or
<release.version>19</release.version>
Then ensure your build is using a JDK20+ or JDK19+.
mvn package -Djava20layer=axx64
or
mvn package -Djava19layer=axx64
where axx64
is amd64
or arm64
.
This brings the java 20 layer
or java 19 layer
in your target/
folder.
Using the CDK is the simplest way to deploy the Java 20/19 Layer (But using the AWS console is also possible).
See and update as necessary the Class [xxx].devops.cdk.CreateApp
(from src/test
folder).
Warning: For this, the pre-requisite is that Docker is installed and running on your machine, if not install it.
- You need to configure your paths (so docker command can be found) in the
toolchains.xml
file in~/.m2/toolchains.xml
like this:
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<toolchain>
<type>paths</type>
<provides>
<id>binaries</id>
</provides>
<configuration>
<!-- Defines the folders to search for binaries of the "paths" toolset -->
<paths>
<path>/usr/local/bin</path>
</paths>
</configuration>
</toolchain>
</toolchains>
-
The native build is depending on the java version (
java11
orjava17
orjava19
orjava20
orjava21
) and the target architecture (amd64
orarm64
). You need to provide this information in maven command line using-Dnative=javaXX-axx64
(by replacing XX and xx with the correct values) -
You need to activate the profile
compile
with-Pcompile
Note: For Java 21, 17 and Java 20 you may use the (Oracle Graalvm Free version)[https://medium.com/graalvm/a-new-graalvm-release-and-new-free-license-4aab483692f5] with the command -Dnative=oracle-javaXX-axx64
.
In case you choose to build from Java 21
targeting amd64
architecture:
mvn package -Dnative=java21-amd64 -Pcompile
Excellent! The Native deployment package is in target
folder with the name [xxx]-aws-lambda-native.zip
.
See why... it certainly means you need to complete the native-image configuration. see below the instructions for running the container for generating the configuration for you.
- If you want to test the previous build and allow you to run some tests, use the profile
run
:-Prun
mvn package -Dnative=java21-amd64 -Prun
Good ! At the end of this command, a container is running, letting you try your native lambda locally.
It is a good time to run your tests on it:
Run the Junit Tests in class in
[xxx].devops.LocalLambdaTests
If it is working as expected, you are ready to deploy it to AWS!
You can now stop the running container, with CTRL-C and are ready to upload your deployment package to AWS.
You have 3 options:
- Create the API and the Lambda automatically from the command line using AWS CDK
- Create the API and the Lambda automatically from the command line using AWS SAM
- Create the API and the Lambda manually via the AWS Console
You need to apply a few changes to the class [xxx].devops.cdk.CreateApp
(from src/test
folder), to set the function Runtime, Architecture and Code.
Deploy the App with the CDK:
cdk deploy
Deploy the App with SAM:
sam deploy --template template-native.yaml
Info: if something go wrong, or if you want to clean the AWS Account, you can delete everything related to the project in AWS with the command sam delete
- Using the
profile
,region
and[lambdaName]Native
you specified. - Using the 'Custom runtime on Amazon Linux 2' Lambda Runtime
- Set the handler to
[package].lambda.[lambdaName]
Then Deploy your AWS Lambda Native Deployment Package
Run the Junit Test in in class in
xxx.devops.UploadAndUpdateLambdaNative
mvn package -Dnative=java21-amd64 -Pconfig
At the end of the build, a container is running, letting you try your Java lambda locally with the GraalVM Tracing Agent.
It is a good time to run your tests on it :
Run the Junit Tests in class in
[xxx].devops.LocalLambdaTests
The generated configuration is updated every 30s in folder: target/[artifactId]-[version]-native-config/java11-amd64/function/config/
.
You can manually copy the files to the folder src/main/resources/META-INF/native-image/[groupId]/[artifactId]/
.
If necessary, update the file native-image.properties
, stop the running container with CTRL-C and retry to compile.