Building lambda python layer

August 28, 2024

Python layer for lambda

The Why?

I do not know if you have faced this issue, but I did recently when I tried to get OpenAI to work with AWS Lambda. I had of course asked chatGPT to guide me, but whatever I build on my MAC does not work on AWS Lambda.

After spending few hours on it, I thought someone might have faced this issue and there might be a blog post or a repo to follow, but I could not find any.

So I decided to write my own blog post and create a repo with a detailed explanation.

The What?

So now I want to build a lambda layer that I can use in my lambda function. Now first thing I should have done is to read AWS blog post on how to create a lambda layer, but lazy I'm, I skimmed through the docs and found this blog post which I thought is what I need.

The below is snippet from the blog post I should have read first.

In rare cases, a Python package might not be available as a wheel. If only the source distribution (sdist) exists, then we recommend installing and packaging your dependencies in a Docker environment based on the Amazon Linux 2023 base container image. 

After carefully reading the post, I've created a lambda layer with the dependencies that I need in my lambda function. Better yet, I've created a repo with a detailed explanation here.

All you need to do is to run the below command to create a layer.

ksh export_lambda_layer.sh

We will be using docker container to build the layer , use app/requirements.txt to install the libraries you need in the layer.

Happy Layering :)