An open API service indexing awesome lists of open source software.

https://github.com/brcolow/lambda-java-runtime

Utility that makes deploying a custom AWS lambda runtime for any JDK version a breeze.
https://github.com/brcolow/lambda-java-runtime

aws-lambda jlink lambda-event-handler lambda-java-runtime

Last synced: 2 months ago
JSON representation

Utility that makes deploying a custom AWS lambda runtime for any JDK version a breeze.

Awesome Lists containing this project

README

        

# Java 13+ AWS Lambda Custom Runtime

# About

This custom AWS Java runtime makes it easy to specify any JDK version (including pre-releases published by the
[AdoptOpenJDK](https://adoptopenjdk.net/) project) and makes deploying the runtime to AWS as easy as running
`mvn install`. The runtime uses the [jlink](https://docs.oracle.com/en/java/javase/11/tools/jlink.html) utility of the
Java Platform Module System to create a stripped-down (lean) build of the JDK. It also uses
[Application Class-Data Sharing](https://openjdk.java.net/jeps/310), a feature introduced in Java 13. Both of these help
to reduce the JDK startup time.

# Supported Lambda Event Handler Methods

In order for the custom runtime to call your Lambda event handler it must use one of the following methods as it's
entry point.

* public void handleRequest(InputStream input, OutputStream output)
* public void handleRequest(InputStream input, OutputStream output, String context)

The second method will be passed a JSON String containing an [AWS Lambda Context object](https://github.com/aws/aws-lambda-java-libs/blob/master/aws-lambda-java-core/src/main/java/com/amazonaws/services/lambda/runtime/Context.java).

These are very general entry-points that can easily be used when handling a specific JSON object (such as an
`APIGatewayProxyRequestEvent` when using a Lambda function connected to an API Gateway) like so (using the
[jackson-jr](https://github.com/FasterXML/jackson-jr) library in this example):

```java
public class LambdaEventHandler {
public void handleRequest(InputStream input, OutputStream output, String context) throws IOException {
APIGatewayProxyRequestEvent event = JSON.std.with(JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY, false)
.beanFrom(APIGatewayProxyRequestEvent.class, input);
Context context = JSON.std.with(JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY, false)
.beanFrom(LambdaContext.class, contextJson);
// Do something with event.getHeaders(), event.getBody(), event.getQueryStringParameters(), etc.
// Return a APIGatewayProxyResponseEvent:
APIGatewayProxyResponseEvent response = handleEvent(event.getBody());
JSON.std.write(response, output);
}
}
```

It is not necessary for the Lambda function to extend or implement any abstract class or interface.

# Change JDK Version

The version of the JDK that will be used when deploying the runtime can be configured using the following
properties in the configuration of `gmavenplus-plugin` in `pom.xml`:

```pom


19
jdk
x64
linux
hotspot
2022-02-03-05-48

```

# Configure AWS Region

You can change the region that will be used for deploying the runtime by the `awsRegion` property of the
`gmavenplus-plugin` configuration in `pom.xml`:

```pom


US_WEST_2


```

# Configure jlink Modules

Because we use jlink to create a stripped-down build of the JDK it is necessary to explicitly add the modules that are
needed to run whatever lambdas will be run by the custom runtime. The list of modules can be specified by the `jlinkModules`
property of the `gmavenplus-plugin` configuration in `pom.xml`:

```pom


java.net.http,java.desktop,java.logging,java.naming,java.sql,java.xml,org.slf4j,org.slf4j.simple


```

# Deploy Runtime to AWS

The first step to deploy the runtime to your AWS account is to clone this repo. Next, the custom runtime is
deployed to AWS by running `mvn install`. To build the runtime without deploying to AWS (for testing, for example)
run `mvn package`.

# mvn-assembly-plugin

Make sure you have copied `src/assembly/lambda_deployment_package_assembly.xml` to your project.

# TODO (Public Consumption)

* Make it possible to supply a custom name for the published runtime.
* Allow for specifying a release version, not just a release date.

# Credit

This project was forked from [ andthearchitect/aws-lambda-java-runtime](https://github.com/andthearchitect/aws-lambda-java-runtime)
which provided a great starting point!