Menu

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.

Open source

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.

Lambda Extension Architecture

For more details, refer to the Building Extensions for AWS Lambda blog post.

Set up the Pyroscope Lambda extension

To set up the Pyroscope Lamnda extension, you need to:

  1. Configure your Lamda function
  2. Set up your environment variables
  3. 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 VariableDefault ValueDescription
PYROSCOPE_REMOTE_ADDRESShttps://profiles-prod-001.grafana.netDestination for relayed Pyroscope data
PYROSCOPE_BASIC_AUTH_USER""HTTP Basic authentication user
PYROSCOPE_BASIC_AUTH_PASSWORD""HTTP Basic authentication password
PYROSCOPE_SELF_PROFILINGfalseWhether to profile the extension itself
PYROSCOPE_LOG_LEVELinfoLog level (error, info, debug, trace)
PYROSCOPE_TIMEOUT10sHTTP client timeout (in Go duration format)
PYROSCOPE_NUM_WORKERS5Number of relay workers
PYROSCOPE_TENANT_ID""Pyroscope tenant ID (for multi-tenancy)

Integrate the Pyroscope SDK

For a Golang Lambda function, integrate the Pyroscope SDK as follows:

Go
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. The ServerAddress must be http://localhost:4040.

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:

bash
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.