Thanks to visit codestin.com
Credit goes to docs.aws.amazon.com

Skip to content

Homepage

Powertools for AWS Lambda (Java) is a developer toolkit to implement Serverless best practices and increase developer velocity.

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

Powertools for AWS Lambda (Java) dependencies are available in Maven Central. You can use your favourite dependency management tool to install it

 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<dependencies>
    ...
    <dependency>
        <groupId>software.amazon.lambda</groupId>
        <artifactId>powertools-tracing</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>software.amazon.lambda</groupId>
        <artifactId>powertools-logging-log4j</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>software.amazon.lambda</groupId>
        <artifactId>powertools-metrics</artifactId>
        <version>2.7.0</version>
    </dependency>
    <!-- Make sure to include AspectJ runtime compatible with plugin version -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.22</version>
    </dependency>
    ...
</dependencies>
...
<!-- Configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project -->
<!-- Note: This AspectJ configuration is not needed when using the functional approach -->
<build>
    <plugins>
        ...
        <plugin>
             <groupId>dev.aspectj</groupId>
             <artifactId>aspectj-maven-plugin</artifactId>
             <version>1.14</version>
             <configuration>
                 <source>11</source> <!-- or higher -->
                 <target>11</target> <!-- or higher -->
                 <complianceLevel>11</complianceLevel> <!-- or higher -->
                 <aspectLibraries>
                     <aspectLibrary>
                         <groupId>software.amazon.lambda</groupId>
                         <artifactId>powertools-tracing</artifactId>
                     </aspectLibrary>
                     <aspectLibrary>
                         <groupId>software.amazon.lambda</groupId>
                         <artifactId>powertools-logging</artifactId>
                     </aspectLibrary>
                     <aspectLibrary>
                         <groupId>software.amazon.lambda</groupId>
                         <artifactId>powertools-metrics</artifactId>
                     </aspectLibrary>
                 </aspectLibraries>
             </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <!-- AspectJ compiler version, in sync with runtime -->
                    <version>1.9.22</version>
                </dependency>
            </dependencies>
             <executions>
                 <execution>
                     <goals>
                         <goal>compile</goal>
                     </goals>
                 </execution>
             </executions>
        </plugin>
        ...
    </plugins>
</build>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    plugins {
        id 'java'
        id 'io.freefair.aspectj.post-compile-weaving' version '8.2.2'
    }

    // the freefair aspect plugins targets gradle 8.2.1
    // https://docs.freefair.io/gradle-plugins/8.2.2/reference/
    wrapper {
        gradleVersion = "8.2.1"
    }        

    repositories {
        mavenCentral()
    }

    dependencies {
        // Note: This AspectJ configuration is not needed when using the functional approach
        aspect 'software.amazon.lambda:powertools-logging-log4j:2.7.0'
        aspect 'software.amazon.lambda:powertools-tracing:2.7.0'
        aspect 'software.amazon.lambda:powertools-metrics:2.7.0'
    }

    sourceCompatibility = 11
    targetCompatibility = 11
Don't want to use AspectJ?

Powertools for AWS Lambda (Java) now provides a functional API that doesn't require AspectJ configuration. Learn more about the functional approach.

Java Compatibility

Powertools for AWS Lambda (Java) supports all Java versions from 11 to 25 in line with the corresponding Lambda runtimes.

In addition to the functional approach, Logging, Metrics, Tracing, Parameters, Idempotency, Validation, and Large Messages utilities support annotations using AspectJ, which require configuration of the aspectjrt runtime library.

You may need to add the appropriate version of aspectjrt to your dependencies based on the JDK used for building your function:

1
2
3
4
5
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.??</version>
</dependency>

Use the following dependency matrix to understand which AspectJ version to use based on your JDK version:

JDK version aspectj version
11-17 1.9.20.1 (or higher)
21 1.9.21 (or higher)
25 1.9.25 (or higher)

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_METRICS_FUNCTION_NAME Function name used as dimension for the cold start metric Metrics
POWERTOOLS_METRICS_DISABLED Disables all flushing of 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