The Datadog CLI modifies existing Lambda functions’ configurations to enable instrumentation without requiring a new deployment. It is the quickest way to get started with Datadog’s serverless monitoring.
You can also add the command to your CI/CD pipelines to enable instrumentation for all your serverless applications. Run the command after your normal serverless application deployment, so that changes made by the Datadog CLI command are not overridden.
If your Lambda function is configured to use code signing, you must add Datadog’s Signing Profile ARN (arn:aws:signer:us-east-1:464622532012:/signing-profiles/DatadogLambdaSigningProfile/9vMI9ZAGLc) to your function’s Code Signing Configuration before you can instrument it with the Datadog CLI.
More information and additional parameters can be found in the CLI documentation.
The Datadog Serverless Plugin automatically adds the Datadog Lambda library to your functions using layers, and configures your functions to send metrics, traces, and logs to Datadog through the Datadog Forwarder.
If your Lambda function is configured to use code signing, you must add Datadog’s Signing Profile ARN (arn:aws:signer:us-east-1:464622532012:/signing-profiles/DatadogLambdaSigningProfile/9vMI9ZAGLc) to your function’s Code Signing Configuration before you install the Datadog Serverless Plugin.
To install and configure the Datadog Serverless Plugin, follow these steps:
Install the Datadog Serverless Plugin:
yarn add --dev serverless-plugin-datadog
In your serverless.yml, add the following:
plugins:
- serverless-plugin-datadog
In your serverless.yml, also add the following section:
custom:
datadog:
forwarderArn: # The Datadog Forwarder ARN goes here.
More information on the Datadog Forwarder ARN or installation can be found here. For additional settings, see the plugin documentation.
Note: You need to follow these additional configuration steps if your Lambda function is simultaneously using Datadog’s tracing libraries and webpack.
The Datadog CloudFormation macro automatically transforms your SAM application template to add the Datadog Lambda library to your functions using layers, and configure your functions to send metrics, traces, and logs to Datadog through the Datadog Forwarder.
Run the following command with your AWS credentials to deploy a CloudFormation stack that installs the macro AWS resource. You only need to install the macro once for a given region in your account. Replace create-stack with update-stack to update the macro to the latest version.
Replace <SERVICE> and <ENV> with your service and environment values.
If your Lambda function is configured to use code signing, you must add Datadog’s Signing Profile ARN (arn:aws:signer:us-east-1:464622532012:/signing-profiles/DatadogLambdaSigningProfile/9vMI9ZAGLc) to your function’s Code Signing Configuration before you can use the macro.
More information and additional parameters can be found in the macro documentation.
The Datadog CDK Constructs automatically configure ingestion of metrics, traces, and logs from your serverless applications by:
Installing and configuring the Datadog Lambda library for your Python and Node.js Lambda functions.
Enabling the collection of traces and custom metrics from your Lambda functions.
Managing subscriptions from the Datadog Forwarder to your Lambda function log groups.
To instrument the function, import the datadog-cdk-construct module in your AWS CDK app and add the following configurations (this example is TypeScript, but usage in other languages is similar):
Replace <SERVICE> and <ENV> with your service and environment values.
If your Lambda function is configured to use code signing, you must add Datadog’s Signing Profile ARN (arn:aws:signer:us-east-1:464622532012:/signing-profiles/DatadogLambdaSigningProfile/9vMI9ZAGLc) to your function’s Code Signing Configuration before you can use the macro.
More information and additional parameters can be found in the Datadog CDK NPM page.
If you are deploying your Lambda function as a container image, you cannot use the Datadog Lambda Library as a layer. Instead, you must install the Datadog Lambda Library as a dependency of your function within the image. If you are using Datadog tracing, you must also install dd-trace.
NPM:
npm install --save datadog-lambda-js dd-trace
Yarn:
yarn add datadog-lambda-js dd-trace
Note: The minor version of the datadog-lambda-js package always matches the layer version. For example, datadog-lambda-js v0.5.0 matches the content of layer version 5.
Set your image’s CMD value to node_modules/datadog-lambda-js/dist/handler.handler. You can set this in AWS or directly in your Dockerfile. Note: The value set in AWS overrides the value in the Dockerfile if you set both.
Set the following environment variables in AWS:
Set DD_LAMBDA_HANDLER to your original handler, for example, myfunc.handler.
Set DD_TRACE_ENABLED to true.
Set DD_FLUSH_TO_LOG to true.
Optionally add service and env tags with appropriate values to your function.
The Datadog Lambda Library can be imported as a layer or JavaScript package.
The minor version of the datadog-lambda-js package always matches the layer version. For example, datadog-lambda-js v0.5.0 matches the content of layer version 5.
Configure the layers for your Lambda function using the ARN in the following format.
# For us,us3,us5,eu, and ap1 regions
arn:aws:lambda:<AWS_REGION>:464622532012:layer:Datadog-<RUNTIME>:<VERSION>
# For us-gov regions
arn:aws-us-gov:lambda:<AWS_REGION>:002406178527:layer:Datadog-<RUNTIME>:<VERSION>
The available RUNTIME options are: Node18-x, Node20-x, Node22-x. The latest VERSION is 121. For example:
If your Lambda function is configured to use code signing, you must add Datadog’s Signing Profile ARN (arn:aws:signer:us-east-1:464622532012:/signing-profiles/DatadogLambdaSigningProfile/9vMI9ZAGLc) to your function’s Code Signing Configuration before you can add the Datadog Lambda library as a layer.
Set your function’s handler to /opt/nodejs/node_modules/datadog-lambda-js/handler.handler if using the layer, or node_modules/datadog-lambda-js/dist/handler.handler if using the package.
Set the environment variable DD_LAMBDA_HANDLER to your original handler, for example, myfunc.handler.
Set the environment variable DD_TRACE_ENABLED to true.
Set the environment variable DD_FLUSH_TO_LOG to true.
Optionally add a service and env tag with appropriate values to your function.
Note: You need to follow these additional configuration steps if your Lambda function is simultaneously using Datadog’s tracing libraries and webpack.
Although it’s optional, Datadog recommends tagging you serverless applications with the env, service, and version tags following the unified service tagging documentation.
If you would like to submit a custom metric or span, see the sample code below:
const{sendDistributionMetric,sendDistributionMetricWithDate}=require("datadog-lambda-js");consttracer=require("dd-trace");// submit a custom span named "sleep"
constsleep=tracer.wrap("sleep",(ms)=>{returnnewPromise((resolve)=>setTimeout(resolve,ms));});exports.handler=async(event)=>{// add custom tags to the lambda function span,
// does NOT work when X-Ray tracing is enabled
constspan=tracer.scope().active();span.setTag('customer_id','123456');awaitsleep(100);// submit a custom span
constsandwich=tracer.trace('hello.world',()=>{console.log('Hello, World!');});// submit a custom metric
sendDistributionMetric("coffee_house.order_value",// metric name
12.45,// metric value
"product:latte",// tag
"order:online",// another tag
);// submit a custom metric with timestamp
sendDistributionMetricWithDate("coffee_house.order_value",// metric name
12.45,// metric value
newDate(Date.now()),// date, must be within last 20 mins
"product:latte",// tag
"order:online",// another tag
);constresponse={statusCode:200,body:JSON.stringify("Hello from serverless!"),};returnresponse;};
For more information on custom metric submission, see Serverless Custom Metrics. For additional details on custom instrumentation, see the Datadog APM documentation for custom instrumentation.