Skip to content

Commit 00198dd

Browse files
jreijnphipag
andauthored
feat(v2): GraalVM support for parameters module (#1824)
* Initial commit of all adding graalvm metadata to all parameter modules * Introduce a working example with sam and graalvm for parameters module * Add GraalVM to example name. * Add 'mvn clean package -P native-image' to Makefile again. * Remove test dependency GraalVM metadata. * Set minimum version of core utility parameters example to Java 11 to allow GitHub workflows to check minimum compatible Java version. * remove wrongly introduced class * Set AspectJ versioon to 1.9.20.1 for core utility graalvm example. --------- Co-authored-by: Philipp Page <github@philipp.page> Co-authored-by: Philipp Page <pagejep@amazon.com>
1 parent 9d111b3 commit 00198dd

File tree

46 files changed

+3098
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3098
-9
lines changed

examples/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030

3131
<modules>
3232
<module>powertools-examples-core-utilities/sam</module>
33+
<module>powertools-examples-core-utilities/sam-graalvm</module>
3334
<module>powertools-examples-core-utilities/cdk/app</module>
3435
<module>powertools-examples-core-utilities/cdk/infra</module>
3536
<module>powertools-examples-core-utilities/serverless</module>
3637
<module>powertools-examples-core-utilities/terraform</module>
3738
<module>powertools-examples-idempotency</module>
38-
<module>powertools-examples-parameters</module>
39+
<module>powertools-examples-parameters/sam</module>
40+
<module>powertools-examples-parameters/sam-graalvm</module>
3941
<module>powertools-examples-serialization</module>
4042
<module>powertools-examples-batch</module>
4143
<module>powertools-examples-validation</module>

examples/powertools-examples-core-utilities/sam-graalvm/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

5-
<name>Powertools for AWS Lambda (Java) - Examples - Core Utilities (logging, tracing, metrics) with SAM</name>
5+
<name>Powertools for AWS Lambda (Java) - Examples - Core Utilities (logging, tracing, metrics) with SAM GraalVM</name>
66
<groupId>software.amazon.lambda.examples</groupId>
77
<version>2.0.0-SNAPSHOT</version>
8-
<artifactId>powertools-examples-core-utilitiessam-graalvm</artifactId>
8+
<artifactId>powertools-examples-core-utilities-sam-graalvm</artifactId>
99
<packaging>jar</packaging>
1010

1111
<properties>
1212
<log4j.version>2.24.0</log4j.version>
13-
<maven.compiler.source>21</maven.compiler.source>
14-
<maven.compiler.target>21</maven.compiler.target>
15-
<aspectj.version>1.9.22.1</aspectj.version>
13+
<maven.compiler.source>11</maven.compiler.source>
14+
<maven.compiler.target>11</maven.compiler.target>
15+
<aspectj.version>1.9.20.1</aspectj.version>
1616
</properties>
1717

1818
<dependencies>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Use the official AWS SAM base image for Java 21
2+
FROM public.ecr.aws/sam/build-java21:latest
3+
4+
#Install GraalVM dependencies
5+
RUN curl -4 -L curl https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_linux-x64_bin.tar.gz | tar -xvz
6+
RUN mv graalvm-jdk-21.* /usr/lib/graalvm
7+
8+
#Make native image and mvn available on CLI
9+
RUN ln -s /usr/lib/graalvm/bin/native-image /usr/bin/native-image
10+
RUN ln -s /usr/lib/maven/bin/mvn /usr/bin/mvn
11+
12+
#Set GraalVM as default
13+
ENV JAVA_HOME=/usr/lib/graalvm
14+
ENV PATH=/usr/lib/graalvm/bin:$PATH
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build-ParametersFunction:
2+
mvn clean package -P native-image
3+
chmod +x target/hello-world
4+
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)
5+
chmod +x src/main/config/bootstrap
6+
cp src/main/config/bootstrap $(ARTIFACTS_DIR)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Powertools for AWS Lambda (Java) - Parameters Example with SAM on GraalVM
2+
3+
This project contains an example of Lambda function using the parameters 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/parameters/).
4+
5+
The example uses the [SSM Parameter Store](https://docs.powertools.aws.dev/lambda/java/utilities/parameters/#ssm-parameter-store)
6+
and the [Secrets Manager](https://docs.powertools.aws.dev/lambda/java/utilities/parameters/#secrets-manager) to inject
7+
runtime parameters into the application.
8+
Have a look at [ParametersFunction.java](src/main/java/org/demo/parameters/ParametersFunction.java) for the full details.
9+
10+
## Configuration
11+
12+
- SAM uses [template.yaml](template.yaml) to define the application's AWS resources.
13+
This file defines the Lambda function to be deployed as well as API Gateway for it.
14+
15+
- Set the environment to use GraalVM
16+
17+
```shell
18+
export JAVA_HOME=<path to GraalVM>
19+
```
20+
21+
## Build the sample application
22+
23+
- Build the Docker image that will be used as the environment for SAM build:
24+
25+
```shell
26+
docker build --platform linux/amd64 . -t powertools-examples-parameters-sam-graalvm
27+
```
28+
29+
- Build the SAM project using the docker image
30+
31+
```shell
32+
sam build --use-container --build-image powertools-examples-parameters-sam-graalvm
33+
```
34+
35+
#### [Optional] Building with -SNAPSHOT versions of PowerTools
36+
37+
- 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.
38+
To get around this, follow these steps:
39+
- Create the native image using the `docker` command below on your development machine. The native image is created in the `target` directory.
40+
- `` docker run --platform linux/amd64 -it -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 powertools-examples-parameters-sam-graalvm mvn clean -Pnative-image package -DskipTests ``
41+
- Edit the [`Makefile`](Makefile) remove this line
42+
- `mvn clean package -P native-image`
43+
- Build the SAM project using the docker image
44+
- `sam build --use-container --build-image powertools-examples-parameters-sam-graalvm`
45+
46+
## Deploy the sample application
47+
48+
- SAM deploy
49+
50+
```shell
51+
sam deploy
52+
```
53+
54+
This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting
55+
started with SAM in [the examples directory](../../README.md)
56+
57+
## Test the application
58+
59+
First, hit the URL of the application. You can do this with curl or your browser:
60+
61+
```bash
62+
curl https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/params/
63+
```
64+
You will get your IP address back. The contents of the logs will be more interesting, and show you the values
65+
of the parameters injected into the handler:
66+
67+
```bash
68+
sam logs --stack-name $MY_STACK_NAME --tail
69+
```
70+
71+
```json
72+
{
73+
...
74+
"thread": "main",
75+
"level": "INFO",
76+
"loggerName": "org.demo.parameters.ParametersFunction",
77+
"message": "secretjsonobj=MyObject{id=23443, code='hk38543oj24kn796kp67bkb234gkj679l68'}\n",
78+
...
79+
}
80+
```
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>software.amazon.lambda.examples</groupId>
5+
<version>2.0.0-SNAPSHOT</version>
6+
<artifactId>powertools-examples-parameters-sam-graalvm</artifactId>
7+
<packaging>jar</packaging>
8+
<name>Powertools for AWS Lambda (Java) - Examples - Parameters GraalVM</name>
9+
10+
<properties>
11+
<log4j.version>2.24.0</log4j.version>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<aspectj.version>1.9.20.1</aspectj.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>software.amazon.lambda</groupId>
20+
<artifactId>powertools-logging-log4j</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>software.amazon.lambda</groupId>
25+
<artifactId>powertools-parameters-ssm</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>software.amazon.lambda</groupId>
30+
<artifactId>powertools-parameters-secrets</artifactId>
31+
<version>${project.version}</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>com.amazonaws</groupId>
36+
<artifactId>aws-lambda-java-core</artifactId>
37+
<version>1.2.3</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.amazonaws</groupId>
41+
<artifactId>aws-lambda-java-events</artifactId>
42+
<version>3.15.0</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.aspectj</groupId>
46+
<artifactId>aspectjrt</artifactId>
47+
<version>${aspectj.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.amazonaws</groupId>
51+
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
52+
<version>2.1.1</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.apache.logging.log4j</groupId>
56+
<artifactId>log4j-api</artifactId>
57+
<version>${log4j.version}</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.apache.logging.log4j</groupId>
61+
<artifactId>log4j-core</artifactId>
62+
<version>${log4j.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.apache.logging.log4j</groupId>
66+
<artifactId>log4j-slf4j2-impl</artifactId>
67+
<version>${log4j.version}</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.apache.logging.log4j</groupId>
71+
<artifactId>log4j-layout-template-json</artifactId>
72+
<version>${log4j.version}</version>
73+
</dependency>
74+
75+
<!-- Test dependencies -->
76+
<dependency>
77+
<groupId>org.mockito</groupId>
78+
<artifactId>mockito-core</artifactId>
79+
<version>5.1.1</version>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.junit.jupiter</groupId>
84+
<artifactId>junit-jupiter-api</artifactId>
85+
<version>5.11.1</version>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.junit.jupiter</groupId>
90+
<artifactId>junit-jupiter-engine</artifactId>
91+
<version>5.9.3</version>
92+
<scope>test</scope>
93+
</dependency>
94+
</dependencies>
95+
96+
<build>
97+
<plugins>
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-surefire-plugin</artifactId>
101+
<!-- JUnit 5 requires Surefire version 3.1.0 or higher -->
102+
<version>3.2.5</version>
103+
</plugin>
104+
<plugin>
105+
<groupId>dev.aspectj</groupId>
106+
<artifactId>aspectj-maven-plugin</artifactId>
107+
<version>1.14</version>
108+
<configuration>
109+
<source>${maven.compiler.source}</source>
110+
<target>${maven.compiler.target}</target>
111+
<complianceLevel>${maven.compiler.target}</complianceLevel>
112+
<aspectLibraries>
113+
<aspectLibrary>
114+
<groupId>software.amazon.lambda</groupId>
115+
<artifactId>powertools-parameters</artifactId>
116+
</aspectLibrary>
117+
</aspectLibraries>
118+
</configuration>
119+
<executions>
120+
<execution>
121+
<goals>
122+
<goal>compile</goal>
123+
</goals>
124+
</execution>
125+
</executions>
126+
<dependencies>
127+
<dependency>
128+
<groupId>org.aspectj</groupId>
129+
<artifactId>aspectjtools</artifactId>
130+
<version>${aspectj.version}</version>
131+
</dependency>
132+
</dependencies>
133+
</plugin>
134+
<plugin>
135+
<groupId>org.apache.maven.plugins</groupId>
136+
<artifactId>maven-shade-plugin</artifactId>
137+
<version>3.6.0</version>
138+
<executions>
139+
<execution>
140+
<phase>package</phase>
141+
<goals>
142+
<goal>shade</goal>
143+
</goals>
144+
<configuration>
145+
<createDependencyReducedPom>false</createDependencyReducedPom>
146+
<transformers>
147+
<transformer implementation="org.apache.logging.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer"/>
148+
</transformers>
149+
</configuration>
150+
</execution>
151+
</executions>
152+
<dependencies>
153+
<dependency>
154+
<groupId>org.apache.logging.log4j</groupId>
155+
<artifactId>log4j-transform-maven-shade-plugin-extensions</artifactId>
156+
<version>0.1.0</version>
157+
</dependency>
158+
</dependencies>
159+
</plugin>
160+
<!-- Don't deploy the example -->
161+
<plugin>
162+
<groupId>org.apache.maven.plugins</groupId>
163+
<artifactId>maven-deploy-plugin</artifactId>
164+
<version>3.1.2</version>
165+
<configuration>
166+
<skip>true</skip>
167+
</configuration>
168+
</plugin>
169+
</plugins>
170+
</build>
171+
<profiles>
172+
<profile>
173+
<id>native-image</id>
174+
<build>
175+
<plugins>
176+
<plugin>
177+
<groupId>org.graalvm.buildtools</groupId>
178+
<artifactId>native-maven-plugin</artifactId>
179+
<version>0.10.1</version>
180+
<extensions>true</extensions>
181+
<executions>
182+
<execution>
183+
<id>build-native</id>
184+
<goals>
185+
<goal>build</goal>
186+
</goals>
187+
<phase>package</phase>
188+
</execution>
189+
</executions>
190+
<configuration>
191+
<imageName>hello-world</imageName>
192+
<mainClass>com.amazonaws.services.lambda.runtime.api.client.AWSLambda</mainClass>
193+
<buildArgs>
194+
<!-- required for AWS Lambda Runtime Interface Client -->
195+
<arg>--enable-url-protocols=http</arg>
196+
<arg>--add-opens java.base/java.util=ALL-UNNAMED</arg>
197+
</buildArgs>
198+
</configuration>
199+
</plugin>
200+
</plugins>
201+
</build>
202+
</profile>
203+
</profiles>
204+
</project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
4+
./hello-world $_HANDLER
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[
2+
{
3+
"name":"com.amazonaws.services.lambda.runtime.LambdaRuntime",
4+
"methods":[{"name":"<init>","parameterTypes":[] }],
5+
"fields":[{"name":"logger"}],
6+
"allPublicMethods":true
7+
},
8+
{
9+
"name":"com.amazonaws.services.lambda.runtime.LambdaRuntimeInternal",
10+
"methods":[{"name":"<init>","parameterTypes":[] }],
11+
"allPublicMethods":true
12+
}
13+
]

0 commit comments

Comments
 (0)