Skip to content Skip to sidebar Skip to footer

How Do I Deploy A Function In Python With Its Dependencies?

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 instal

Solution 1:

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.


Solution 2:

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/**

  • Include .vendor directory to system path in handler.py file

import sys sys.path.insert(0, './.vendor')

  • Install dependencies pip install -r requirements.txt -t .vendor

Now serverless deploy will upload function with dependencies.


Solution 3:

I'd recommend using the serverless-python-requirements plugin to include packages installed via pip


Post a Comment for "How Do I Deploy A Function In Python With Its Dependencies?"