You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/README.md
+17-13Lines changed: 17 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,35 @@
1
1
# Powertools for AWS Lambda (Java) Examples
2
2
3
3
This directory holds example projects demoing different components of the Powertools for AWS Lambda (Java).
4
+
4
5
Each example can be copied from its subdirectory and used independently of the rest of this repository.
5
6
6
7
## Examples
7
8
8
9
*[powertools-examples-core-utilities](powertools-examples-core-utilities) - Demonstrates the core logging, tracing, and metrics modules with different build tools and languages
*[powertools-examples-serialization](powertools-examples-serialization) - Uses the serialization module to serialize and deserialize API Gateway & SQS payloads
*[powertools-examples-validation](powertools-examples-validation) - Uses the validation module to validate user requests received via API Gateway
18
27
*[powertools-examples-cloudformation](powertools-examples-cloudformation) - Deploys a Cloudformation custom resource
19
28
*[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
20
30
21
31
## 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.
24
33
25
34
To use the SAM CLI, you need the following tools.
26
35
@@ -29,17 +38,13 @@ To use the SAM CLI, you need the following tools.
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
31
40
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.
37
42
38
43
To build and deploy an example application for the first time, run the following in your shell:
39
44
40
45
```bash
41
46
# Switch to the directory containing an example for the powertools-idempotency module
42
-
$ cd powertools-examples-idempotency
47
+
$ cd powertools-examples-idempotency/sam
43
48
44
49
# Build and deploy the example
45
50
$ sam build
@@ -56,7 +61,7 @@ The first command will build the source of your application. The second command
56
61
57
62
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
58
63
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)
60
65
61
66
### External examples
62
67
@@ -69,10 +74,9 @@ You can find more examples in the https://github.com/aws/aws-sam-cli-app-templat
69
74
70
75
### SAM - Other Tools
71
76
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.
# 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:
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:
- 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