github.com/grafana/pyroscope@v1.18.0/docs/sources/configure-client/aws-lambda.md (about)

     1  ---
     2  title: "AWS Lambda profiling extension"
     3  menuTitle: "AWS Lambda profiling extension"
     4  description: "Profiling AWS Lambda functions with Pyroscope"
     5  weight: 500
     6  ---
     7  
     8  # AWS Lambda profiling extension
     9  
    10  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.
    11  
    12  ## Why profile AWS Lambda functions?
    13  
    14  AWS Lambda functions, while powerful and flexible, can lead to significant costs if not managed efficiently.
    15  Serverless architectures, like AWS Lambda, can mask performance issues.
    16  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:
    17  
    18  * **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.
    19  
    20  * **Complex performance profile**: Lambda functions may interact with various services and resources, making it challenging to pinpoint performance bottlenecks.
    21  
    22  * **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.
    23  
    24  Profiling Lambda functions helps identify these hidden performance bottlenecks, enabling developers to optimize their code for both performance and cost.
    25  Effective profiling can reveal inefficient code paths, unnecessary memory usage, and areas where the execution time can be reduced.
    26  By addressing these issues, organizations can significantly reduce their AWS bill, improve application responsiveness, and ensure a more efficient use of resources.
    27  
    28  ## Architecture
    29  
    30  This extension runs a relay server on the same network namespace as the Lambda function, ensuring minimal added latency.
    31  
    32  ![Lambda Extension Architecture](https://user-images.githubusercontent.com/23323466/186037668-44de7caa-6576-422a-b3f7-8416325f4a98.png)
    33  
    34  For more details, refer to the [Building Extensions for AWS Lambda blog post](https://aws.amazon.com/blogs/compute/building-extensions-for-aws-lambda-in-preview/).
    35  
    36  ## Set up the Pyroscope Lambda extension
    37  
    38  To set up the Pyroscope Lambda extension, you need to:
    39  
    40  1. Configure your Lambda function
    41  1. Set up your environment variables
    42  1. Integrate the Pyroscope SDK
    43  
    44  ### Configure your Lambda function
    45  
    46  Configure your Lambda function to use the extension. Find the latest release on our [releases page](https://github.com/grafana/pyroscope-lambda-extension/releases).
    47  
    48  ### Set up the environment variables
    49  
    50  Configure the extension with the following environment variables:
    51  
    52  | Environment Variable           | Default Value                           | Description                                  |
    53  | ------------------------------ | --------------------------------------- | -------------------------------------------- |
    54  | `PYROSCOPE_REMOTE_ADDRESS`     | `https://profiles-prod-001.grafana.net` | Destination for relayed Pyroscope data       |
    55  | `PYROSCOPE_BASIC_AUTH_USER`    | `""`                                    | HTTP Basic authentication user               |
    56  | `PYROSCOPE_BASIC_AUTH_PASSWORD`| `""`                                    | HTTP Basic authentication password           |
    57  | `PYROSCOPE_SELF_PROFILING`     | `false`                                 | Whether to profile the extension itself      |
    58  | `PYROSCOPE_LOG_LEVEL`          | `info`                                  | Log level (`error`, `info`, `debug`, `trace`)|
    59  | `PYROSCOPE_TIMEOUT`            | `10s`                                   | HTTP client timeout (in Go duration format)  |
    60  | `PYROSCOPE_NUM_WORKERS`        | `5`                                     | Number of relay workers                      |
    61  | `PYROSCOPE_TENANT_ID`          | `""`                                    | Pyroscope tenant ID (for multi-tenancy)      |
    62  
    63  ### Integrate the Pyroscope SDK
    64  
    65  The Pyroscope AWS Lambda extension is compatible with all existing Pyroscope SDKs. Here are some key considerations:
    66   - Initialize the SDK before setting up the AWS Lambda handler.
    67   - Ensure that the Pyroscope server address is configured to http://localhost:4040.
    68  
    69  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:
    70   - [Java](https://docs.aws.amazon.com/lambda/latest/dg/java-package.html#java-package-layers)
    71   - [Python](https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies)
    72   - [Ruby](https://docs.aws.amazon.com/lambda/latest/dg/ruby-package.html#ruby-package-runtime-dependencies)
    73   - [Node.js](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html#nodejs-package-dependencies)
    74  
    75  For a Golang Lambda function, integrate the Pyroscope SDK as follows:
    76  
    77  ```go
    78  func HandleRequest(ctx context.Context) (string, error) {
    79      return "Hello world!", nil
    80  }
    81  
    82  func main() {
    83      pyroscope.Start(pyroscope.Config{
    84          ApplicationName: "simple.golang.lambda",
    85          ServerAddress:   "http://localhost:4040",
    86      })
    87      lambda.Start(HandleRequest)
    88  }
    89  ```
    90  
    91  Replace `simple.golang.lambda` with your application name.
    92  
    93  ## Use cases
    94  
    95  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](http://pyroscope.io/blog/profile-aws-lambda-functions).
    96  
    97  ## Send data to Pyroscope
    98  
    99  To configure the extension for data transmission:
   100  
   101  ```bash
   102  PYROSCOPE_REMOTE_ADDRESS="<URL>"
   103  PYROSCOPE_BASIC_AUTH_USER="<User>"
   104  PYROSCOPE_BASIC_AUTH_PASSWORD="<Password>"
   105  # PYROSCOPE_TENANT_ID="<TenantID>" # For multi-tenant mode
   106  ```
   107  
   108  Replace placeholders accordingly. For sending data to Grafana, use your Grafana stack user and API key for authentication.