pythonamazon-web-servicesserverlessserverless-framework

Is there a way to define a requirements file for each lambda function in a serverless.yml in an AWS-Python environment?


We can do it at entire project level like: `

custom:
  pkgfunc:
    buildDir: _build
    requirementsFile: "requirements.txt"

but I needed to do something like:

functions:
  my_func:
    name: my_func
    handler: package.handler
    requirementsFile: "my_func_requirements.txt"

I wanted to check if it's possible and if yes, then, how good of a solution this is. My Primary objective is to reduce the lambda size as much as possible. Whichever lambda does not need a library should not get it packaged.

To reduce the stack size, I've separated some of the functions requiring huge libraries into different lambdas and I'm calling them internally via API/Boto calls.

Help is much appreciated.


Solution

  • You can use the plugin serverless-python-requirements:

    https://www.serverless.com/plugins/serverless-python-requirements

    Split the code into modules as required and you can then control the custom requirements for each function