Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
AWS Lambda profiling extension
The Pyroscope AWS Lambda extension is a robust tool for profiling AWS Lambda functions, ensuring minimal latency impact. This profiling is essential for optimizing your functions.
Why profile AWS Lambda functions?
AWS Lambda functions, while powerful and flexible, can lead to significant costs if not managed efficiently. Serverless architectures, like AWS Lambda, can mask performance issues. Since Lambda functions are billed based on execution time and allocated memory, code inefficiencies can lead to higher costs. Often, these costs accumulate unnoticed because of the following reasons:
Granular billing: Lambda functions are billed in milliseconds, which can make small inefficiencies seem insignificant at first. However, when scaled to thousands or millions of invocations, these inefficiencies can lead to substantial costs.
Complex performance profile: Lambda functions may interact with various services and resources, making it challenging to pinpoint performance bottlenecks.
Variable load: The serverless nature of AWS Lambda means that functions might handle variable loads at different times, making it hard to optimize for every scenario.
Profiling Lambda functions helps identify these hidden performance bottlenecks, enabling developers to optimize their code for both performance and cost. Effective profiling can reveal inefficient code paths, unnecessary memory usage, and areas where the execution time can be reduced. By addressing these issues, organizations can significantly reduce their AWS bill, improve application responsiveness, and ensure a more efficient use of resources.
Architecture
This extension runs a relay server on the same network namespace as the Lambda function, ensuring minimal added latency.
For more details, refer to the Building Extensions for AWS Lambda blog post.
Set up the Pyroscope Lambda extension
To set up the Pyroscope Lambda extension, you need to:
- Configure your Lambda function
- Set up your environment variables
- Integrate the Pyroscope SDK
Configure your Lambda function
Configure your Lambda function to use the extension. Find the latest release on our releases page.
Set up the environment variables
Configure the extension with the following environment variables:
Environment Variable | Default Value | Description |
---|---|---|
PYROSCOPE_REMOTE_ADDRESS | https://profiles-prod-001.grafana.net | Destination for relayed Pyroscope data |
PYROSCOPE_BASIC_AUTH_USER | "" | HTTP Basic authentication user |
PYROSCOPE_BASIC_AUTH_PASSWORD | "" | HTTP Basic authentication password |
PYROSCOPE_SELF_PROFILING | false | Whether to profile the extension itself |
PYROSCOPE_LOG_LEVEL | info | Log level (error , info , debug , trace ) |
PYROSCOPE_TIMEOUT | 10s | HTTP client timeout (in Go duration format) |
PYROSCOPE_NUM_WORKERS | 5 | Number of relay workers |
PYROSCOPE_TENANT_ID | "" | Pyroscope tenant ID (for multi-tenancy) |
Integrate the Pyroscope SDK
The Pyroscope AWS Lambda extension is compatible with all existing Pyroscope SDKs. Here are some key considerations:
- Initialize the SDK before setting up the AWS Lambda handler.
- Ensure that the Pyroscope server address is configured to http://localhost:4040.
Note that the SDK packages are not automatically included in the extension layer. For Java, Python, Node.js, and Ruby, you must either include the SDK package in the function deployment package or add it as a Lambda layer. Refer to the detailed guide in the AWS Lambda documentation for your specific runtime for further instructions:
For a Golang Lambda function, integrate the Pyroscope SDK as follows:
func HandleRequest(ctx context.Context) (string, error) {
return "Hello world!", nil
}
func main() {
pyroscope.Start(pyroscope.Config{
ApplicationName: "simple.golang.lambda",
ServerAddress: "http://localhost:4040",
})
lambda.Start(HandleRequest)
}
Replace simple.golang.lambda
with your application name.
Use cases
Once set up, you can use the Pyroscope UI to analyze your Lambda function’s data to facilitate performance optimizations. For more on this, visit our Pyroscope AWS Lambda Extension blog post.
Send data to Pyroscope
To configure the extension for data transmission:
PYROSCOPE_REMOTE_ADDRESS="<URL>"
PYROSCOPE_BASIC_AUTH_USER="<User>"
PYROSCOPE_BASIC_AUTH_PASSWORD="<Password>"
# PYROSCOPE_TENANT_ID="<TenantID>" # For multi-tenant mode
Replace placeholders accordingly. For sending data to Grafana, use your Grafana stack user and API key for authentication.