2023/10/2

Creating Python Lambda layer

How to create

Run bellow command.

mkdir layer
cp requirements.txt layer/requirements.txt
docker 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"
mkdir layer
cp requirements.txt layer/requirements.txt
docker 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.

Collecting httpx==0.25.0 (from -r requirements.txt (line 1))
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
Downloading httpx-0.25.0-py3-none-any.whl.metadata (7.6 kB)
Collecting certifi (from httpx==0.25.0->-r requirements.txt (line 1))
Obtaining dependency information for certifi from https://files.pythonhosted.org/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl.metadata
Downloading certifi-2023.7.22-py3-none-any.whl.metadata (2.2 kB)
Collecting httpcore<0.19.0,>=0.18.0 (from httpx==0.25.0->-r requirements.txt (line 1))
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
Downloading httpcore-0.18.0-py3-none-any.whl.metadata (18 kB)
Collecting idna (from httpx==0.25.0->-r requirements.txt (line 1))
Downloading idna-3.4-py3-none-any.whl (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 2.9 MB/s eta 0:00:00
Collecting sniffio (from httpx==0.25.0->-r requirements.txt (line 1))
Downloading sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting anyio<5.0,>=3.0 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
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
Downloading anyio-4.0.0-py3-none-any.whl.metadata (4.5 kB)
Collecting h11<0.15,>=0.13 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 10.2 MB/s eta 0:00:00
Downloading httpx-0.25.0-py3-none-any.whl (75 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.7/75.7 kB 7.7 MB/s eta 0:00:00
Downloading httpcore-0.18.0-py3-none-any.whl (76 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.0/76.0 kB 8.0 MB/s eta 0:00:00
Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 158.3/158.3 kB 6.6 MB/s eta 0:00:00
Downloading anyio-4.0.0-py3-none-any.whl (83 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 83.1/83.1 kB 9.8 MB/s eta 0:00:00
Installing collected packages: sniffio, idna, h11, certifi, anyio, httpcore, httpx
Successfully 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
Collecting httpx==0.25.0 (from -r requirements.txt (line 1))
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
Downloading httpx-0.25.0-py3-none-any.whl.metadata (7.6 kB)
Collecting certifi (from httpx==0.25.0->-r requirements.txt (line 1))
Obtaining dependency information for certifi from https://files.pythonhosted.org/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl.metadata
Downloading certifi-2023.7.22-py3-none-any.whl.metadata (2.2 kB)
Collecting httpcore<0.19.0,>=0.18.0 (from httpx==0.25.0->-r requirements.txt (line 1))
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
Downloading httpcore-0.18.0-py3-none-any.whl.metadata (18 kB)
Collecting idna (from httpx==0.25.0->-r requirements.txt (line 1))
Downloading idna-3.4-py3-none-any.whl (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 2.9 MB/s eta 0:00:00
Collecting sniffio (from httpx==0.25.0->-r requirements.txt (line 1))
Downloading sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting anyio<5.0,>=3.0 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
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
Downloading anyio-4.0.0-py3-none-any.whl.metadata (4.5 kB)
Collecting h11<0.15,>=0.13 (from httpcore<0.19.0,>=0.18.0->httpx==0.25.0->-r requirements.txt (line 1))
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 10.2 MB/s eta 0:00:00
Downloading httpx-0.25.0-py3-none-any.whl (75 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.7/75.7 kB 7.7 MB/s eta 0:00:00
Downloading httpcore-0.18.0-py3-none-any.whl (76 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.0/76.0 kB 8.0 MB/s eta 0:00:00
Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 158.3/158.3 kB 6.6 MB/s eta 0:00:00
Downloading anyio-4.0.0-py3-none-any.whl (83 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 83.1/83.1 kB 9.8 MB/s eta 0:00:00
Installing collected packages: sniffio, idna, h11, certifi, anyio, httpcore, httpx
Successfully 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

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