Unable to import module "lambda_function" no module named "lambda_function" | AWS Cognito | Lambda Function


Unable to import module ’lambda_function’ no module named ’lambda_function’ while testing AWS Lambda function from Docker image locally.

I had the following folder structure for Lambda functions,

node
  preAuthenticationTrigger
    index.js
  Dockerfile
python
  generateFunc
    lambda_function.py
  Dockerfile

After building the docker image for Python lambda functions, I was trying to test the lambda functions locally using the following command:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d {}

But it was throwing the unable to import lambda module error.

The issue was with the Dockfile CMD which was pointing to the file to be invoked in a wrong way. Here is what worked for me,

FROM public.ecr.aws/lambda/python:3.8
COPY . ${LAMBDA_TASK_ROOT}
CMD ["generateFunc/lambda_function.lambda_handler"]