MCPcopy Index your code
hub / github.com/DataDog/datadog-cdk-constructs

github.com/DataDog/datadog-cdk-constructs @v2-4.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2-4.2.0 ↗ · + Follow
272 symbols 731 edges 105 files 22 documented · 8%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Datadog CDK Constructs

NPM PyPI Go Maven License

Use this Datadog CDK Construct Library to deploy serverless applications using AWS CDK.

For more information on the DatadogECSFargate construct, see [here][23].

This CDK library automatically configures ingestion of metrics, traces, and logs from your serverless applications by:

  • Installing and configuring the Datadog Lambda layers for your [Python][1], [Node.js][2], [Java][15], [Go][26], [Ruby][25], and [.NET][19] Lambda functions.
  • Enabling the collection of traces and custom metrics from your Lambda functions.
  • Managing subscriptions from the Datadog Forwarder to your Lambda and non-Lambda log groups.

Lambda

Package Installation

npm

yarn add --dev datadog-cdk-constructs-v2
# or
npm install datadog-cdk-constructs-v2 --save-dev

PyPI

pip install datadog-cdk-constructs-v2
Note:

Pay attention to the output from your package manager as the Datadog CDK Construct Library has peer dependencies.

Go

go get github.com/DataDog/datadog-cdk-constructs-go/ddcdkconstruct/v3

Maven (Java)

Add to your pom.xml:

<dependency>
    <groupId>com.datadoghq</groupId>
    <artifactId>datadog-cdk-constructs</artifactId>
    <version>3.3.0</version>
</dependency>

Usage

AWS CDK

Add this to your CDK stack:

TypeScript

import { DatadogLambda, DatadogDefaultLayerVersions } from "datadog-cdk-constructs-v2";

const datadogLambda = new DatadogLambda(this, "datadogLambda", {
  // Lambda library layer versions are optional; they default to the latest
  // version bundled with this construct. Set them to pin a specific version.
  addLayers: <BOOLEAN>,
  extensionLayerVersion: DatadogDefaultLayerVersions.EXTENSION,
  forwarderArn: "<FORWARDER_ARN>",
  createForwarderPermissions: <BOOLEAN>,
  flushMetricsToLogs: <BOOLEAN>,
  site: "<SITE>",
  apiKey: "{Datadog_API_Key}",
  apiKeySecretArn: "{Secret_ARN_Datadog_API_Key}",
  apiKeySecret: <AWS_CDK_ISECRET>, // Only available in datadog-cdk-constructs-v2
  apiKmsKey: "{Encrypted_Datadog_API_Key}",
  enableDatadogTracing: <BOOLEAN>,
  enableMergeXrayTraces: <BOOLEAN>,
  enableDatadogLogs: <BOOLEAN>,
  injectLogContext: <BOOLEAN>,
  logLevel: <STRING>,
  env: <STRING>, //Optional
  service: <STRING>, //Optional
  version: <STRING>, //Optional
  tags: <STRING>, //Optional
});
datadogLambda.addLambdaFunctions([<LAMBDA_FUNCTIONS>])
datadogLambda.addForwarderToNonLambdaLogGroups([<LOG_GROUPS>])

Python

from datadog_cdk_constructs_v2 import DatadogLambda, DatadogDefaultLayerVersions
datadog = DatadogLambda(
    self,
    "Datadog",
    # Lambda library layer versions are optional; they default to the latest
    # version bundled with this construct. Set them to pin a specific version.
    extension_layer_version=DatadogDefaultLayerVersions.EXTENSION,
    add_layers=<BOOLEAN>,
    api_key=os.getenv("DD_API_KEY"),
    site=<SITE>,
)
datadog.add_lambda_functions([<LAMBDA_FUNCTIONS>])
datadog.add_forwarder_to_non_lambda_log_groups(self.logGroups)

Go

