How do I deploy a function in python with its dependencies?
Question: I'm trying to use the `serverless` framework to create and deploy an AWS Lamba
function. I created a folder named `vendored` in the root of the project and
installed (using `pip install`) the function dependencies. However, whenever I
try to run it (using `serverless function run`) I got an error:

> Serverless: Running isNewUser...  
>  Serverless: WARNING: This variable is not defined: region  
>  Serverless: -----------------  
>  Serverless: Failed - This Error Was Returned:  
>  Serverless: {"exception": ["Traceback (most recent call last):\n", " File
> \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-
> handler\", line 170, in \n handler = import_program_as_module(path)\n", "
> File \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-
> handler\", line 149, in import_program_as_module\n module =
> make_module_from_file('lambda_handler', handler_file)\n", " File
> \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-
> handler\", line 129, in make_module_from_file\n py_source_description\n", "
> File \"/home/fernando/workspace/os-cac/isNewUser/handler.py\", line 11, in
> \n from vtex.order import Order\n", "ImportError: No module named
> vtex.order\n"], "success": false} Serverless: Exception message from Python  
>  Serverless: Traceback (most recent call last): , File
> "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-
> handler", line 170, in handler = import_program_as_module(path) , File
> "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-
> handler", line 149, in import_program_as_module module =
> make_module_from_file('lambda_handler', handler_file) , File
> "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-
> handler", line 129, in make_module_from_file py_source_description , File
> "/home/fernando/workspace/os-cac/isNewUser/handler.py", line 11, in from
> vtex.order import Order ,ImportError: No module named vtex.order `

`vtex.order` is a module imported in handler.py

The structure of my project is something like:

    
    
    /root/
         |
         |--_meta/
         |--vendored/
                    |--dependencies...
         |--function-name/
                         |--handler.py
                         |--event.json
                         |--s-function.json
         |--requirements.txt
         |--admin.env
         |--package.json
         |--s-project.json
         |--s-resources-cf.json
         |--s-project.json
    

Is there anything I'm doing wrong? Should I somehow configure my function to
include the dependencies from vendored?


Answer: Here are a few steps that should make it work:

  1. Make sure that the handler entry in `s-function.json` has the function-name in its path: `"handler": "function-name/handler.handler",`
  2. in `handler.py` add the following:
    
        import os
    import sys
    
    here = os.path.dirname(os.path.realpath(__file__))
    sys.path.append(os.path.join(here, "../vendored"))
    
    from vtex.order import Order
    

That's it. Let me know if it worked.

