Skip to content

feat(serialization): Add GraalVM metadata configuration #1905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<module>powertools-examples-idempotency</module>
<module>powertools-examples-parameters/sam</module>
<module>powertools-examples-parameters/sam-graalvm</module>
<module>powertools-examples-serialization</module>
<module>powertools-examples-serialization/sam</module>
<module>powertools-examples-serialization/sam-graalvm</module>
<module>powertools-examples-kafka</module>
<module>powertools-examples-batch</module>
<module>powertools-examples-validation</module>
Expand Down
14 changes: 14 additions & 0 deletions examples/powertools-examples-serialization/sam-graalvm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#Use the official AWS SAM base image for Java 21
FROM public.ecr.aws/sam/build-java21:latest

#Install GraalVM dependencies
RUN curl -4 -L curl https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_linux-x64_bin.tar.gz | tar -xvz
RUN mv graalvm-jdk-21.* /usr/lib/graalvm

#Make native image and mvn available on CLI
RUN ln -s /usr/lib/graalvm/bin/native-image /usr/bin/native-image
RUN ln -s /usr/lib/maven/bin/mvn /usr/bin/mvn

#Set GraalVM as default
ENV JAVA_HOME=/usr/lib/graalvm
ENV PATH=/usr/lib/graalvm/bin:$PATH
13 changes: 13 additions & 0 deletions examples/powertools-examples-serialization/sam-graalvm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
build-APIGatewayDeserializationFunction:
mvn clean package -P native-image
chmod +x target/hello-world
cp target/hello-world $(ARTIFACTS_DIR) # (ARTIFACTS_DIR --> https://github.com/aws/aws-lambda-builders/blob/develop/aws_lambda_builders/workflows/custom_make/DESIGN.md#implementation)
chmod +x src/main/config/bootstrap
cp src/main/config/bootstrap $(ARTIFACTS_DIR)

build-SQSEventDeserializationFunction:
mvn clean package -P native-image
chmod +x target/hello-world
cp target/hello-world $(ARTIFACTS_DIR) # (ARTIFACTS_DIR --> https://github.com/aws/aws-lambda-builders/blob/develop/aws_lambda_builders/workflows/custom_make/DESIGN.md#implementation)
chmod +x src/main/config/bootstrap
cp src/main/config/bootstrap $(ARTIFACTS_DIR)
111 changes: 111 additions & 0 deletions examples/powertools-examples-serialization/sam-graalvm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Powertools for AWS Lambda (Java) - Serialization Example

This project contains an example of Lambda function using the serialization utilities module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/serialization/).

The project contains two `RequestHandler`s -

* [APIGatewayRequestDeserializationFunction](src/main/java/org/demo/serialization/APIGatewayRequestDeserializationFunction.java) - Uses the serialization library to deserialize an API Gateway request body
* [SQSEventDeserializationFunction](src/main/java/org/demo/serialization/SQSEventDeserializationFunction.java) - Uses the serialization library to deserialize an SQS message body

In both cases, the output of the serialized message will be printed to the function logs. The message format
in JSON looks like this:

```json
{
"id":1234,
"name":"product",
"price":42
}
```

## Deploy the sample application

This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting
started with SAM in [the examples directory](../../README.md)

## Configuration

- Set the environment to use GraalVM

```shell
export JAVA_HOME=<path to GraalVM>
```

## Build the sample application

- Build the Docker image that will be used as the environment for SAM build:

```shell
docker build --platform linux/amd64 . -t powertools-examples-serialization-sam-graalvm
```

- Build the SAM project using the docker image

```shell
sam build --use-container --build-image powertools-examples-serialization-sam-graalvm
```

#### [Optional] Building with -SNAPSHOT versions of PowerTools

- If you are testing the example with a -SNAPSHOT version of PowerTools, the maven build inside the docker image will fail. This is because the -SNAPSHOT version of the PowerTools library that you are working on is still not available in maven central/snapshot repository.
To get around this, follow these steps:
- Create the native image using the `docker` command below on your development machine. The native image is created in the `target` directory.
- `` docker run --platform linux/amd64 -it -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 powertools-examples-serialization-sam-graalvm mvn clean -Pnative-image package -DskipTests ``
- Edit the [`Makefile`](Makefile) remove this line
- `mvn clean package -P native-image`
- Build the SAM project using the docker image
- `sam build --use-container --build-image powertools-examples-serialization-sam-graalvm`


## Test the application

### 1. API Gateway Endpoint

To test the HTTP endpoint, we can post a product to the test URL:

```bash
curl -X POST https://gct1q3gaw0.execute-api.eu-west-1.amazonaws.com/Prod/product/ -H "Content-Type: application/json" -d '{"id": 1234, "name": "product", "price": 42}'
```

The result will indicate that the handler has successfully deserialized the request body:

```
Received request for productId: 1234
```

If we look at the logs using `sam logs --tail --stack-name $MY_STACK`, we will see the full deserialized request:

