Search code examples
pythonamazon-web-servicesaws-lambdavirtualenvcvxpy

Unable to install cvxpy into virtualenv for AWS lambda


I am trying to run the cvxpy package in an AWS lambda function. This package isn't in the SDK, so I've read that I'll have to compile the dependencies into a zip, and then upload the zip into the lambda function.

I've done some research and tried out the links below, but when I try to pip install cvxpy I get error messages - I'm on a Windows box, but I know that AWS Lambda runs on Linux.

Appreciate the help!

http://i-systems.github.io/HSE545/machine%20learning%20all/cvxpy_install/CVXPY%2BInstallation%2BGuide%2Bfor%2BWindows.html

https://programwithus.com/learn-to-code/Pip-and-virtualenv-on-Windows/

https://medium.com/@manivannan_data/import-custom-python-packages-on-aws-lambda-function-5fbac36b40f8

https://www.cvxpy.org/install/index.html


Solution

  • For installing cvxpy on windows it requires c++ build tools (please refer: https://buildmedia.readthedocs.org/media/pdf/cvxpy/latest/cvxpy.pdf)

    On Windows:

    pip install cvxpy --target python/lib/python3.7/site-packages
    

    On Linux:

    • I created the same directory structure as earlier python/lib/python3.7/site-packages and installed the cvxpy and zipped it as shown below.
    • Later I uploaded the zip file to an S3 bucket and created a new lambda layer.
    • Attaching that lambda layer to my lambda function, I colud able to resolve the import issues failing earlier and run the basic cvxpy program on lambda.
    mkdir -p alley/python/lib/python3.7/site-packages
    pip install cvxpy --target alley/python/lib/python3.7/site-packages
    cd alley
    zip -rqvT cvxpy_layer.zip .
    

    Lambda layer Image:

    enter image description here

    Lambda function execution:

    enter image description here