|
| 1 | +# lambda-layers |
| 2 | +To run Python functions in AWS Lambda with extra imported packages, we need to have these packages in Lambda Layers |
| 3 | +However making the layers locally uses your local environment's Python Runtime, OS and CPU Architecture. |
| 4 | +So the zip files we upload to the Lambda layers can be very hit or miss (mostly miss) |
| 5 | + |
| 6 | +This repo tries to solve that problem with the following steps, |
| 7 | +1. Create an Amazon Linux container |
| 8 | +2. Install the required packages with requirements.txt and pip3 |
| 9 | +3. Generate the zip file INSIDE the container |
| 10 | +4. Output the zip file to your local machine |
| 11 | + |
| 12 | +# Usage |
| 13 | +1. update requirements.txt with the packages you want in the layer |
| 14 | +- Use specific versions, if possible. eg: ```requests==2.31.0``` instead of ```requests``` |
| 15 | +- [Syntax for requirements.txt](https://pip.pypa.io/en/stable/reference/requirements-file-format/) |
| 16 | +- [Introduction to requirements.txt](https://www.freecodecamp.org/news/python-requirementstxt-explained/) |
| 17 | + |
| 18 | +2. run the runner.sh script and it will generate the python.zip file |
| 19 | +```./runner.sh``` |
| 20 | + |
| 21 | +3. upload this python.zip file to Lambda layers and use with, |
| 22 | +Python Runtime : Python 3.9 |
| 23 | +Architecture : x86_64 |
| 24 | + |
| 25 | +# Change Python Runtime or Architecture |
| 26 | +- If you wish to use a different Python runtime like 3.11, 3.12 etc, you can import a newer [Amazon Linux base image](https://hub.docker.com/_/amazonlinux) in the Dockerfile. |
| 27 | +This generally comes with a new stable version of Python |
| 28 | +- You can also install a [specific version of Python](https://www.python.org/downloads/) by updating the ```yum install``` lines of the Dockerfile |
| 29 | +- arm64 has known unresolved issues on Lambda with Python as of 20240322, so I recommend against it for now |
0 commit comments