Datadog CDK Constructs v2

3.10.0 · active · verified Thu Apr 16

Datadog CDK Constructs v2 is a Python library that provides AWS CDK v2 constructs to automatically instrument Python and Node.js Lambda functions with Datadog monitoring. It simplifies the process of integrating Datadog layers, environment variables, and permissions into your serverless applications. The library is actively maintained with frequent releases, typically bi-monthly or monthly, reflecting ongoing updates and feature additions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple AWS Lambda function and apply the Datadog CDK construct to instrument it for monitoring. Before deploying, ensure you have your AWS credentials configured and the `DD_API_KEY` and `DD_APP_KEY` environment variables set. Replace `datadoghq.com` with your specific Datadog site if needed. This example uses a Python 3.9 runtime, which is generally compatible with the latest Datadog layers.

import os
from aws_cdk import App, Stack, Environment
from aws_cdk import aws_lambda as _lambda
from constructs import Construct
from datadog_cdk_constructs_v2 import Datadog

class MyDatadogStack(Stack):
    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Define a simple Lambda function
        my_lambda = _lambda.Function(
            self, "MyDatadogFunction",
            runtime=_lambda.Runtime.PYTHON_3_9, # Python 3.9+ recommended for latest layers
            handler="app.handler",
            code=_lambda.Code.from_inline(
                """import json
import os

def handler(event, context):
    print('Hello from Lambda!')
    # Simulate some work for traces/metrics
    if os.environ.get('DD_TRACE_ENABLED') == 'true':
        import datadog_lambda.trigger
    return {
        'statusCode': 200,
        'body': json.dumps('Success')
    }"""),
        )

        # Apply Datadog integration to the Lambda function
        datadog = Datadog(self, "Datadog",
            datadog_api_key=os.environ.get("DD_API_KEY", ""), # Required
            datadog_app_key=os.environ.get("DD_APP_KEY", ""), # Recommended for full features (e.g., custom metrics)
            datadog_site="datadoghq.com", # Or 'datadoghq.eu', 'us3.datadoghq.com', etc.
            # Optional configurations:
            # enable_lambda_metric_collection=True,
            # enable_tracing=True,
            # enable_forwarder=True, # For log collection
            # add_extension=True, # Use Datadog Lambda Extension
            # service="my-lambda-service",
            # env="prod",
            # version="1.0.0"
        )
        datadog.add_lambda_functions([my_lambda])

app = App()
MyDatadogStack(app, "DatadogIntegrationStack",
               env=Environment(account=os.environ.get("CDK_DEFAULT_ACCOUNT"),
                               region=os.environ.get("CDK_DEFAULT_REGION"))
               )
app.synth()

view raw JSON →