Lambda Layers

AWS Lambda ships with some software packages pre-installed, depending on which language runtime you pick. But what if you need something else? Typically in a serverfull environment you would install the package (or add it to your Cloudformation/Terraform/etc scripts) and be done with it. But in Lambda your options are a bit more limited. You can‘t just install something and be done with it. You could run pip install at the beginning of your scripts, but do you want your users to have to wait for pip to run every time they visit your site? No. Luckily AWS gives us a better solution. It‘s called Lambda Layers.

I needed an extra dependency for a serverless meme maker I was working on, which required the Python package called Pillow. Interestingly enough, AWS lists Pillow in their documentation on creating a Lambda Layer. Simple enough… download the Python packages with pip download pillow and zip up the resulting folder. Do note that the package needs to have been compiled for the right target… if you‘re downloading on Mac or Windows you might not get the right package. I do almost all of my coding in AWS Cloud9, so I‘m running the right OS for a layer. You might need to do a bit more work.

One the package is zipped, you‘ll need to upload it to S3. You can do it with the AWS CLI but I just dragged the file over to a new S3 bucket. After that, inside the AWS console click on Lambda and then at the far left side click Layers. Add a new layer, giving it the S3 bucket you just created.

How do you use a layer?

Simple enough if you‘re using the Serverless Framework. I just added this to my serverless.yml

functions:
  meme:
    handler: handler.meme
    layers: arn:aws:lambda:us-east-2:512794413931:layer:pillow:1

and viola, I can now from PIL import image. The layer is attached as the /opt directory. There are some limitations: you can only have five layers attached, and they can‘t exceed 250MB in total. Not a problem for Pillow, which clocks in around 2.5MB. Layers are region-specifc, so make sure you‘re creating your layer in the same region as your Lambda function!

One Reply to “Lambda Layers”

  1. Hi. After adding the layer to serverless.yaml file I still get the error. What I did is, I removed pillow from requirements.txt, but still each time while trying to make deployment, I still see ‘PIL’ and ‘Pillow-9.3.0.dist-info’ directories are created in deployment zip files . Can you help please its not working?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.