2023/10/2

Creating Python Lambda layer

AWSTech

How to create

Run bellow command.

1mkdir layer
2cp requirements.txt layer/requirements.txt
3docker run -ti -v $(pwd)/layer:/app -w /app --entrypoint /bin/bash public.ecr.aws/lambda/python:3.11 -c "pip3 install --target ./python -r requirements.txt"
1mkdir layer
2cp requirements.txt layer/requirements.txt
3docker run -ti -v $(pwd)/layer:/app -w /app --entrypoint /bin/bash public.ecr.aws/lambda/python:3.11 -c "pip3 install --target ./python -r requirements.txt"

Bellow is result shown.

1Collecting httpx==0.25.0 (from -r requirements.txt (line 1))
2 Obtaining dependency information for httpx==0.25.0 from https://files.pythonhosted.org/packages/33/0d/d9ce469af019741c8999711d36b270ff992ceb1a0293f73f9f34fdf131e9/httpx-0.25.0-py3-none-any.whl.metadata
3 Downloading httpx-0.25.0-py3-none-any.whl.metadata (7.6 kB)
4Collecting certifi (from httpx==0.25.0->-r requirements.txt (line 1))
5 Obtaining dependency information for certifi from https://files.pythonhosted.org/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl.metadata
6 Downloading certifi-2023.7.22-py3-none-any.whl.metadata (2.2 kB)
7Collecting httpcore<0.19.0,>=0.18.0 (from httpx==0.25.0->-r requirements.txt (line 1))
8 Obtaining dependency information for httpcore<0.19.0,>=0.18.0 from https://files.pythonhosted.org/packages/ac/97/724afbb7925339f6214bf1fdb5714d1a462690466832bf8fb3fd497649f1/httpcore-0.18.0-py3-none-any.whl.metadata
9 Downloading httpcore-0.18.0-py3-none-any.whl.metadata (18 kB)
10Collecting idna (from httpx==0.25.0->-r requirements.txt (line 1))
11 Downloading idna-3.4-py3-none-any.whl (61 kB)
12 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 2.9 MB/s eta 0:00:00
13Collecting sniffio (from httpx==0.25.0->-r requirements.txt (line 1))
14 Downloading sniffio-1.3.0-py3-none-any.whl (10 kB)
15Collecting anyio<5.0,>=3.0 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
16 Obtaining dependency information for anyio<5.0,>=3.0 from https://files.pythonhosted.org/packages/36/55/ad4de788d84a630656ece71059665e01ca793c04294c463fd84132f40fe6/anyio-4.0.0-py3-none-any.whl.metadata
17 Downloading anyio-4.0.0-py3-none-any.whl.metadata (4.5 kB)
18Collecting h11<0.15,>=0.13 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
19 Downloading h11-0.14.0-py3-none-any.whl (58 kB)
20 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 10.2 MB/s eta 0:00:00
21Downloading httpx-0.25.0-py3-none-any.whl (75 kB)
22 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.7/75.7 kB 7.7 MB/s eta 0:00:00
23Downloading httpcore-0.18.0-py3-none-any.whl (76 kB)
24 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.0/76.0 kB 8.0 MB/s eta 0:00:00
25Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)
26 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 158.3/158.3 kB 6.6 MB/s eta 0:00:00
27Downloading anyio-4.0.0-py3-none-any.whl (83 kB)
28 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 83.1/83.1 kB 9.8 MB/s eta 0:00:00
29Installing collected packages: sniffio, idna, h11, certifi, anyio, httpcore, httpx
30Successfully installed anyio-4.0.0 certifi-2023.7.22 h11-0.14.0 httpcore-0.18.0 httpx-0.25.0 idna-3.4 sniffio-1.3.0
1Collecting httpx==0.25.0 (from -r requirements.txt (line 1))
2 Obtaining dependency information for httpx==0.25.0 from https://files.pythonhosted.org/packages/33/0d/d9ce469af019741c8999711d36b270ff992ceb1a0293f73f9f34fdf131e9/httpx-0.25.0-py3-none-any.whl.metadata
3 Downloading httpx-0.25.0-py3-none-any.whl.metadata (7.6 kB)
4Collecting certifi (from httpx==0.25.0->-r requirements.txt (line 1))
5 Obtaining dependency information for certifi from https://files.pythonhosted.org/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl.metadata
6 Downloading certifi-2023.7.22-py3-none-any.whl.metadata (2.2 kB)
7Collecting httpcore<0.19.0,>=0.18.0 (from httpx==0.25.0->-r requirements.txt (line 1))
8 Obtaining dependency information for httpcore<0.19.0,>=0.18.0 from https://files.pythonhosted.org/packages/ac/97/724afbb7925339f6214bf1fdb5714d1a462690466832bf8fb3fd497649f1/httpcore-0.18.0-py3-none-any.whl.metadata
9 Downloading httpcore-0.18.0-py3-none-any.whl.metadata (18 kB)
10Collecting idna (from httpx==0.25.0->-r requirements.txt (line 1))
11 Downloading idna-3.4-py3-none-any.whl (61 kB)
12 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 2.9 MB/s eta 0:00:00
13Collecting sniffio (from httpx==0.25.0->-r requirements.txt (line 1))
14 Downloading sniffio-1.3.0-py3-none-any.whl (10 kB)
15Collecting anyio<5.0,>=3.0 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
16 Obtaining dependency information for anyio<5.0,>=3.0 from https://files.pythonhosted.org/packages/36/55/ad4de788d84a630656ece71059665e01ca793c04294c463fd84132f40fe6/anyio-4.0.0-py3-none-any.whl.metadata
17 Downloading anyio-4.0.0-py3-none-any.whl.metadata (4.5 kB)
18Collecting h11<0.15,>=0.13 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
19 Downloading h11-0.14.0-py3-none-any.whl (58 kB)
20 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 10.2 MB/s eta 0:00:00
21Downloading httpx-0.25.0-py3-none-any.whl (75 kB)
22 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.7/75.7 kB 7.7 MB/s eta 0:00:00
23Downloading httpcore-0.18.0-py3-none-any.whl (76 kB)
24 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.0/76.0 kB 8.0 MB/s eta 0:00:00
25Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)
26 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 158.3/158.3 kB 6.6 MB/s eta 0:00:00
27Downloading anyio-4.0.0-py3-none-any.whl (83 kB)
28 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 83.1/83.1 kB 9.8 MB/s eta 0:00:00
29Installing collected packages: sniffio, idna, h11, certifi, anyio, httpcore, httpx
30Successfully installed anyio-4.0.0 certifi-2023.7.22 h11-0.14.0 httpcore-0.18.0 httpx-0.25.0 idna-3.4 sniffio-1.3.0

This command install Python packages within Docker container and packages is installed under python dir.

So you can deploy with aws-cli. Run bellow

1cd layer
2zip -r lasyer.zip python
3aws lambda publish-layer-version --layer-name my-layer \
4 --description "My layer" \
5 --zip-file fileb://layer.zip \
6 --compatible-runtimes python3.11 \
7 --compatible-architectures "x86_64"
1cd layer
2zip -r lasyer.zip python
3aws lambda publish-layer-version --layer-name my-layer \
4 --description "My layer" \
5 --zip-file fileb://layer.zip \
6 --compatible-runtimes python3.11 \
7 --compatible-architectures "x86_64"