```json
{
...
"level": "INFO",
"loggerName": "org.demo.serialization.APIGatewayRequestDeserializationFunction",
"message": "product=Product{id=1234, name='product', price=42.0}\n",
...
}
```

### 2. SQS Queue
For the SQS handler, we have to send a request to our queue. We can either construct the Queue URL (see below), or
find it from the SQS section of the AWS console.

```bash
aws sqs send-message --queue-url "https://sqs.[REGION].amazonaws.com/[ACCOUNT-ID]/sqs-event-deserialization-queue" --message-body '{"id": 1234, "name": "product", "price": 123}'
```

Here we can find the message by filtering through the logs for messages that have come back from our SQS handler:

```bash
sam logs --tail --stack-name $MY_STACK --filter SQS
```

```bash
{
...
"level": "INFO",
"loggerName": "org.demo.serialization.SQSEventDeserializationFunction",
"message": "products=[Product{id=1234, name='product', price=42.0}]\n",
...
}

```
89 changes: 89 additions & 0 deletions examples/powertools-examples-serialization/sam-graalvm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>software.amazon.lambda.examples</groupId>
<version>2.1.1</version>
<artifactId>powertools-examples-serialization-sam-graalvm</artifactId>
<packaging>jar</packaging>
<name>Powertools for AWS Lambda (Java) - Examples - Serialization GraalVM</name>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging-log4j</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-serialization</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.16.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Don't deploy the example -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.10.1</version>
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>build</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<imageName>hello-world</imageName>
<mainClass>com.amazonaws.services.lambda.runtime.api.client.AWSLambda</mainClass>
<buildArgs>
<!-- required for AWS Lambda Runtime Interface Client -->
<arg>--enable-url-protocols=http</arg>
<arg>--add-opens java.base/java.util=ALL-UNNAMED</arg>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e

./hello-world $_HANDLER
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"name":"com.amazonaws.services.lambda.runtime.LambdaRuntime",
"methods":[{"name":"<init>","parameterTypes":[] }],
"fields":[{"name":"logger"}],
"allPublicMethods":true
},
{
"name":"com.amazonaws.services.lambda.runtime.LambdaRuntimeInternal",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allPublicMethods":true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"name": "com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent",
"allDeclaredFields": true,
"allDeclaredMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent$ProxyRequestContext",
"allDeclaredFields": true,
"allDeclaredMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent$RequestIdentity",
"allDeclaredFields": true,
"allDeclaredMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent",
"allDeclaredFields": true,
"allDeclaredMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allPublicMethods": true,
"allDeclaredClasses": true,
"allPublicClasses": true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"name":"com.amazonaws.services.lambda.runtime.api.client.runtimeapi.LambdaRuntimeClientException",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","int"] }]
},
{
"name":"com.amazonaws.services.lambda.runtime.api.client.runtimeapi.InvocationRequest",
"fields":[{"name":"id"}, {"name":"invokedFunctionArn"}, {"name":"deadlineTimeInMs"}, {"name":"xrayTraceId"}, {"name":"clientContext"}, {"name":"cognitoIdentity"}, {"name":"content"}],
"allPublicMethods":true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Args = --initialize-at-build-time=jdk.xml.internal.SecuritySupport
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"name":"com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.databind.deser.Deserializers[]"
},
{
"name":"com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.databind.ext.Java7SupportImpl",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.amazonaws.services.lambda.runtime.LambdaRuntime",
"fields":[{"name":"logger"}]
},
{
"name":"java.lang.Void",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"java.util.Collections$UnmodifiableMap",
"fields":[{"name":"m"}]
},
{
"name":"jdk.internal.module.IllegalAccessLogger",
"fields":[{"name":"logger"}]
},
{
"name":"sun.misc.Unsafe",
"fields":[{"name":"theUnsafe"}]
},
{
"name":"com.amazonaws.services.lambda.runtime.api.client.runtimeapi.InvocationRequest",
"fields":[{"name":"id"}, {"name":"invokedFunctionArn"}, {"name":"deadlineTimeInMs"}, {"name":"xrayTraceId"}, {"name":"clientContext"}, {"name":"cognitoIdentity"}, {"name":"content"}],
"allPublicMethods":true
},
{
"name":"software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor",
"fields":[{"name":"IS_COLD_START"},{"name":"SERVICE_NAME"}]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"resources": {
"includes": [
{
"pattern": "\\Qaarch64/aws-lambda-runtime-interface-client.glibc.so\\E"
},
{
"pattern": "\\Qaarch64/aws-lambda-runtime-interface-client.musl.so\\E"
},
{
"pattern": "\\Qx86_64/aws-lambda-runtime-interface-client.glibc.so\\E"
},
{
"pattern": "\\Qx86_64/aws-lambda-runtime-interface-client.musl.so\\E"
}
]
},
"bundles": []
}
Loading