This repository contains a sample Java project to deploy an AWS Lambda function using AWS SAM. The main goal of this project is to provide a testing ground for triaging issues and bug reports related to Powertools for AWS Lambda (Java).
This project requires:
- Java 21
- Maven 3.9+
SDKMAN! is useful to switch Java versions quickly to reproduce issues on different Java versions.
# Install SDKMAN! (if not already installed)
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
# Install Java 21 (Amazon Corretto)
sdk install java 21.0.8-amzn
# Install Maven
sdk install maven
# Verify installations
java -version
mvn -version- Fork the repository using the "Use this template" button (green, top right).
- Clone your forked repository to your local machine.
- Run
mvn clean compileto compile the project and install dependencies. - Make changes to the Lambda function code in
src/main/java/triage/TriageHandler.java. - Deploy the stack using
sam build && sam deploy --guided(first time) orsam deploy(subsequent deployments). - Test the Lambda function by invoking it with a sample event payload from the issue report or by using the provided event in the
events/directory. - Observe the logs in CloudWatch to verify the function's behavior.
- If you are able to reproduce the issue, update this
README.mdfile with the details of the issue, including:- Steps to reproduce
- Expected behavior
- Actual behavior
- Any relevant logs or error messages
- Push your changes to your forked repository and report your findings in the original issue report on the Powertools for AWS Lambda GitHub repository.
powertools-triaging-template-java/
├── pom.xml # Maven project configuration
├── template.yaml # SAM template defining infrastructure
├── src/
│ ├── main/java/triage/
│ │ └── TriageHandler.java # Main Lambda function handler
│ └── test/java/triage/
│ └── TriageHandlerTest.java # Unit tests for the handler
└── events/
└── event.json # Sample API Gateway event payload
This application uses SAM as IaC tool and creates:
- A Lambda function running Java 21 called
TriageFunction - An API Gateway endpoint at
/triage - CloudWatch Log Group for the Lambda function
- CloudFormation outputs for key resources
The Lambda function code is in src/main/java/triage/TriageHandler.java.
# Compile the project only (skip tests) - useful for reproducing issues quickly
mvn clean compile
# Compile both main code and test code (but don't run tests)
mvn clean test-compile
# Compile and run unit tests - useful for triaging unit test related issues
mvn clean test
# Clean and compile everything including tests
mvn clean compile test-compile# Build the SAM application
sam build
# Build the SAM application without running unit tests
MAVEN_OPTS="-DskipTests=true" sam build
# Deploy the stack (first time - guided)
sam deploy --guided
# Deploy the stack (subsequent deployments)
sam deploy# Invoke function locally with sample event
sam local invoke TriageFunction -e events/event.json
# Start API Gateway locally
sam local start-api
# Test the local API
curl http://localhost:3000/triage# Run all unit tests
mvn test
# Run tests with verbose output
mvn test -X
# Run specific test class
mvn test -Dtest=TriageHandlerTest
# Run tests and generate reports
mvn clean test surefire-report:report# Tail CloudWatch logs
sam logs -n TriageFunction --stack-name <your-stack-name> --tail
# View CloudWatch logs
sam logs -n TriageFunction --stack-name <your-stack-name>The project is configured with the following Powertools environment variables in template.yaml:
POWERTOOLS_LOG_LEVEL: Set toINFOPOWERTOOLS_LOGGER_SAMPLE_RATE: Set to0.1(10% sampling)POWERTOOLS_LOGGER_LOG_EVENT: Set totrue(logs incoming events)POWERTOOLS_METRICS_NAMESPACE: Set toTriagingTemplatePOWERTOOLS_SERVICE_NAME: Set totriage
When triaging runtime or deployment issues, you may want to skip tests for faster iteration:
mvn clean compile.
MAVEN_OPTS="-DskipTests=true" sam build
sam deployWhen customers report issues with Powertools in unit tests, run the full test suite:
mvn clean test
# Check test reports in target/surefire-reports/ (or in local stdout)When investigating compilation or dependency issues:
mvn clean compile -X # Verbose output for debugging
mvn dependency:tree # Check dependency conflicts, useful to debug CVEs in transitive dependencies