Treblle with AWS Lambda
Requirements
- AWS Lambda environment
- Java 8+
- Maven or Gradle build system
Installation
Add the following dependency to your Maven project:
<dependency>
<groupId>com.treblle</groupId>
<artifactId>treblle-aws-lambda</artifactId>
<version>1.0.2</version>
</dependency>If you’re using Gradle, add:
implementation 'com.treblle:treblle-aws-lambda:1.0.2'Tip
Check Maven Central Repository for all available versions and metadata.
Getting started
First, you must create a FREE account on treblle.com to get an API key and SDK Token. After you have those, you must integrate Treblle into your AWS Lambda handler.
Basic Implementation
Here’s a simple example of integrating Treblle with an AWS Lambda function that handles API Gateway requests:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.treblle.aws.lambda.TreblleHandler;
public class MyLambdaHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final TreblleHandler treblleHandler;
public MyLambdaHandler() {
// Initialize Treblle with your API key and SDK Token
this.treblleHandler = new TreblleHandler(
"YOUR_API_KEY",
"YOUR_SDK_TOKEN"
);
}
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
// Your Lambda handler code wrapped with Treblle monitoring
return treblleHandler.handle(request, context, this::processRequest);
}
// Your actual business logic
private APIGatewayProxyResponseEvent processRequest(APIGatewayProxyRequestEvent request, Context context) {
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
try {
// Your API implementation
response.setStatusCode(200);
response.setBody("{\"message\":\"Success\"}");
} catch (Exception e) {
// Treblle will automatically capture this error
response.setStatusCode(500);
response.setBody("{\"error\":\"Internal server error\"}");
}
return response;
}
}That’s it!
Your API requests and responses from your AWS Lambda function are being sent to your Treblle dashboard. This gives you features like auto-documentation, real-time request/response monitoring, error tracking, etc.
Configuration Options
The TreblleHandler constructor supports several configuration options:
// Initialize Treblle with additional options
TreblleHandler treblleHandler = new TreblleHandler(
"YOUR_API_KEY", // Your Treblle API key
"YOUR_SDK_TOKEN", // Your Treblle SDK Token
Arrays.asList("secret_field", "sensitive_data"), // Additional fields to mask
false // Debug mode
);Environment Variables
You can also configure Treblle using environment variables in your AWS Lambda configuration:
Environment Variable
Purpose
TREBLLE_API_KEY
Your Treblle API key
TREBLLE_SDK_TOKEN
Your Treblle SDK Token
TREBLLE_MASKING_KEYWORDS
Comma-separated list of additional fields to mask
TREBLLE_DEBUG
Set to "true" to enable debug mode
TREBLLE_ENDPOINT
(Optional) Custom endpoint URL
With environment variables, you can simplify your code:
// Reads configuration from environment variables
TreblleHandler treblleHandler = new TreblleHandler();Need to hide additional fields?
Treblle automatically masks sensitive data like passwords, tokens, and authentication information. If you want to expand the list of fields you want to hide, you can pass additional field names to the constructor or set them via environment variables:
Via constructor
List<String> additionalFields = Arrays.asList("secretField", "privateKey");
TreblleHandler treblleHandler = new TreblleHandler(
"YOUR_API_KEY",
"YOUR_SDK_TOKEN",
additionalFields
);Via environment variables
TREBLLE_MASKING_KEYWORDS=secretField,privateKeyMasking Entire Objects
To mask entire JSON objects and all their properties, use the pattern:
fieldName.*Performance Considerations
Treblle is designed to have minimal impact on your Lambda’s performance:
- Initialize the TreblleHandler once in your class constructor to avoid creating it on each invocation
- Treblle uses asynchronous processing to minimize the impact on your Lambda execution time
- The SDK is lightweight and optimized for serverless environments
Deployment
When deploying your Lambda function with Treblle, make sure:
- Your function has internet access (if in a VPC).
- You’ve correctly set up environment variables for your API key and SDK Token.
- You’ve given your Lambda enough memory and timeout to accommodate the additional Treblle processing.
Note
Ensure your Lambda has appropriate IAM permissions and network access to communicate with Treblle’s servers.
Error Handling
Treblle automatically captures and reports errors that occur in your Lambda function. The SDK captures:
- Exception details
- Stacktrace information
- Request context
- Response data
This information is sent securely to your Treblle dashboard, where you can review and address issues.