Lambda Layers in Windows - The Easy Way
This isn't necessarily the best way.. its more like the Temu way.
Intro
I’ll start by saying this.. This isn’t necessarily the right way to do things. This is a way I found that helped get around some environment restrictions I had run into. I was having a difficult time deploying a lambda layer in AWS to use a Markdown library in Python. I could make the layer in my personal account, but I couldn’t get it deployed in the environment I was working in.
This guide will teach you how to package a lambda function in a way that all used libraries are self-contained in the lambda. You may not be able to edit the lambda in the AWS user interface with this method.
Core
To dive right in we will need to have a few things setup. We need to have a folder to use as our self-contained lambda, a matching python version to our lambda runtime (Python 3.7, Python 3.10, etc.), and a virtual environment tool. A lambda can be set to a variety of runtimes and it is important to ensure your current environment is using the matching version of your runtime. Technically, it is possible for a mismatch and it to still work, as long as all associated libraries are compatible.
Your folder structure will look like this at the end:
lambda_funcion.zip
|-- lambda_function.py
|-- <package folders>
There is not an additional subfolder that houses the lambda_function.py and libraries. A subfolder like `python` is not needed for this method. This is different than if you use lambda layers.
Start by organizing your project and environment into a single folder. Once we've created everything, we will move the contents of `site-packages` to the below location, along with the actual lambda function.
<your project name>
|-- <subfolder for virtual env here>
|-- <subfolder for lambda function here>
|-- lambda_function.py
|-- <site-packages content here>
Virtual Environment
Confirm your python version matches the desired runtime:
python --version
Set up your virtual environment in the folder mentioned:
python -m venv <location of virtual env folder>
Switch to your virtual environment:
<subfolder for virtual environment>\Scripts\activate
You’ll know if you are in the virtual environment if your PowerShell or terminal looks similar to this:
(your virtual environment)$
Proceed to install all libraries:
(your virtual environment)$ pip install <library name>
Repeat as needed.
Deactivate your environment.
(your virtual environment)$ deactivate
Move Site-Packages contents
Once finished, copy the contents of the site-packages folder from the virtual environment to the path mentioned earlier... This can be done using commands in PowerShell or Windows Explorer. The below command, cp, is just an alias for Copy-Item in PowerShell.
cp -Path .\<subfolder for virtual environment>\Lib\site-packages\ -Recurse -Destination .\<subfolder for lambda>\
Once all of your files and folders are ready and you can zip it up. Zip it from within the subfolder, otherwise it will zip everything inside a folder within the .zip, and we do not want that. Just a reminder that your .zip should now look like this:
lambda_funcion.zip
|-- lambda_function.py
|-- <package folders>
Conclusion
The zip can just be used to replace or make a new lambda. Configure your test case or start using it! Personally, I recommend only using the User Interface for development testing. When you are ready to productionize, make sure you are taking advantage of a code pipeline. All of what has been covered can be automated through automated pipelines like DevOps Pipelines, Gitea Actions, or Git Actions. Happy coding!