Skip to content

Commit fd910e4

Browse files
authored
First Commit
1 parent a02b53f commit fd910e4

8 files changed

Lines changed: 66 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.zip

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# amazonlinux:2023 has python 3.9 + venv
2+
FROM amazonlinux:2023
3+
4+
# We will need pip3 and zip
5+
RUN yum -y update && yum -y install \
6+
python3-pip \
7+
zip

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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

docker_install.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
python3 -m venv python
2+
source python/bin/activate
3+
4+
pip3 install -r requirements.txt -t python/lib/python3.9/site-packages
5+
6+
zip -r9 python.zip python

python.zip

9.94 MB
Binary file not shown.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

runner.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
container_name=layer_builder_docker
2+
docker_image=layer_builder_image
3+
4+
docker build -t $docker_image .
5+
docker run -td --name=$container_name $docker_image
6+
docker cp ./requirements.txt $container_name:/
7+
8+
docker exec -i $container_name /bin/bash < ./docker_install.sh
9+
docker cp $container_name:/python.zip python.zip
10+
docker stop $container_name
11+
docker rm $container_name

runner_podman.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
container_name=layer_builder_podman
2+
podman_image=layer_builder_image
3+
4+
podman build -t $podman_image .
5+
podman run -td --name=$container_name $podman_image
6+
podman cp ./requirements.txt $container_name:/
7+
8+
podman exec -i $container_name /bin/bash < ./docker_install.sh
9+
podman cp $container_name:/python.zip python.zip
10+
podman stop $container_name
11+
podman rm $container_name

0 commit comments

Comments
 (0)