Datadog CDK Constructs v2
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
-
ModuleNotFoundError: No module named 'datadog_cdk_constructs'
cause Attempting to import the CDK v2 construct using the v1 package name or a misspelling (hyphen vs. underscore).fixEnsure you have installed `datadog-cdk-constructs-v2` and are importing it as `from datadog_cdk_constructs_v2 import Datadog`. -
No metrics, traces, or logs appearing in Datadog for instrumented Lambda functions.
cause Missing or incorrect Datadog API/APP keys or `datadog_site` configuration, or network issues preventing Lambda from reaching Datadog endpoints.fixVerify `DD_API_KEY` and `DD_APP_KEY` are correctly set as environment variables or passed as props to the `Datadog` construct. Confirm `datadog_site` (e.g., 'datadoghq.com', 'datadoghq.eu') matches your Datadog organization. Check Lambda VPC/security group configurations if applicable. -
TypeError: __init__() got an unexpected keyword argument 'enableDatadogASM'
cause Using the deprecated `enableDatadogASM` property in a newer version of the construct.fixReplace `enableDatadogASM` with `datadogAppSecMode`. For example, `enableDatadogASM=True` becomes `datadogAppSecMode='auto'`. -
ValueError: Runtime.PYTHON_3_X is not supported for Datadog integration.
cause Attempting to instrument a Lambda function with an unsupported or end-of-life Python runtime version (e.g., Python 3.6, 3.7, 3.8) with recent Datadog layers, or using an outdated `Datadog` construct version.fixUpgrade your Lambda function's runtime to a supported version, typically Python 3.9 or newer. Consult Datadog's official documentation for current Lambda layer runtime compatibility and ensure you are using a recent version of `datadog-cdk-constructs-v2`.
Warnings
- breaking Migration from `datadog-cdk-constructs` (v1) to `datadog-cdk-constructs-v2` requires significant code changes. This package is explicitly designed for AWS CDK v2 (`aws-cdk-lib`) and is not compatible with CDK v1 (`@aws-cdk/core`). The import paths and construct signatures have changed.
- gotcha Incorrect or missing Datadog API/APP keys, or an incorrect `datadog_site` can lead to no metrics, traces, or logs appearing in Datadog. These are crucial for proper integration.
- deprecated The `enableDatadogASM` property has been deprecated in favor of `datadogAppSecMode` for configuring Application Security Monitoring (ASM).
- gotcha Lambda functions deployed with Python runtimes earlier than 3.9, or Node.js runtimes earlier than 16, may experience issues with newer Datadog layers or extensions, potentially leading to deployment failures or runtime errors.
Install
-
pip install datadog-cdk-constructs-v2
Imports
- Datadog
from datadog_cdk_constructs import Datadog
from datadog_cdk_constructs_v2 import Datadog
Quickstart
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()