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:
yarn add --dev datadog-cdk-constructs-v2
# or
npm install datadog-cdk-constructs-v2 --save-dev
pip install datadog-cdk-constructs-v2
Pay attention to the output from your package manager as the Datadog CDK Construct Library has peer dependencies.
go get github.com/DataDog/datadog-cdk-constructs-go/ddcdkconstruct/v3
Add to your pom.xml:
<dependency>
<groupId>com.datadoghq</groupId>
<artifactId>datadog-cdk-constructs</artifactId>
<version>3.3.0</version>
</dependency>
Add this to your CDK stack:
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>])
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)
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()
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 is enabled by default through automatic lambda tagging, and will work if:
datadog-cdk-constructs-v2 version is >= 1.4.0If 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)
}
}
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 |
$ claude mcp add datadog-cdk-constructs \
-- python -m otcore.mcp_server <graph>