{"id":8066,"library":"datadog-cdk-constructs-v2","title":"Datadog CDK Constructs v2","description":"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.","status":"active","version":"3.10.0","language":"en","source_language":"en","source_url":"https://github.com/DataDog/datadog-cdk-constructs","tags":["aws-cdk","datadog","serverless","lambda","monitoring","observability","cloud-infrastructure","cdk-v2"],"install":[{"cmd":"pip install datadog-cdk-constructs-v2","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for building AWS CDK v2 applications. Python 3.9+ is required for the construct.","package":"aws-cdk-lib","optional":false},{"reason":"Required by AWS CDK v2.","package":"constructs","optional":false}],"imports":[{"note":"This package is specifically for AWS CDK v2. The older `datadog-cdk-constructs` (without the `-v2` suffix) is for AWS CDK v1 and uses a different Python import path (`datadog_cdk_constructs`). Ensure you are using the `_v2` suffix in your import statement for this package.","wrong":"from datadog_cdk_constructs import Datadog","symbol":"Datadog","correct":"from datadog_cdk_constructs_v2 import Datadog"}],"quickstart":{"code":"import os\nfrom aws_cdk import App, Stack, Environment\nfrom aws_cdk import aws_lambda as _lambda\nfrom constructs import Construct\nfrom datadog_cdk_constructs_v2 import Datadog\n\nclass MyDatadogStack(Stack):\n    def __init__(self, scope: Construct, id: str, **kwargs) -> None:\n        super().__init__(scope, id, **kwargs)\n\n        # Define a simple Lambda function\n        my_lambda = _lambda.Function(\n            self, \"MyDatadogFunction\",\n            runtime=_lambda.Runtime.PYTHON_3_9, # Python 3.9+ recommended for latest layers\n            handler=\"app.handler\",\n            code=_lambda.Code.from_inline(\n                \"\"\"import json\nimport os\n\ndef handler(event, context):\n    print('Hello from Lambda!')\n    # Simulate some work for traces/metrics\n    if os.environ.get('DD_TRACE_ENABLED') == 'true':\n        import datadog_lambda.trigger\n    return {\n        'statusCode': 200,\n        'body': json.dumps('Success')\n    }\"\"\"),\n        )\n\n        # Apply Datadog integration to the Lambda function\n        datadog = Datadog(self, \"Datadog\",\n            datadog_api_key=os.environ.get(\"DD_API_KEY\", \"\"), # Required\n            datadog_app_key=os.environ.get(\"DD_APP_KEY\", \"\"), # Recommended for full features (e.g., custom metrics)\n            datadog_site=\"datadoghq.com\", # Or 'datadoghq.eu', 'us3.datadoghq.com', etc.\n            # Optional configurations:\n            # enable_lambda_metric_collection=True,\n            # enable_tracing=True,\n            # enable_forwarder=True, # For log collection\n            # add_extension=True, # Use Datadog Lambda Extension\n            # service=\"my-lambda-service\",\n            # env=\"prod\",\n            # version=\"1.0.0\"\n        )\n        datadog.add_lambda_functions([my_lambda])\n\napp = App()\nMyDatadogStack(app, \"DatadogIntegrationStack\",\n               env=Environment(account=os.environ.get(\"CDK_DEFAULT_ACCOUNT\"),\n                               region=os.environ.get(\"CDK_DEFAULT_REGION\"))\n               )\napp.synth()","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your project is using `aws-cdk-lib` (CDK v2) and update all `datadog_cdk_constructs` imports to `datadog_cdk_constructs_v2`. Refer to the official migration guide if transitioning from a CDK v1 project.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Verify that `DD_API_KEY` and `DD_APP_KEY` environment variables are correctly set during deployment, or passed directly to the `Datadog` construct. Confirm `datadog_site` (e.g., `datadoghq.com`, `datadoghq.eu`, `us3.datadoghq.com`) matches your Datadog organization's region.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update your `Datadog` construct properties. Replace `enableDatadogASM=True` with `datadogAppSecMode='auto'` or another desired mode (e.g., `'iast'` or `'full'`). Set to `None` to disable. Check the latest documentation for available `datadogAppSecMode` values.","message":"The `enableDatadogASM` property has been deprecated in favor of `datadogAppSecMode` for configuring Application Security Monitoring (ASM).","severity":"deprecated","affected_versions":"v2-3.3.0 and newer"},{"fix":"Ensure your Lambda function runtimes are compatible with the latest Datadog layers. Datadog generally supports current and recent LTS runtimes. Upgrade your Lambda functions to Python 3.9+ or Node.js 16+ where possible, or explicitly configure older Datadog layer versions if necessary.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure you have installed `datadog-cdk-constructs-v2` and are importing it as `from datadog_cdk_constructs_v2 import Datadog`.","cause":"Attempting to import the CDK v2 construct using the v1 package name or a misspelling (hyphen vs. underscore).","error":"ModuleNotFoundError: No module named 'datadog_cdk_constructs'"},{"fix":"Verify `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.","cause":"Missing or incorrect Datadog API/APP keys or `datadog_site` configuration, or network issues preventing Lambda from reaching Datadog endpoints.","error":"No metrics, traces, or logs appearing in Datadog for instrumented Lambda functions."},{"fix":"Replace `enableDatadogASM` with `datadogAppSecMode`. For example, `enableDatadogASM=True` becomes `datadogAppSecMode='auto'`.","cause":"Using the deprecated `enableDatadogASM` property in a newer version of the construct.","error":"TypeError: __init__() got an unexpected keyword argument 'enableDatadogASM'"},{"fix":"Upgrade 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`.","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.","error":"ValueError: Runtime.PYTHON_3_X is not supported for Datadog integration."}]}