DataDog / datadog-lambda-python

The Datadog AWS Lambda Layer for Python
https://docs.datadoghq.com/integrations/amazon_lambda/#installing-and-using-the-datadog-layer
Apache License 2.0
81 stars 43 forks source link

Unable to import module 'lambda_function': No module named 'ddtrace.internal._encoding' #344

Closed abusutil closed 1 year ago

abusutil commented 1 year ago

Expected Behavior

Lambda executions logs sent to Datadog. And Lambda run without issue.

Actual Behavior

Lambda show only this error (in Datadog) : Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'ddtrace.internal._encoding' And it's blocking the execution.

Steps to Reproduce the Problem

I'm build the lambda with some different layers and the lambda working good with CloudWatch.

requirements.txt

boto3 >=1.26
PyMySQL >=1.1
aws_secretsmanager_caching >=1.1.1
datadog-lambda >=4.74

Building .zip archives for layers

#!/bin/bash
while IFS="" read -r p || [ -n "$p" ] 
do
cutted=$(echo $p | cut -d ' ' -f 1)
pip3 install "$p" --target $HOME/project/tmp/layer-"$cutted"/python/lib/python3.10/site-packages
cd $HOME/project/tmp/layer-"$cutted" && zip -r "$HOME/project/tmp/layer-$cutted.zip" ./*
done < $HOME/project/src/py_modules/requirements.txt

I want to send logs directly from Datadog, so, I fellow this page : https://docs.datadoghq.com/serverless/installation/python/?tab=custom#using-the-package (Option B)

I added those lines into my function_handler.py : https://docs.datadoghq.com/serverless/guide/handler_wrapper/

Specifications

I don't understand why I have this error. I saw that this method need to compile ddtrace with the correct architecture, and I think, it's done by enter architecture value in my TF. I missed something ?

Thanks for your advices.

Best,

purple4reina commented 1 year ago

Assuming you're working from an arm64 computer, you are calling pip3 install locally before zipping. You will need to tell pip to use amd64. This can be done in docker using --platform=linux/amd64 or using the --platform option on pip install directly.

abusutil commented 1 year ago

Thanks for your answer.

So, the issue was not my build agent architecture, but the python version used to build the plugins.

I'm using CircleCI to do that, and the CircleCI executor using python3.8 and my Lambda is with python3.10. So I changed the executor to use an image with python3.10 and working good.

Thanks again :)