Skip to content

Commit 2126bd9

Browse files
authored
Merge branch 'main' into dependabot/maven/aws.sdk.version-2.32.31
2 parents 84dcc69 + a7785b3 commit 2126bd9

File tree

59 files changed

+9132
-387
lines changed

Some content is hidden

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

59 files changed

+9132
-387
lines changed

.github/workflows/check-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
mvn -B -q install --file pom.xml
8888
8989
graalvm-build:
90-
runs-on: ubuntu-latest
90+
runs-on: aws-powertools_ubuntu-latest_8-core
9191
steps:
9292
- id: checkout
9393
name: Checkout repository

.github/workflows/security-dependencies-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ jobs:
2626
- name: Checkout Repository
2727
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
2828
- name: Verify Contents
29-
uses: actions/dependency-review-action@bc41886e18ea39df68b1b1245f4184881938e050 # v4.7.2
29+
uses: actions/dependency-review-action@595b5aeba73380359d98a5e087f648dbb0edce1b # v4.7.3
3030
with:
3131
config-file: './.github/dependency-review-config.yml'

docs/utilities/validation.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,53 @@ If you need to configure the Jackson ObjectMapper, you can use the `ValidationCo
292292
}
293293
}
294294
```
295+
296+
## Advanced
297+
298+
### Lambda SnapStart priming
299+
300+
The Validation utility integrates with AWS Lambda SnapStart to improve restore durations. To make sure the SnapStart priming logic of this utility runs correctly, you need an explicit reference to `ValidationConfig` in your code to allow the library to register before SnapStart takes a memory snapshot. Learn more about what priming is in this [blog post](https://aws.amazon.com/blogs/compute/optimizing-cold-start-performance-of-aws-lambda-using-advanced-priming-strategies-with-snapstart/){target="_blank"}.
301+
302+
If you don't set a custom `ValidationConfig` in your code yet, make sure to reference `ValidationConfig` in your Lambda handler initialization code. This can be done by adding one of the following lines to your handler class:
303+
304+
=== "Constructor"
305+
306+
```java hl_lines="7"
307+
import software.amazon.lambda.powertools.validation.Validation;
308+
import software.amazon.lambda.powertools.validation.ValidationConfig;
309+
310+
public class MyFunctionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
311+
312+
public MyFunctionHandler() {
313+
ValidationConfig.get(); // Ensure ValidationConfig is loaded for SnapStart
314+
}
315+
316+
@Override
317+
@Validation(inboundSchema = "classpath:/schema_in.json", outboundSchema = "classpath:/schema_out.json")
318+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
319+
// ...
320+
return something;
321+
}
322+
}
323+
```
324+
325+
=== "Static Initializer"
326+
327+
```java hl_lines="7"
328+
import software.amazon.lambda.powertools.validation.Validation;
329+
import software.amazon.lambda.powertools.validation.ValidationConfig;
330+
331+
public class MyFunctionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
332+
333+
static {
334+
ValidationConfig.get(); // Ensure ValidationConfig is loaded for SnapStart
335+
}
336+
337+
@Override
338+
@Validation(inboundSchema = "classpath:/schema_in.json", outboundSchema = "classpath:/schema_out.json")
339+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
340+
// ...
341+
return something;
342+
}
343+
}
344+
```

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!

0 commit comments

Comments
 (0)