Skip to content

Commit 046ac85

Browse files
committed
Add Idempotency GraalVM example and remove unncessary dependencies from pom.xml
1 parent 1681e19 commit 046ac85

File tree

27 files changed

+655
-78
lines changed

27 files changed

+655
-78
lines changed

examples/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
# Powertools for AWS Lambda (Java) Examples
22

33
This directory holds example projects demoing different components of the Powertools for AWS Lambda (Java).
4+
45
Each example can be copied from its subdirectory and used independently of the rest of this repository.
56

67
## Examples
78

89
* [powertools-examples-core-utilities](powertools-examples-core-utilities) - Demonstrates the core logging, tracing, and metrics modules with different build tools and languages
910
* [CDK](./powertools-examples-core-utilities/cdk)
10-
* [Gradle](./powertools-examples-core-utilities/gradle)
1111
* [SAM](./powertools-examples-core-utilities/sam)
12+
* [SAM GraalVM](./powertools-examples-core-utilities/sam-graalvm)
1213
* [Serverless](./powertools-examples-core-utilities/serverless)
14+
* [Terraform](./powertools-examples-core-utilities/terraform)
15+
* [Gradle](./powertools-examples-core-utilities/gradle)
1316
* [Kotlin](./powertools-examples-core-utilities/kotlin)
1417
* [powertools-examples-idempotency](powertools-examples-idempotency) - An idempotent HTTP API
18+
* [SAM](./powertools-examples-idempotency/sam)
19+
* [SAM GraalVM](./powertools-examples-idempotency/sam-graalvm)
1520
* [powertools-examples-parameters](powertools-examples-parameters) - Uses the parameters module to provide runtime parameters to a function
21+
* [SAM](./powertools-examples-parameters/sam)
22+
* [SAM GraalVM](./powertools-examples-parameters/sam-graalvm)
1623
* [powertools-examples-serialization](powertools-examples-serialization) - Uses the serialization module to serialize and deserialize API Gateway & SQS payloads
24+
* [SAM](./powertools-examples-serialization/sam)
25+
* [SAM GraalVM](./powertools-examples-serialization/sam-graalvm)
1726
* [powertools-examples-validation](powertools-examples-validation) - Uses the validation module to validate user requests received via API Gateway
1827
* [powertools-examples-cloudformation](powertools-examples-cloudformation) - Deploys a Cloudformation custom resource
1928
* [powertools-examples-batch](powertools-examples-batch) - Examples for each of the different batch processing deployments
29+
* [powertools-examples-kafka](powertools-examples-kafka) - Examples for Kafka event processing
2030

