FAQs
How can I use Powertools for AWS Lambda (Java) with Lombok?¶
Powertools uses aspectj-maven-plugin
to compile-time weave (CTW) aspects into the project. In case you want to use Lombok
or other compile-time preprocessor for your project, it is required to change aspectj-maven-plugin
configuration to enable in-place weaving feature. Otherwise the plugin will ignore changes introduced by Lombok
and will use .java
files as a source.
To enable in-place weaving feature you need to use following aspectj-maven-plugin
configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
How can I use Powertools for AWS Lambda (Java) with Kotlin projects?¶
Powertools uses aspectj-maven-plugin
to compile-time weave (CTW) aspects into the project. When using it with Kotlin projects, it is required to forceAjcCompile
.
No explicit configuration should be required for gradle projects.
To enable forceAjcCompile
you need to use following aspectj-maven-plugin
configuration:
1 2 3 4 5 6 7 8 9 10 |
|
How can I use Powertools for AWS Lambda (Java) with the AWS CRT HTTP Client?¶
Powertools uses the url-connection-client
as the default HTTP client. The url-connection-client
is a lightweight HTTP client, which keeps the impact on Lambda cold starts to a minimum.
With the announcement of the aws-crt-client
a new HTTP client has been released, which offers faster SDK startup time and smaller memory footprint.
Unfortunately, replacing the url-connection-client
dependency with the aws-crt-client
will not immediately improve the lambda cold start performance and memory footprint,
as the default version of the dependency contains native system libraries for all supported runtimes and architectures (Linux, MacOS, Windows, AMD64, ARM64, etc). This makes the CRT client portable, without the user having to consider where their code will run, but comes at the cost of JAR size.
Configuring dependencies¶
Using the aws-crt-client
in your project requires the exclusion of the url-connection-client
transitive dependency from the powertools dependency.
1 2 3 4 5 6 7 8 9 10 11 |
|
aws-crt-client
and exclude the "generic" aws-crt
dependency (contains all runtime libraries).
Instead, set a specific classifier of the aws-crt
to use the one for your target runtime: either linux-x86_64
for a Lambda configured for x86 or linux-aarch_64
for Lambda using arm64.
You will need to add a separate maven profile to build and debug locally when your development environment does not share the target architecture you are using in Lambda.
By specifying the specific target runtime, we prevent other target runtimes from being included in the jar file, resulting in a smaller Lambda package and improved cold start times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Explicitly set the AWS CRT HTTP Client¶
After configuring the dependencies, it's required to explicitly specify the AWS SDK HTTP client. Depending on the Powertools module, there is a different way to configure the SDK client.
The following example shows how to use the Lambda Powertools Parameters module while leveraging the AWS CRT Client.
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 |
|
The aws-crt-client
was considered for adoption as the default HTTP client in Lambda Powertools for Java as mentioned in Move SDK http client to CRT,
but due to the impact on the developer experience it was decided to stick with the url-connection-client
.
How can I use Powertools for AWS Lambda (Java) with GraalVM?¶
Powertools core utilities, i.e. logging, metrics and tracing, include the GraalVM Reachability Metadata (GRM) in the META-INF
directories of the respective JARs. You can find a working example of Serverless Application Model (SAM) based application in the examples directory.
Below, you find typical steps you need to follow in a Maven based Java project:
Set the environment to use GraalVM¶
1 |
|
Use log4j >2.24.0
¶
Log4j version 2.24.0
adds support for GraalVM. Depending on your project's dependency hierarchy, older version of log4j might be included in the final dependency graph. Make sure version >2.24.0
of these dependencies are used by your Maven project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Add the AWS Lambda Java Runtime Interface Client dependency¶
The Runtime Interface Client allows your function to receive invocation events from Lambda, send the response back to Lambda, and report errors to the Lambda service. Add the below dependency to your Maven project:
1 2 3 4 5 |
|
Also include the AWS Lambda GRM files by copying the com.amazonaws
directory in your project's META-INF/native-image
directory
Build the native image¶
Use the native-maven-plugin
to build the native image. You can do this by adding the plugin to your pom.xml
and creating a build profile called native-image
that can build the native image of your Lambda function:
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 |
|
Create a Docker image using a Dockerfile
like this to create an x86 based build image.
1 |
|
Create the native image of you Lambda function using the Docker command below.
1 |
|
target/
directory.