2

I want to use matplotlib to create image files in an AWS Lambda function. Since my system does not run the same version of Linux that Lambda uses, I received the guidance to download a wheel (.whl file) from the matplotlib website. I downloaded matplotlib-3.3.3-cp36-cp36m-manylinux1_x86_64.whl https://files.pythonhosted.org/packages/d2/43/2bd63467490036697e7be71444fafc7b236923d614d4521979a200c6b559/matplotlib-3.3.3-cp36-cp36m-manylinux1_x86_64.whl

to the folder where I am creating my deployment package.

First of all, I there are many wheel files and it is not clear if this is the correct one to use. Second, it doesn't work.

THen I upacked the wheel with the command

wheel unpack *.whl

This produced a directory with the matplotlib package in it.

My main function is

import matplotlib.pyplot

def lambda_handler(event, context):
return

I gave the commands

zip -r my-deployment-package.zip .
zip -g my-deployment-package.zip lambda_function.py

and uploaded the zip to an s3 bucket. Then I used the "aws lambda update-function-code..." command to update the lambda function. When I deploy and test, I get the error: "Unable to import module 'lambda_function': No module named 'matplotlib'"

Thinking that the matplotlib module is not there (perhaps I need to do something with the whl file or put it in a different place), i activated my virtual environment and started the python interpreter. Trying to import matplotlib, I get "module not found." Trying to use pip install on the matplotlib directory, it complains that there is no setup.py:

$ pip install --no-index --find-links=. matplotlib-3.3.3/
ERROR: Directory 'matplotlib-3.3.3/' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.

How can I get the module to be recognized?

Anna Naden
  • 91
  • 1
  • 9

2 Answers2

1

With the help of Serverless framework and Docker, you can build a layer in a minute. Check out my answer in this post.

If you are in a hurry, feel free to grab this lambda layer that I packaged for you. It's compatible with Python 3.8. Here is the gdrive download link.

jellycsc
  • 10,904
  • 2
  • 15
  • 32
  • Read your post, didn;t understand it much. Don;t know anything about Lambda layers. Is Serverless an AWS thing, an application of Docker or what? I will Google. My attempts with matplotlib were intended as a proof of concept, There will be other dependencies, for example Pandas – Anna Naden Nov 18 '20 at 05:32
  • I see that Lamda has a size limit of 250 MB, unzipped. For matplotlib, that is 180 MB. Since I need Pandas too, I think this quota limit kills my idea – Anna Naden Nov 18 '20 at 05:41
  • An alternative is to put your library into EFS, and let lambda import packages from there. – jellycsc Nov 18 '20 at 16:27
0

No need to build your own layer; loads of those available at https://github.com/keithrozario/Klayers - you just have to include them in your Lambda config.

ivica
  • 1,388
  • 1
  • 22
  • 38