Search code examples
pythonaws-lambdazipaws-codebuild

AWS Python Lambda Unable to Import Module In zip File constructed on CodeBuild


I am trying to deploy a python lambda function with a zip file archive.

Using AWS CodeBuild I followed the instructions to setup a zip file with my source code and dependencies at the top level.

However, when I invoke my Lambda function an import error is reported:

{
  "errorMessage": "Unable to import module 'my_module': No module named 'cytoolz.itertoolz'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "<random uuid>",
  "stackTrace": []
}

Why is Lambda unable to find a dependency that exists within a zip file that is built using CodeBuild?


Solution

  • The problem is with python versions. The Standard 6.0 CodeBuild environment has python 3.10 installed but the python runtime environment in Lambda only goes up to 3.9 (at the time of this answer).

    Therefore, when installing the dependencies into the zip file

    pip install --target ./package requests
    
    cd package 
    zip -r ../my-deployment-package.zip . 
    

    The version of pip is 3.10 and all of the dependencies that are installed and packaged in the zip file are the wrong python version.

    In CodeBuild install a python & pip 3.9 to create the zip file.