Home > Article > Backend Development > Creating AWS layer with Docker.
When we need to create a layer in AWS, for lambda functions, and this layer has some SO dependencies for its operations, so we are in a problem, the aws documentation for this, can be insufficient.
So, in this case, we can build the missing binaries in the layer that are required.
Well, for this example, we are going to make the demo using Python 3.x and the Pdf2Image library
mkdir lambda-layer cd lambda-layer mkdir python cd python
pip3 install [your_dependencies] \ --platform manylinux2014_x86_64 \ --target . \ --only-binary=:all: \ --implementation cp \ --python-version [TU_VERSION_PYTHON] \ --no-deps
Example with pdf2image:
pip3 install pdf2image Pillow \ --platform manylinux2014_x86_64 \ --target . \ --only-binary=:all: \ --implementation cp \ --python-version 3.10 \ --no-deps
FROM ubuntu:22.04 as builder ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y \ python3.10 \ python3-pip \ binutils \ zip \ [TUS_PAQUETES_ADICIONALES] \ --no-install-recommends \ && rm -rf /var/lib/apt/lists/* WORKDIR /lambda RUN mkdir -p /opt/python/lib/python3.10/site-packages/bin COPY python/ /opt/python/lib/python3.10/site-packages/ RUN cp [TUS_BINARIOS] /opt/python/lib/python3.10/site-packages/bin/ && \ chmod 755 /opt/python/lib/python3.10/site-packages/bin/* RUN cd /opt && zip -r9 /lambda/layer.zip python/ FROM alpine:3.18 COPY --from=builder /lambda/layer.zip / CMD ["/bin/sh"]
# image build docker build -t lambda-layer . # extract layer.zip docker run --rm -v "$(pwd)":/out lambda-layer cp /layer.zip /out/
After the previous steps, we can upload our layer as always and import it into our projects
import os import sys #Configuring paths SITE_PACKAGES = '/opt/python/lib/python3.10/site-packages' BIN_DIR = os.path.join(SITE_PACKAGES, 'bin') os.environ['PATH'] = f"{BIN_DIR}:{os.environ['PATH']}" sys.path.append(SITE_PACKAGES) #importing dependencies from pdf2image import [your_import] def lambda_handler(event, context): try: # your code here return { 'statusCode': 200, 'body': 'Success' } except Exception as e: return { 'statusCode': 500, 'body': f'Error: {str(e)}' }
The above is the detailed content of Creating AWS layer with Docker.. For more information, please follow other related articles on the PHP Chinese website!