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 a layer, 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.
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 CloudFormation macro automatically transforms the CloudFormation template generated by the AWS CDK 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.
To instrument the function, add the DatadogServerless transform and the CfnMapping to your Stack object in your AWS CDK app. See the sample code below in Python (the usage in other language should be 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 macro documentation.
Replace the placeholder <AWS_REGION>, <RUNTIME> and <VERSION> in the layer ARN with appropriate values. The available RUNTIME options are Python38, Python39, Python310, Python311, Python312, Python313. The latest VERSION is 106. For example:
# For regular regions
arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Python313:106
# For us-gov regions
arn:aws-us-gov:lambda:us-gov-east-1:002406178527:layer:Datadog-Python313:106
If your Lambda function is configured to use code signing, add Datadog’s Signing Profile ARN (arn:aws:signer:us-east-1:464622532012:/signing-profiles/DatadogLambdaSigningProfile/9vMI9ZAGLc) to your function’s Code Signing Configuration.
If your Lambda function is configured to use code signing, add Datadog’s Signing Profile ARN (arn:aws:signer:us-east-1:464622532012:/signing-profiles/DatadogLambdaSigningProfile/9vMI9ZAGLc) to your function’s Code Signing Configuration.
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.
pip install datadog-lambda
Note that the minor version of the datadog-lambda package always matches the layer version. For example, datadog-lambda v0.5.0 matches the content of layer version 5.
Set your image’s CMD value to datadog_lambda.handler.handler. You can set this in AWS or directly in your Dockerfile. Note that 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.
You can either install the Datadog Lambda library as a layer (recommended) or Python package.
The minor version of the datadog-lambda package always matches the layer version. For example, datadog-lambda 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,ap1, and eu 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 Python38, Python39, Python310, Python311, Python312, Python313. The latest VERSION is 106. 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.
Install datadog-lambda and its dependencies locally to your function project folder. Note: datadog-lambda depends on ddtrace, which uses native extensions; therefore they must be installed and compiled in a Linux environment. For example, you can use dockerizePip for the Serverless Framework and –use-container for AWS SAM. For more details, see how to add dependencies to your function deployment package.
Although it’s optional, Datadog recommends tagging your 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:
importtimefromddtraceimporttracerfromdatadog_lambda.metricimportlambda_metricdeflambda_handler(event,context):# add custom tags to the lambda function span,# does NOT work when X-Ray tracing is enabledcurrent_span=tracer.current_span()ifcurrent_span:current_span.set_tag('customer.id','123456')# submit a custom spanwithtracer.trace("hello.world"):print('Hello, World!')# submit a custom metriclambda_metric(metric_name='coffee_house.order_value',value=12.45,timestamp=int(time.time()),# optional, must be within last 20 minstags=['product:latte','order:online'])return{'statusCode':200,'body':get_message()}# trace a function@tracer.wrap()defget_message():return'Hello from serverless!'
For more information on custom metric submission, see here. For additional details on custom instrumentation, see the Datadog APM documentation for custom instrumentation.