I'm trying to use the serverless
framework to create and deploy an AWS Lambda 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?
Here are a few steps that should make it work:
s-function.json
has the function-name in its path:
"handler": "function-name/handler.handler",
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.
I followed below steps to deploy with dependencies:
Created a directory for dependencies in project root mkdir .vendor
Add dependencies in requirements.txt file manually or use pip freeze > requirements.txt
Update serverless.yml file
package:
include:
- .vendor/**
import sys
sys.path.insert(0, './.vendor')
pip install -r requirements.txt -t .vendor
Now serverless deploy
will upload function with dependencies.
I'd recommend using the serverless-python-requirements plugin to include packages installed via pip
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With