Skip to content

Commit 703b57d

Browse files
committed
Update README
1 parent 4688362 commit 703b57d

1 file changed

Lines changed: 46 additions & 25 deletions

File tree

README.md

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
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)
1+
# Lambda Layers for Python Runtimes
2+
### Problem
3+
To import packages in AWS Lambda Functions, they are required as Lambda Layers
4+
5+
These layers need to match the language runtime/version, architecture, lambda operating system, etc. Creating these in local environments can cause unforeseen inconsistencies and potential compatibility issues
6+
 
7+
8+
# Solution
9+
This repo tries to solve the problem by standardising the environment using a container. The packages are baked into layers inside this standard container. A .zip artifact is generated, which can be easily uploaded to Lambda Layers
510

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
1111

1212
# 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-
- You can also install a [specific version of Python](https://www.python.org/downloads/) by updating the ```yum install``` lines of the Dockerfile
28-
- arm64 has known unresolved issues on Lambda with Python as of 20240322, so I recommend against it for now
13+
1. Update `requirements.txt` with the packages you want in the layer
14+
[Syntax options for requirements.txt](https://pip.pypa.io/en/stable/reference/requirements-file-format/)
15+
2. Make sure podman or docker daemon is running
16+
3. Execute `runner.sh` script and it will generate the `python_layer.zip` artifact
17+
4. Upload `python_layer.zip` to Lambda Layers and use it in your Python Lambda function(s)
18+
19+
20+
21+
### Following is done automatically by `runner.sh`
22+
1. Create an Amazon Linux container
23+
2. Install the required packages written in requirements.txt with pip
24+
3. Generate the zip file INSIDE the container so as to match the Lambda environment
25+
4. Output the zip file to your local machine which you can upload to Lambda Layers
26+
5. Delete the Amazon Linux container and image to free up space (can be disabled in `runner.sh` file)
27+
28+
 
29+
30+
# Supported Defaults
31+
> [!NOTE]
32+
> Python Runtime: Python 3.14
33+
> Lambda Operating System: Amazon Linux 2023
34+
> Architecture: x86_64
35+
> Container Runtimes: Podman, Docker
36+
37+
# Support for other Python versions
38+
If you wish to build layers for Python runtimes/versions other than 3.14,
39+
1. [Note the operating system necessary for your python runtime.](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html#runtimes-supported)
40+
This repo uses Amazon Linux 2023 by default. But if your runtime/version needs a different operating system, you can find the specific image tags on [amazonlinux dockerhub](https://hub.docker.com/_/amazonlinux). Use that tag in the `Dockerfile`
41+
42+
2. Update the `PYTHON_VERSION` argument in the `Dockerfile`
43+
3. Execute `runner.sh`
44+
45+
46+
# Future Considerations
47+
- Support for arm64 architecture
48+
- Support runtimes for other languages
49+
- Support other popular container runtimes

0 commit comments

Comments
 (0)