import (
    "github.com/DataDog/datadog-cdk-constructs-go/ddcdkconstruct/v3"
)
datadogLambda := ddcdkconstruct.NewDatadogLambda(
    stack,
    jsii.String("Datadog"),
    &ddcdkconstruct.DatadogLambdaProps{
        // Lambda library layer versions are optional; they default to the latest
        // version bundled with this construct. Set them to pin a specific version.
        ExtensionLayerVersion: ddcdkconstruct.DatadogDefaultLayerVersions_EXTENSION(),
        AddLayers:             jsii.Bool(<BOOLEAN>),
        Site:                  jsii.String(<SITE>),
        ApiKey:                jsii.String(os.Getenv("DD_API_KEY")),
        // ...
    })
datadogLambda.AddLambdaFunctions(&[]interface{}{myFunction}, nil)
datadogLambda.AddForwarderToNonLambdaLogGroups()

Java

import com.datadoghq.cdkconstructs.DatadogLambda;
import com.datadoghq.cdkconstructs.DatadogLambdaProps;
import com.datadoghq.cdkconstructs.DatadogDefaultLayerVersions;

DatadogLambda datadogLambda = new DatadogLambda(this, "Datadog",
    DatadogLambdaProps.builder()
        // Lambda library layer versions are optional; they default to the latest
        // version bundled with this construct. Set them to pin a specific version.
        .addLayers(<BOOLEAN>)
        .extensionLayerVersion(DatadogDefaultLayerVersions.EXTENSION)
        .flushMetricsToLogs(<BOOLEAN>)
        .site("<SITE>")
        .apiKey(System.getenv("DD_API_KEY"))
        .enableDatadogTracing(<BOOLEAN>)
        .enableDatadogLogs(<BOOLEAN>)
        .build()
);
datadogLambda.addLambdaFunctions(new Object[]{myFunction});
datadogLambda.addForwarderToNonLambdaLogGroups(new Object[]{myLogGroup});

Source Code Integration

Source code integration is enabled by default through automatic lambda tagging, and will work if:

  • The Datadog Github integration is installed.
  • Your datadog-cdk-constructs-v2 version is >= 1.4.0

Alternative Methods to Enable Source Code Integration

If the automatic implementation doesn't work for your case, please follow one of the two guides below.

Note: these alternate guides only work for Typescript.

datadog-cdk version satisfied, but Datadog Github integration NOT installed

If the Datadog Github integration is not installed, you need to import the datadog-ci package and manually upload your Git metadata to Datadog. For the best results, import the datadog-ci package where your CDK Stack is initialized.

const app = new cdk.App();

// Make sure to add @datadog/datadog-ci via your package manager
const datadogCi = require("@datadog/datadog-ci");
// Manually uploading Git metadata to Datadog.
datadogCi.gitMetadata.uploadGitCommitHash("{Datadog_API_Key}", "<SITE>");

const app = new cdk.App();
new ExampleStack(app, "ExampleStack", {});

app.synth();

datadog-cdk version NOT satisfied

Change your initialization function as follows (in this case, gitHash value is passed to the CDK):

async function main() {
  // Make sure to add @datadog/datadog-ci via your package manager
  const datadogCi = require("@datadog/datadog-ci");
  const [, gitHash] = await datadogCi.gitMetadata.uploadGitCommitHash("{Datadog_API_Key}", "<SITE>");

  const app = new cdk.App();
  // Pass in the hash to the ExampleStack constructor
  new ExampleStack(app, "ExampleStack", {}, gitHash);
}

Ensure you call this function to initialize your stack.

In your stack constructor, change to add an optional gitHash parameter, and call addGitCommitMetadata():

export class ExampleStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps, gitHash?: string) {
    ...
    ...
    datadogLambda.addGitCommitMetadata([<YOUR_FUNCTIONS>], gitHash)
  }
}

Configuration

To further configure your DatadogLambda construct for Lambda, use the following custom parameters:

Note: The descriptions use the npm package parameters, but they also apply to PyPI and Go package parameters.

