Search code examples
pythonamazon-web-servicesaws-lambdapackageserverless-framework

Build and use local package for AWS Lambda using serverless framework


I am trying to package a local python package¹ and use it within an AWS lambda deployed via the Serverless framework. I already use serverless-python-requirements plugin to add pip dependencies to deployed package.

How can I proceed ?

Shall I create a package and zip it? Or generate a whl file and use pip? And then, how to deploy it?


¹: I cannot just add it to "normal codebase" as I want to share it with other bricks (Glue jobs for example)


Solution

  • Here's the solution:

    1.

    Build a .whl file corresponding to package using

    python setup.py bdist_wheel
    

    within a parent directory.

    2.

    Add the relative path to this .whl file to used pip requirement file (requirements.txt for instance) :

    req0==1.0.9
    req1==5.5.0
    ../<relative path to local package>/dist/<package name>-<version>-<details>.whl # generated .whl file's name
    
    3.

    serverless-python-requirements will automagically pack this dependency within the deployed archive when doing sls deploy. How cool is that, huh!