Skip to content

Hello world app deployed as an AWS Lambda as a standard Java JAR and also as a native binary using the AWS CDK.

License

Notifications You must be signed in to change notification settings

kito99/graalvm-lambda-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graalvm-lambda-demo

Hello world app deployed as an AWS Lambda as a fat Java JAR and also as a GraalVM native image using the AWS CDK. Useful for comparing building/deploying native vs pure Java on Lambdas.

This demo was derived from:

Companion presentation: https://kitomann.com/#/sessions/7/scale-up-with-graalvm-and-aws-lambdas. See the presentation for background information (i.e. why use GraalVM, Lambda overview, etc).

Pre-requisites:

To build this project, you must have the following installed:

Building and deploying

Building and deployment is a two-step process:

  1. Build the Java JAR
cd function
mvn clean package

NOTE: You don't need to build the native image here, because this needs to be done on the target OS (Amazon Linux). It will be handled by the deployment process.

  1. Deploy to AWS Lambda

TIP: Make sure Docker Desktop is running before deploying.

NOTE: Currently this project is set to build using an ARM Docker image and deploy to Lambda ARM architecture (which is cheaper). To change to x86, edit InfrastructureStack.java.

The follow command does the following:

  1. Deploy the Java JAR using the AWS Java 11 Runtime
  2. Build a native image in an Amazon Linux Docker container and deploy a ZIP using an Amazon Linux custom runtime
cd infra
cdk deploy

NOTE: If the deployment fails, use cdk --debug deploy to get some more info about the root cause.

After deployment is complete, each deployment outputs a URL you can use for testing:

Outputs:
GraalVmLambdaDemoStack.HelloWorldGraalApiUrl = https://bchs2cynrg.execute-api.us-east-1.amazonaws.com/hello-world-graal
GraalVmLambdaDemoStack.HelloWorldJvmApiUrl = https://bchs2cynrg.execute-api.us-east-1.amazonaws.com/hello-world-jvm
GraalVmLambdaDemoStack.HelloWorldJvmSnapStartApiUrl = https://bchs2cynrg.execute-api.us-east-1.amazonaws.com/hello-world-jvm-snapstert

And you can use curl (or your browser) to test:

$ curl https://bchs2cynrg.execute-api.us-east-1.amazonaws.com/hello-world-jvm
Hello world!
$ curl https://bchs2cynrg.execute-api.us-east-1.amazonaws.com/hello-world-graal
Hello world!
$ curl https://bchs2cynrg.execute-api.us-east-1.amazonaws.com/hello-world-jvm-snapstert
Hello world!

If you log into the AWS Console, you'll be able to find the following artifacts:

  • CloudFormation Stack: GraalVmLambdaDemoStack
  • Lambda: GraalVmLambdaDemoStack-HelloWorldFunctionGraalVm
  • Lambda: GraalVmLambdaDemoStack-HelloWorldFunctionJvm
  • Lambda: GraalVmLambdaDemoStack-LogRetentiona
  • API Gateway Route: GraalVmLambdaDemoApi
    • Route: /hello-world-graal
    • Route: /hello-world-jvm

To see how these are configured, look at InfrastructureStack.java.

NOTE: I've also added another function called GraalVmLambdaDemoStack-HelloWorldFunctionJvmSnapstart. Currently this is exactly the same as HelloWorldFunctionJvm, because SnapStart doesn't appear to be supported yet in the Java CDK. You must manually enable it as described here: https://docs.aws.amazon.com/lambda/latest/dg/snapstart-activate.html#snapshot-console.

Performance

You can find performance info in the AWS Console; the easiest way is to view the Lambda function, Monitor -> Logs and expand the request:

Viewing logs in the AWS Console

In general, you can expect the native image to have a faster cold start time, a faster response time, and lower memory utilization.

Java Runtime Logs:

JVM Logs

Native Image Custom Runtime Logs:

Native Image Logs

About

Hello world app deployed as an AWS Lambda as a standard Java JAR and also as a native binary using the AWS CDK.

Resources

License

Stars

Watchers

Forks

Packages

No packages published