Powertools for AWS Lambda (Java) is a collection of utilities designed to help you build serverless applications on AWS.
The toolkit is modular, so you can pick and choose the utilities you need for your application, but also combine them for a complete solution for your serverless applications.
If you prefer using annotations to apply cross-cutting concerns to your Lambda handlers, the AspectJ annotation pattern is a good fit. This approach lets you decorate methods with Powertools utilities using annotations, applying their functionality with minimal code changes.
This pattern works well when you want to keep your business logic clean and separate concerns using aspect-oriented programming.
Note
This approach requires configuring AspectJ compile-time weaving in your build tool (Maven or Gradle). See the installation guide for setup instructions.
If you prefer a more functional programming style or want to avoid AspectJ configuration, you can use the Powertools for AWS Lambda (Java) utilities directly in your code. This approach is more explicit and provides full control over how the utilities are applied.
This pattern is ideal when you want to avoid AspectJ setup or prefer a more imperative style. It also eliminates the AspectJ runtime dependency, making your deployment package more lightweight.
importcom.amazonaws.services.lambda.runtime.Context;importcom.amazonaws.services.lambda.runtime.RequestHandler;importcom.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;importcom.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importsoftware.amazon.lambda.powertools.logging.CorrelationIdPaths;importsoftware.amazon.lambda.powertools.logging.PowertoolsLogging;publicclassAppimplementsRequestHandler<APIGatewayProxyRequestEvent,APIGatewayProxyResponseEvent>{privatestaticfinalLoggerlog=LoggerFactory.getLogger(App.class);publicAPIGatewayProxyResponseEventhandleRequest(APIGatewayProxyRequestEventinput,Contextcontext){returnPowertoolsLogging.withLogging(context,0.7,CorrelationIdPaths.API_GATEWAY_REST,input,()->processRequest(input));}privateAPIGatewayProxyResponseEventprocessRequest(APIGatewayProxyRequestEventinput){// do something with inputlog.info("Processing request");returnnewAPIGatewayProxyResponseEvent().withStatusCode(200).withBody("Success");}}
importcom.amazonaws.services.lambda.runtime.Context;importcom.amazonaws.services.lambda.runtime.RequestHandler;importcom.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;importcom.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;importsoftware.amazon.lambda.powertools.tracing.TracingUtils;publicclassAppimplementsRequestHandler<APIGatewayProxyRequestEvent,APIGatewayProxyResponseEvent>{publicAPIGatewayProxyResponseEventhandleRequest(APIGatewayProxyRequestEventinput,Contextcontext){TracingUtils.withSubsegment("processPayment",subsegment->{subsegment.putAnnotation("operation","payment");// Business logic here});returnnewAPIGatewayProxyResponseEvent().withStatusCode(200).withBody("Success");}}
Note
The functional approach is available for all utilities. Further examples and detailed usage can be found in the individual documentation pages for each utility.