npm package parameter PyPI package parameter Description
addLayers add_layers Whether to add the runtime Lambda Layers or expect the user to bring their own. Defaults to true. When false, you must include the Datadog Lambda library in your functions' deployment packages.
pythonLayerVersion python_layer_version Version of the Python Lambda layer to install, such as 122. Optional: Defaults to the latest version bundled with this construct (DatadogDefaultLayerVersions.PYTHON). Set this to pin a specific version. Find the latest version number [here][5]. Warning: This parameter and pythonLayerArn are mutually exclusive. If used, only set one or the other.
pythonLayerArn python_layer_arn The custom ARN of the Python Lambda layer to install. Required if you are deploying at least one Lambda function written in Python and addLayers is true. Warning: This parameter and pythonLayerVersion are mutually exclusive. If used, only set one or the other.
nodeLayerVersion node_layer_version Version of the Node.js Lambda layer to install, such as 133. Optional: Defaults to the latest version bundled with this construct (DatadogDefaultLayerVersions.NODE). Set this to pin a specific version. Find the latest version number from [here][6]. Warning: This parameter and nodeLayerArn are mutually exclusive. If used, only set one or the other.
nodeLayerArn node_layer_arn The custom ARN of the Node.js Lambda layer to install. Required if you are deploying at least one Lambda function written in Node.js and addLayers is true. Warning: This parameter and nodeLayerVersion are mutually exclusive. If used, only set one or the other.
javaLayerVersion java_layer_version Version of the Java layer to install, such as 25. Optional: Defaults to the latest version bundled with this construct (DatadogDefaultLayerVersions.JAVA). Set this to pin a specific version. Find the latest version number [here][28]. Note: extensionLayerVersion >= 25 and javaLayerVersion >= 5 are required for the DatadogLambda construct to instrument your Java functions properly. Warning: This parameter and javaLayerArn are mutually exclusive. If used, only set one or the other.
javaLayerArn java_layer_arn The custom ARN of the Java layer to install. Required if you are deploying at least one Lambda function written in Java and addLayers is true. Warning: This parameter and javaLayerVersion are mutually exclusive. If used, only set one or the other.
dotnetLayerVersion dotnet_layer_version Version of the .NET layer to install, such as 23. Optional: Defaults to the latest version bundled with this construct (DatadogDefaultLayerVersions.DOTNET). Set this to pin a specific version. Find the latest version number from [here][18]. Warning: This parameter and dotnetLayerArn are mutually exclusive. If

Extension points exported contracts — how you extend this code

DatadogLambdaProps (Interface)
(no doc)
src/interfaces.ts
DatadogECSBaseProps (Interface)
(no doc)
src/ecs/interfaces.ts
LambdaLayer (Interface)
(no doc)
e2e/helpers/lambda-verifier.ts
DatadogLambdaStrictProps (Interface)
(no doc)
src/interfaces.ts
LogCollectionFeatureConfig (Interface)
(no doc)
src/ecs/interfaces.ts
LambdaConfiguration (Interface)
(no doc)
e2e/helpers/lambda-verifier.ts
Runtime (Interface)
(no doc)
src/interfaces.ts
DogstatsdFeatureConfig (Interface)
(no doc)
src/ecs/interfaces.ts

Core symbols most depended-on inside this repo

addLambdaFunctions
called by 138
src/datadog-lambda.ts
add
called by 39
src/ecs/environment.ts
addContainer
called by 23
src/ecs/fargate/datadog-ecs-fargate.ts
toString
called by 15
src/ecs/environment.ts
findDatadogSubscriptionFilters
called by 13
test/test-utils.ts
setEnvIfUndefined
called by 12
src/env.ts
addForwarderToNonLambdaLogGroups
called by 11
src/datadog-lambda.ts
applyLayers
called by 11
src/layer.ts

Shape

Function 104
Method 65
Class 64
Interface 29
Enum 5
Struct 5

Languages

TypeScript80%
Go8%
Python7%
Java4%
C#1%

Modules by API surface

src/datadog-lambda.ts17 symbols
src/ecs/fargate/datadog-ecs-fargate.ts16 symbols
e2e/helpers/lambda-telemetry-checker.ts14 symbols
src/layer.ts10 symbols
src/datadog-step-functions.ts10 symbols
e2e/helpers/lambda-verifier.ts10 symbols
src/ecs/environment.ts9 symbols
src/env.ts7 symbols
src/ecs/interfaces.ts7 symbols
src/ecs/fargate/interfaces.ts7 symbols
src/interfaces.ts6 symbols
src/forwarder.ts6 symbols

For agents

$ claude mcp add datadog-cdk-constructs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact