0

I am attempting to add an AWS Lambda layer to be able to use matplotlib in AWS Lambda. To do this, I downloaded the following file from Python Package Index:

matplotlib-3.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

I ensured this was the correct version of python and the correct operating system for my machine.

Next, I extracted the files into a folder on my machine called 'python' then created a zip folder with the same name (python). I uploaded this zip folder to my AWS Lambda Layer for matplotlib.

After successfully creating a lambda layer, I added this layer to my lambda function.

Despite this, I still get the following error when trying to execute a function with matplotlib imported:

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'matplotlib'",
  "errorType": "Runtime.ImportModuleError",

I tried redownloading the python package and re-creating the lambda layer multiple times, but I arrived at the same issue each time. Any help would be greatly appreciated.

c0nv3xity
  • 15
  • 3
  • Can you share the structure of your lambda function components and its tree structure? How are you provisioning the lambda and the layers? do you use IAC tools? – Allan Chua Jun 25 '23 at 08:22
  • Instead of using Lambda Layers have you considered packaging your code as an oci image and deploying that way? I’ve found it way easier to troubleshoot issues like this. Here is a recent example https://stackoverflow.com/questions/76516017/how-to-use-tabula-on-aws-lambda-to-read-pdf/76527077#76527077 – Ricardo Sueiras Jun 25 '23 at 08:53

1 Answers1

1

The following worked for me in order to create a layer for matplotlib.

In my local machine I executed the next commands:

mkdir python
cd python
pip install \
--platform manylinux2014_x86_64 \
--target=./ \
--implementation cp \
--python-version 3.8 \
--only-binary=:all: --upgrade \
matplotlib

I'm using python 3.8, change for your needed version.

Then I zipped the python folder and uploaded the python.zip file to a s3 bucket and proceeded to create the layer.

I understand that just extracting a wheel's files will not have all the necessary files/dependencies, with the above command pip will download besides the principal package (matplotlib) all its dependencies.

Ale Sosa
  • 93
  • 1
  • 8
  • thank you! Did you run the above code in your terminal? What type of machine are you running? – c0nv3xity Jun 26 '23 at 06:23
  • Yes I did, I have a macbook m1 (mac os, arm architecture). The code should work for windows and linux too. Regardless of the machine that is running, the code will download the matplotlib library for linux x86 platform as the parameter —platform is “manylinux2014_x86_64” – Ale Sosa Jun 26 '23 at 12:15
  • I'll give it a try and let you know if it works out. Thank you for the suggestion! – c0nv3xity Jun 26 '23 at 21:39
  • for some reason, this code is not running in my command prompt. Any suggestions? – c0nv3xity Jun 27 '23 at 02:42
  • What is the output message after running the commands? – Ale Sosa Jun 27 '23 at 03:49
  • '''ERROR: Directory '\\' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.''' – c0nv3xity Jun 27 '23 at 04:15
  • It seems that there is a problem with the path or the directory is not writable. Are you using Windows OS? Is so, can you try modifying the path to “.\”, so the command would look like: pip install --platform manylinux2014_x86_64 --target .\ --implementation cp --python-version 3.8 --only-binary=:all: --upgrade matplotlib – Ale Sosa Jun 27 '23 at 16:32
  • Yes, I am using Windows OS. Let me try the command you posted in your comment above, and thank you for your help. – c0nv3xity Jun 27 '23 at 21:11
  • I was successfully able to import matplotlib to lambda and create a pie chart, but do you have any advice on how to fix this? [WARNING] 2023-06-27T21:37:24.262Z Matplotlib created a temporary config/cache directory at /tmp/matplotlib-2vh8dz_q because the default path (/home/sbx_user1051/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing. START RequestId: c9a29bfe-9f4b-454d-b194-ccc506d8dc00 Version: $LATEST – c0nv3xity Jun 27 '23 at 21:41
  • also thank you for your help! – c0nv3xity Jun 27 '23 at 21:42
  • I did some research an apparently by default, Matplotlib tries to create a config/cache directory in "/home/sbx_user1051/.config/matplotlib", and this directory is not writable. Try the following, open your Lambda function in the Console. Go to the "Configuration" tab. Then "Environment variables" . Add a new environment variable as follow: Key: MPLCONFIGDIR Value: /tmp/matplotlib By setting the MPLCONFIGDIR to "/tmp/matplotlib", you are specifying a writable directory within the Lambda environment where Matplotlib can create its temporary configuration and cache files. – Ale Sosa Jun 27 '23 at 22:41
  • After setting the environment variable, redeploy your Lambda function and the warning message should no longer appear. – Ale Sosa Jun 27 '23 at 22:52
  • finally! thank you very much :) – Nadav96 Jul 30 '23 at 11:40