Skip to content

AWS SAM application with a Lambda function written in Java and deployed with a custom Java runtime.

License

Notifications You must be signed in to change notification settings

rimironenko/aws-lambda-java-optimized-app

Repository files navigation

aws-lambda-java-optimized-app

This project contains an AWS Lambda Maven application and scripts to package and deploy it in three different ways:

  • As a Lambda function with Java 11 runtime.
  • As a Lambda function with custom GraalVM Java 17 runtime packaged as the Lambda Layer.
  • As a Lambda function with custom Java 19 (Amazon Corretto) runtime packaged as the Lambda layer.

All the options can be tested by the Artillery script to collect a performance benchmark.

Architecture

The architecture is presented in the picture given below.

Architecture

  1. The https://www.artillery.io/ load script sends requests to API Gateway

  2. API gateway invokes the Lambda function written in Java

  3. The Lambda function writes a simple object that consists of Amazon Request ID to DynamoDB table using the DynamoDB-enhanced library from AWS Java 2 SDK

Prerequisites

Project structure

Build

The application can be built by three different options listed below. For custom runtimes, ensure that you have a Docker running.

Java 11 runtime

cd lambda-app/ && mvn clean package -P java11

GraalVM Java 17 custom runtime

sh ./build-java17-graalvm.sh

Amazon Corretto Java 19 custom runtime

sh ./build-java19-custom.sh

Deployment

Depending on the build option, you need a particular SAM template to deploy the artifacts crated by the build option chosen.

Java 11 runtime

sam deploy -t template-java11-runtime.yaml --stack-name java11

GraalVM Java 17 custom runtime

sam deploy -t template-java17-graalvm-runtime.yaml --stack-name java17-graalvm

Amazon Corretto Java 19 custom runtime

sam deploy -t template-java19-custom-runtime.yaml --stack-name java19-custom

Artillery load test

To test the deployed application by Artillery, you need to note the API gateway endpoint from AWS SAM Outputs and run the following command:

artillery run -t {PUT_YOUR_ENDPOINT_HERE} -v '{ "url": "/items/" }' loadtest.yaml

Cold Start and Warm Start timings

Get the Lambda Cold Start and Warm Start timings by the following CloudWatch query:

filter @type = "REPORT"
    | parse @log /\d+:\/aws\/lambda\/(?<function>.*)/
    | stats
    count(*) as invocations,
    pct(@duration+coalesce(@initDuration,0), 0) as p0,
    pct(@duration+coalesce(@initDuration,0), 25) as p25,
    pct(@duration+coalesce(@initDuration,0), 50) as p50,
    pct(@duration+coalesce(@initDuration,0), 75) as p75,
    pct(@duration+coalesce(@initDuration,0), 90) as p90,
    pct(@duration+coalesce(@initDuration,0), 95) as p95,
    pct(@duration+coalesce(@initDuration,0), 99) as p99,
    pct(@duration+coalesce(@initDuration,0), 100) as p100
    group by function, ispresent(@initDuration) as coldstart
    | sort by coldstart, function

About

AWS SAM application with a Lambda function written in Java and deployed with a custom Java runtime.

Topics

Resources

License

Stars

Watchers

Forks