2131
## Working with AWS Serverless Application Model (SAM) Examples
22-
Many of the examples use [AWS Serverless Application Model](https://aws.amazon.com/serverless/sam/) (SAM). To get started
23-
with them, you can use the SAM Command Line Interface (SAM CLI) to build it and deploy an example to AWS.
32+
Many of the examples use [AWS Serverless Application Model](https://aws.amazon.com/serverless/sam/) (SAM). To get started with them, you can use the SAM Command Line Interface (SAM CLI) to build it and deploy an example to AWS.
2433

2534
To use the SAM CLI, you need the following tools.
2635

@@ -29,17 +38,13 @@ To use the SAM CLI, you need the following tools.
2938
* Maven - [Install Maven](https://maven.apache.org/install.html)
3039
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
3140

32-
To learn more about SAM,
33-
[check out the developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli.html).
34-
You can use the CLI to [test events locally](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli-local-invoke.html),
35-
and [run the application locally](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli-local-start-api.html),
36-
amongst other things.
41+
To learn more about SAM, [check out the developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli.html). You can use the CLI to [test events locally](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli-local-invoke.html), and [run the application locally](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli-local-start-api.html), amongst other things.
3742

3843
To build and deploy an example application for the first time, run the following in your shell:
3944

4045
```bash
4146
# Switch to the directory containing an example for the powertools-idempotency module
42-
$ cd powertools-examples-idempotency
47+
$ cd powertools-examples-idempotency/sam
4348

4449
# Build and deploy the example
4550
$ sam build
@@ -56,7 +61,7 @@ The first command will build the source of your application. The second command
5661

5762
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
5863

59-
If you're not using SAM, you can look for examples for other tools under [powertools-examples-core](./powertools-examples-core)
64+
If you're not using SAM, you can look for examples for other tools under [powertools-examples-core-utilities](./powertools-examples-core-utilities)
6065

6166
### External examples
6267

@@ -69,10 +74,9 @@ You can find more examples in the https://github.com/aws/aws-sam-cli-app-templat
6974

7075
### SAM - Other Tools
7176

72-
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
73-
The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.
77+
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.
7478

7579
* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
7680
* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
7781
* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html)
78-
* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html)
82+
* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html)

examples/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
<module>powertools-examples-core-utilities/cdk/infra</module>
3636
<module>powertools-examples-core-utilities/serverless</module>
3737
<module>powertools-examples-core-utilities/terraform</module>
38-
<module>powertools-examples-idempotency</module>
38+
<module>powertools-examples-idempotency/sam</module>
39+
<module>powertools-examples-idempotency/sam-graalvm</module>
3940
<module>powertools-examples-parameters/sam</module>
4041
<module>powertools-examples-parameters/sam-graalvm</module>
4142
<module>powertools-examples-serialization/sam</module>
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@sha256:a5554d68374e19450c6c88448516ac95a9acedc779f318040f5c230134b4e461
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build-IdempotencyFunction:
2+
chmod +x target/hello-world
3+
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)
4+
chmod +x src/main/config/bootstrap
5+
cp src/main/config/bootstrap $(ARTIFACTS_DIR)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Powertools for AWS Lambda (Java) - Idempotency Example with SAM on GraalVM
2+
3+
This project contains an example of a Lambda function using the idempotency 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/idempotency/).
4+
5+
The example exposes a HTTP POST endpoint. When the user sends the address of a webpage to it, the endpoint fetches the contents of the URL and returns them to the user.
6+
7+
Have a look at [App.java](src/main/java/helloworld/App.java) for the full details.
8+
9+
## Build the sample application
10+
11+
> [!NOTE]
12+
> Building AWS Lambda packages on macOS (ARM64/Intel) for deployment on AWS Lambda (Linux x86_64 or ARM64) will result in incompatible binary dependencies that cause import errors at runtime.
13+
14+
Choose the appropriate build method based on your operating system:
15+
16+
### Build locally using Docker
17+
18+
Recommended for macOS and Windows users: Cross-compile using Docker to match target platform of Lambda:
19+
20+
```shell
21+
docker build --platform linux/amd64 . -t powertools-examples-idempotency-sam-graalvm
22+
docker run --platform linux/amd64 -it -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 powertools-examples-idempotency-sam-graalvm mvn clean -Pnative-image package -DskipTests
23+
sam build --use-container --build-image powertools-examples-idempotency-sam-graalvm
24+
```
25+
26+
**Note**: The Docker run command mounts your local Maven cache (`~/.m2`) and builds the native binary with SNAPSHOT support, then SAM packages the pre-built binary.
27+
28+
### Build on native OS
29+
30+
For Linux users with GraalVM installed:
31+
32+
```shell
33+
export JAVA_HOME=<path to GraalVM>
34+
mvn clean -Pnative-image package -DskipTests
35+
sam build
36+
```
37+
38+
## Deploy the sample application
39+
40+
```shell
41+
sam deploy
42+
```
43+
44+
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)
45+
46+
## Test the application
47+
48+
```bash
49+
curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/helloidem/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}'
50+
```
51+
52+
this should return the contents of the webpage, for instance:
53+
54+
```json
55+
{ "message": "hello world", "location": "123.123.123.1" }
56+
```
57+
58+
- First call will execute the handleRequest normally, and store the response in the idempotency table (Look into DynamoDB)
59+
- Second call (and next ones) will retrieve from the cache (if cache is enabled, which is by default) or from the store, the handler won't be called. Until the expiration happens (by default 1 hour).
60+
61+
Check out [App.java](src/main/java/helloworld/App.java) to see how it works!
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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.3.0</version>
6+
<artifactId>powertools-examples-idempotency-sam-graalvm</artifactId>
7+
<packaging>jar</packaging>
8+
<name>Powertools for AWS Lambda (Java) - Examples - Idempotency GraalVM</name>
9+
10+
<properties>
11+
<maven.compiler.source>11</maven.compiler.source>
12+
<maven.compiler.target>11</maven.compiler.target>
13+
<aspectj.version>1.9.20.1</aspectj.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>software.amazon.lambda</groupId>
19+
<artifactId>powertools-tracing</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>software.amazon.lambda</groupId>
24+
<artifactId>powertools-logging-log4j</artifactId>
25+
<version>${project.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>software.amazon.lambda</groupId>
29+
<artifactId>powertools-idempotency-dynamodb</artifactId>
30+
<version>${project.version}</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.amazonaws</groupId>
35+
<artifactId>aws-lambda-java-core</artifactId>
36+
<version>1.3.0</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.amazonaws</groupId>
40+
<artifactId>aws-lambda-java-events</artifactId>
41+
<version>3.16.1</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.amazonaws</groupId>
45+
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
46+
<version>2.8.3</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.aspectj</groupId>
51+
<artifactId>aspectjrt</artifactId>
52+
<version>${aspectj.version}</version>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>dev.aspectj</groupId>
60+
<artifactId>aspectj-maven-plugin</artifactId>
61+
<version>1.14.1</version>
62+
<configuration>
63+
<source>${maven.compiler.source}</source>
64+
<target>${maven.compiler.target}</target>
65+
<complianceLevel>${maven.compiler.target}</complianceLevel>
66+
<aspectLibraries>
67+
<aspectLibrary>
68+
<groupId>software.amazon.lambda</groupId>
69+
<artifactId>powertools-tracing</artifactId>
70+
</aspectLibrary>
71+
<aspectLibrary>
72+
<groupId>software.amazon.lambda</groupId>
73+
<artifactId>powertools-logging</artifactId>
74+
</aspectLibrary>
75+
<aspectLibrary>
76+
<groupId>software.amazon.lambda</groupId>
77+
<artifactId>powertools-idempotency-core</artifactId>
78+
</aspectLibrary>
79+
</aspectLibraries>
80+
</configuration>
81+
<executions>
82+
<execution>
83+
<goals>
84+
<goal>compile</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
<dependencies>
89+
<dependency>
90+
<groupId>org.aspectj</groupId>
91+
<artifactId>aspectjtools</artifactId>
92+
<version>${aspectj.version}</version>
93+
</dependency>
94+
</dependencies>
95+
</plugin>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-shade-plugin</artifactId>
99+
<version>3.6.0</version>
100+
<executions>
101+
<execution>
102+
<phase>package</phase>
103+
<goals>
104+
<goal>shade</goal>
105+
</goals>
106+
<configuration>
107+
<createDependencyReducedPom>false</createDependencyReducedPom>
108+
<transformers>
109+
<transformer
110+
implementation="org.apache.logging.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer" />
111+
</transformers>
112+
</configuration>
113+
</execution>
114+
</executions>
115+
<dependencies>
116+
<dependency>
117+
<groupId>org.apache.logging.log4j</groupId>
118+
<artifactId>log4j-transform-maven-shade-plugin-extensions</artifactId>
119+
<version>0.2.0</version>
120+
</dependency>
121+
</dependencies>
122+
</plugin>
123+
<!-- Don't deploy the example -->
124+
<plugin>
125+
<groupId>org.apache.maven.plugins</groupId>
126+
<artifactId>maven-deploy-plugin</artifactId>
127+
<version>3.1.4</version>
128+
<configuration>
129+
<skip>true</skip>
130+
</configuration>
131+
</plugin>
132+
</plugins>
133+
</build>
134+
<profiles>
135+
<profile>
136+
<id>native-image</id>
137+
<build>
138+
<plugins>
139+
<plugin>
140+
<groupId>org.graalvm.buildtools</groupId>
141+
<artifactId>native-maven-plugin</artifactId>
142+
<version>0.11.0</version>
143+
<extensions>true</extensions>
144+
<executions>
145+
<execution>
146+
<id>build-native</id>
147+
<goals>
148+
<goal>build</goal>
149+
</goals>
150+
<phase>package</phase>
151+
</execution>
152+
</executions>
153+
<configuration>
154+
<imageName>hello-world</imageName>
155+
<mainClass>com.amazonaws.services.lambda.runtime.api.client.AWSLambda</mainClass>
156+
<buildArgs>
157+
<!-- required for AWS Lambda Runtime Interface Client -->
158+
<arg>--enable-url-protocols=http,https</arg>
159+
<arg>--add-opens java.base/java.util=ALL-UNNAMED</arg>
160+
</buildArgs>
161+
</configuration>
162+
</plugin>
163+
</plugins>
164+
</build>
165+
</profile>
166+
</profiles>
167+
</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)