Search code examples
python-3.xamazon-web-servicesselenium-webdriveraws-lambdaaws-lambda-layers

AWS Lambda deployment error/ Couldn't find module


So , I have a python -selenium script which needs to be deployed on AWS Lambda. It uses multiple external dependencies like selenium, beautiful-soup, fireabase-admin and some apis. So whenever i install this dependencies , multiple small other dependencies are also installed like grpcio, urllib3 etc. I have tried various approaches, including separating dependencies into layers, but the problem persists.

Solutions I have tried till now.
1. I zipped all my dependencies and my code files in a single zip file and uploaded to Lambda function . This started giving me dependency issues one by one and have tried downgrading/upgrading them successfully. But then it gave me a dependency issue which I wasn't able to solve.

2. Then i tried to divide the dependency in a lambda layer and my chromedriver(Also needed to run my script) in another layer . Hence creating 2 layers. I uploaded just me script files to Lambda. But, Lambda wasn't able to pick my dependencies in this way. It resulted in this error

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

Note: The Lambda function runs successfully on my local environment, but not on AWS Lambda.

I appreciate any guidance on how to resolve this issue and successfully deploy my Python script on AWS Lambda. Thank you!

Solutions I have tried till now.
1. Zipping all dependencies and code in a single file

2. Dividing dependencies into layers and adding layer to the function. The zip file structure for my dependency was ->

python.zip -> Lib > python3.8 > site-packages > Rest of dependencies.

I expected the function to run successfully as it does on my Windows local machine but the issue persists.


Solution

  • The issue lies with the lambda handler, which must be defined properly in the Lambda function.

    The handler should always be in the format "file_name.function_name."

    For example, if your Python file is named "lambda_function.py" and your function is called "lambda_handler," as I mentioned below:

    enter image description here

    then your handler needs to be like this enter image description here