Lambda Extensions are a way to inject custom code into your Lambda function’s execution environment. They run alongside your function, can be invoked before or after your function, and can even process invocation events.

Here’s a Lambda function, written in Python, that uses a Datadog Lambda Extension to collect metrics and traces:

import json
import os

def lambda_handler(event, context):
    # Your function logic here
    print("Hello from Lambda!")

    # Simulate some work
    for _ in range(100000):
        pass

    return {
        "statusCode": 200,
        "body": json.dumps("Function executed successfully!")
    }

To add Datadog monitoring, you’ll need to:

  1. Create a Lambda Layer for the Datadog Extension:

    • Go to the Datadog documentation for Lambda Extensions and follow the instructions to create a Lambda Layer. This typically involves downloading a ZIP file provided by Datadog.
    • Upload this ZIP file as a Lambda Layer in your AWS account.
  2. Configure your Lambda Function:

    • Attach the Datadog Layer: In your Lambda function’s configuration, under "Layers," add the Datadog Lambda Extension layer you just created.
    • Set Environment Variables: You need to configure environment variables for the Datadog Extension to know where to send data and how to authenticate.
      • DD_SITE: Set this to your Datadog site (e.g., datadoghq.com, us3.datadoghq.com).
      • DD_API_KEY: Your Datadog API key.
      • DD_LAMBDA_EXTENSION_LAYER_VERSION_ARN: This is automatically populated by Lambda when you add the layer. You don’t need to set it manually.
      • DD_ENV: (Optional) Tag your traces and metrics with an environment name (e.g., prod, staging).
      • DD_SERVICE: (Optional) Tag your traces and metrics with a service name (e.g., my-lambda-function).

    Your Lambda function’s configuration in the AWS console would look something like this:

    • Runtime: Python 3.9 (or your chosen runtime)
    • Handler: index.lambda_handler
    • Layers:
      • Datadog Lambda Extension (Version 1)
    • Environment Variables:
      • DD_SITE: datadoghq.com
      • DD_API_KEY: a1b2c3d4e5f678901234567890abcdef
      • DD_ENV: dev
      • DD_SERVICE: my-cool-lambda
  3. Enable Tracing in Lambda:

    • In your Lambda function’s configuration, under "Monitoring and operations tools," ensure "Active tracing" is enabled. This allows Lambda to send invocation and execution data to the extension.

Once configured, the Datadog Lambda Extension will automatically:

  • Collect Logs: It captures stdout and stderr from your Lambda function, enriching them with trace IDs and other metadata before sending them to Datadog.
  • Collect Metrics: It gathers standard Lambda metrics (invocations, duration, errors, throttles) and custom metrics emitted by your function.
  • Collect Traces: It instruments your function’s execution to generate distributed traces, showing the flow of requests across your services.

The extension acts as a local agent within the Lambda execution environment. When your Lambda function is invoked, the extension is also initialized. It then receives invocation events from Lambda, processes them, and forwards the collected data to the Datadog backend via the configured API key and site. The key benefit here is that the extension handles the data collection and transmission outside of your function’s code, meaning your function logic remains clean and uninstrumented, while still getting comprehensive observability.

The most surprising thing about Lambda Extensions is that they extend the Lambda runtime itself, not just your function. They can be invoked before your function handler runs (e.g., to fetch secrets) and after it completes (e.g., to process results or send final telemetry).

The next concept to explore is how to use the Datadog Lambda Extension to send custom metrics directly from your function code.

Want structured learning?

Take the full Lambda course →