Skip to content

Commit 7bfedb3

Browse files
committed
Added documentation
1 parent a0791a4 commit 7bfedb3

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

docs/FAQs.md

+110-1
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,113 @@ The following example shows how to use the Lambda Powertools Parameters module w
142142
}
143143
```
144144
The `aws-crt-client` was considered for adoption as the default HTTP client in Lambda Powertools for Java as mentioned in [Move SDK http client to CRT](https://github.com/aws-powertools/powertools-lambda-java/issues/1092),
145-
but due to the impact on the developer experience it was decided to stick with the `url-connection-client`.
145+
but due to the impact on the developer experience it was decided to stick with the `url-connection-client`.
146+
147+
## How can I use Powertools for AWS Lambda (Java) with GraalVM?
148+
149+
Powertools core utilities, i.e. [logging](./core/logging.md), [metrics](./core/metrics.md) and [tracing](./core/tracing.md), include the [GraalVM Reachability Metadata (GRM)](https://www.graalvm.org/latest/reference-manual/native-image/metadata/) in the `META-INF` directories of the respective JARs. You can find a working example of Serverless Application Model (SAM) based application in the [examples](../examples/powertools-examples-core-utilities/sam-graalvm/README.md) directory.
150+
151+
These are typical steps you need to follow in a maven based Java project
152+
153+
### Set the environment to use GraalVM
154+
155+
```shell
156+
export JAVA_HOME=<path to GraalVM>
157+
```
158+
159+
### Use log4j > 2.24.0
160+
Log4j version `2.24.0` adds [support for GraalVM](https://github.com/apache/logging-log4j2/issues/1539#issuecomment-2106766878). Depending on your project's dependency hierarchy, older version of log4j might be included in the final dependency graph. Make sure version `>2.24.0` of these dependencies are used by your maven project:
161+
162+
```xml
163+
<dependencies>
164+
<dependency>
165+
<groupId>org.apache.logging.log4j</groupId>
166+
<artifactId>log4j-api</artifactId>
167+
<version>${log4j.version}</version>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.apache.logging.log4j</groupId>
171+
<artifactId>log4j-core</artifactId>
172+
<version>${log4j.version}</version>
173+
</dependency>
174+
<dependency>
175+
<groupId>org.apache.logging.log4j</groupId>
176+
<artifactId>log4j-slf4j2-impl</artifactId>
177+
<version>${log4j.version}</version>
178+
</dependency>
179+
<dependency>
180+
<groupId>org.apache.logging.log4j</groupId>
181+
<artifactId>log4j-layout-template-json</artifactId>
182+
<version>${log4j.version}</version>
183+
</dependency>
184+
</dependencies>
185+
186+
```
187+
188+
### Add the AWS Lambda Java Runtime Interface Client dependency
189+
190+
Runtime Interface Client allow your function to receive invocation events from Lambda, send the response back to Lambda, and report errors to the Lambda service. Add the below dependency to your maven project:
191+
192+
```xml
193+
<dependency>
194+
<groupId>com.amazonaws</groupId>
195+
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
196+
<version>2.1.1</version>
197+
</dependency>
198+
```
199+
200+
Also include the AWS Lambda GRM files by copying the `com.amazonaws`[directory](../examples/powertools-examples-core-utilities/sam-graalvm/src/main/resources/META-INF/native-image/) in your project's `META-INF/native-image` directory
201+
202+
### Build the native image
203+
204+
1. Use the `native-maven-plugin` to build the native image. You can do this by adding the plugin to your `pom.xml` and creating a build profile called `native-image` that can build the native image of your Lambda function:
205+
206+
```xml
207+
<profiles>
208+
<profile>
209+
<id>native-image</id>
210+
<build>
211+
<plugins>
212+
<plugin>
213+
<groupId>org.graalvm.buildtools</groupId>
214+
<artifactId>native-maven-plugin</artifactId>
215+
<version>0.10.1</version>
216+
<extensions>true</extensions>
217+
<executions>
218+
<execution>
219+
<id>build-native</id>
220+
<goals>
221+
<goal>build</goal>
222+
</goals>
223+
<phase>package</phase>
224+
</execution>
225+
</executions>
226+
<configuration>
227+
<imageName>your-project-name</imageName>
228+
<mainClass>com.amazonaws.services.lambda.runtime.api.client.AWSLambda</mainClass>
229+
<buildArgs>
230+
<!-- required for AWS Lambda Runtime Interface Client -->
231+
<arg>--enable-url-protocols=http</arg>
232+
<arg>--add-opens java.base/java.util=ALL-UNNAMED</arg>
233+
</buildArgs>
234+
</configuration>
235+
</plugin>
236+
</plugins>
237+
</build>
238+
</profile>
239+
</profiles>
240+
```
241+
242+
2. Create a docker image using a Dockerfile like [this](../examples/powertools-examples-core-utilities/sam-graalvm/Dockerfile) to create an x86 based build image.
243+
244+
```shell
245+
docker build --platform linux/amd64 . -t your-org/your-app-graalvm-builder
246+
```
247+
248+
3. Create the native image of you Lambda function using the docker command below.
249+
250+
```shell
251+
docker run --platform linux/amd64 -it -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 your-org/your-app-graalvm-builder mvn clean -Pnative-image package
252+
253+
```
254+
The native image is created in the target directory.

examples/powertools-examples-core-utilities/sam-graalvm/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ This project demonstrates the Lambda for Powertools Java module deployed using [
55
For general information on the deployed example itself, you can refer to the parent [README](../README.md)
66

77
## Configuration
8-
SAM uses [template.yaml](template.yaml) to define the application's AWS resources.
8+
- SAM uses [template.yaml](template.yaml) to define the application's AWS resources.
99
This file defines the Lambda function to be deployed as well as API Gateway for it.
1010

11+
- Set the environment to use GraalVM
12+
13+
```shell
14+
export JAVA_HOME=<path to GraalVM>
15+
````
16+
1117
## Build the sample application
1218

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

0 commit comments

Comments
 (0)