Homepage
Powertools for AWS Lambda (Java) is a suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier.
Tip
Powertools for AWS Lambda is also available for Python, TypeScript, and .NET
Looking for a quick run through of the core utilities?
Check out this detailed blog post with a practical example. To dive deeper, the Powertools for AWS Lambda (Java) workshop is a great next step.
Tenets¶
This project separates core utilities that will be available in other runtimes vs general utilities that might not be available across all runtimes.
- AWS Lambda only – We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
- Eases the adoption of best practices – The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
- Keep it lean – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
- We strive for backwards compatibility – New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
- We work backwards from the community – We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
- Progressive - Utilities are designed to be incrementally adoptable for customers at any stage of their Serverless journey. They follow language idioms and their community’s common practices.
Install¶
Quick hello world example using SAM CLI
You can use SAM to quickly setup a serverless project including Powertools for AWS Lambda (Java).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Manual installation Powertools for AWS Lambda (Java) dependencies are available in Maven Central. You can use your favourite dependency management tool to install it
Depending on your version of Java (either Java 1.8 or 11+), the configuration slightly changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Why a different configuration?
Powertools for AWS Lambda (Java) is using AspectJ internally
to handle annotations. Recently, in order to support Java 17 we had to move to dev.aspectj:aspectj-maven-plugin
because
org.codehaus.mojo:aspectj-maven-plugin
does not support Java 17.
Under the hood, org.codehaus.mojo:aspectj-maven-plugin
is based on AspectJ 1.9.7,
while dev.aspectj:aspectj-maven-plugin
is based on AspectJ 1.9.8, compiled for Java 11+.
Java Compatibility¶
Powertools for AWS Lambda (Java) supports all Java version from 8 up to 21 as well as the corresponding Lambda runtimes.
For the following modules, Powertools for AWS Lambda (Java) leverages the aspectj library to provide annotations: - Logging - Metrics - Tracing - Parameters - Idempotency - Validation - Large messages
You may need to add the good version of aspectjrt
to your dependencies based on the jdk used for building your function:
1 2 3 4 5 |
|
Use the following dependency matrix between this library and the JDK:
JDK version | aspectj version |
---|---|
1.8 |
1.9.7 |
11-17 |
1.9.20.1 |
21 |
1.9.21 |
Environment variables¶
Info
Explicit parameters take precedence over environment variables.
Environment variable | Description | Utility |
---|---|---|
POWERTOOLS_SERVICE_NAME | Sets service name used for tracing namespace, metrics dimension and structured logging | All |
POWERTOOLS_METRICS_NAMESPACE | Sets namespace used for metrics | Metrics |
POWERTOOLS_LOGGER_SAMPLE_RATE | Debug log sampling | Logging |
POWERTOOLS_LOG_LEVEL | Sets logging level | Logging |
POWERTOOLS_LOGGER_LOG_EVENT | Enables/Disables whether to log the incoming event when using the aspect | Logging |
POWERTOOLS_TRACER_CAPTURE_RESPONSE | Enables/Disables tracing mode to capture method response | Tracing |
POWERTOOLS_TRACER_CAPTURE_ERROR | Enables/Disables tracing mode to capture method error | Tracing |