Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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?

1 Answer

0 votes
by (44.4k points)

Use these steps, it might work:

  1. The handler entry in s-function.json should have the function-name in its path like this: "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

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...