title: AWS Lambda (python2.7): Adding matplotlib as a layer
body: <p>I am having trouble trying to add <code>matplotlib</code> as a <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html" rel="nofollow noreferrer">layer</a> to my Python 2.7 AWS Lambda function. </p>
  
  <p>On the <a href="https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html" rel="nofollow noreferrer">Lambda execution environment</a>, I am trying to install the necessary libraries and create a layer as described <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Things I've tried:</p>
  
  <p>First, I <code>pip install</code>ed matplotlib into a virtual environment and copied the contents of the site-packages under lib and lib64. When the lambda function is executed, I get a <code>No module named pkg_resources</code> exception. I also tried installing with the <code>--target</code> option to install all dependancies to the same folder. The result was the same. </p>
  
  <p>I read <a href="https://stackoverflow.com/a/10538412/287923">here</a> that it may be due to outdated setuptools package. When I did an update <code>pip install --upgrade setuptools</code> and then tried to install matplotlib I started getting the following exception:</p>
  
  <pre><code>pkg_resources.DistributionNotFound: The 'pip==9.0.3' distribution was not found and is required by the application 
  </code></pre>
  
  <p>Finally I thought of installing matplotlib with </p>
  
  <pre><code>sudo yum install python-matplotlib
  </code></pre>
  
  <p>and then collect the required packages as described <a href="https://aws.amazon.com/premiumsupport/knowledge-center/lambda-linux-binary-package/" rel="nofollow noreferrer">here</a>. But this did not make matplotlib importable from within the python shell, so I guess it won't work as a Lambda layer. </p>
  
  <p>Thanks for any help. </p>
  
  <p>P.S: At AWS re:invent, exactly this was <a href="https://www.slideshare.net/AmazonWebServices/reinvent-deep-dive-on-lambda-layers-and-runtime-api" rel="nofollow noreferrer">demoed</a> but there are no details on the session :/</p>