AWS python lambda datadog integration

I have a lambda written in python and I want to submit some custom metrics from the lambda to datadog

I followed this documentation, (https://www.datadoghq.com/blog/datadog-lambda-layer/) Added datadog dependencies as a lambda layer and added it to my lambda as a layer. When I test the lambda I receive the below error,

Response
{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'datadog_lambda'",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

Function Logs
START RequestId: 63addc4a-389b-4526-865e-b44bc272f1ab Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'datadog_lambda'

Can anyone help me with figuring out the issue here.

Upvotes: 2

Views: 2573

Answers (3)

adamsxs
adamsxs

Reputation: 86

Given that we don't have a dockerfile, my best guess is that you followed the instructions (https://docs.datadoghq.com/serverless/aws_lambda/installation/python/?tab=containerimage) and then didn't correctly set the DD_LAMBDA_HANDLER environment variable that tells Datadog where to redirect the request. If you don't, it won't be able to find your function an you'll get an import error just like the one you desribed.

Please add a dockerfile next time! It'll be much easier to help you get a solution.

Upvotes: 0

shifu.zheng
shifu.zheng

Reputation: 711

We need to use lambda-datadog Terraform module wraps the aws_lambda_function resource and automatically configures your Lambda function for Datadog Serverless Monitoring by:

  • Adding the Datadog Lambda layers
  • Redirecting the Lambda handler
  • Enabling the collection and sending of metrics, traces, and logs to Datadog

For details, please refer to: https://docs.datadoghq.com/serverless/aws_lambda/installation/python/?tab=terraform

Upvotes: 0

Jyothish
Jyothish

Reputation: 1133

I assume that your lambda python script name is datadog_lambda.py. Then you need to change your lambda function handler name. Default handler name in lambda console is "lambda_function.lambda_handler". lambda_function is the default file name in Lambda. So change the handler to datadog_lambda.lambda_handler.

You can change the handler name from the Runtime settings pane.

From AWS documentation

A function handler can be any name; however, the default name in the Lambda 
console is lambda_function.lambda_handler. This function handler name reflects 
the function name (lambda_handler) and the file where the handler code is 
stored (lambda_function.py).

enter image description here

Upvotes: -4

Related Questions