DEV Community

Cover image for Creating AWS layer with Docker.
Anariak
Anariak

Posted on

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

Prerequisites

  • Docker instalado
  • Python 3.x
  • AWS CLI configurado (optional)
  • Acceso a AWS Lambda

1. preparing the environment

mkdir lambda-layer
cd lambda-layer
mkdir python
cd python
Enter fullscreen mode Exit fullscreen mode

2. Installing the python dependencies

pip3 install [your_dependencies] \
    --platform manylinux2014_x86_64 \
    --target . \
    --only-binary=:all: \
    --implementation cp \
    --python-version [TU_VERSION_PYTHON] \
    --no-deps
Enter fullscreen mode Exit fullscreen mode

Example with pdf2image:

pip3 install pdf2image Pillow \
    --platform manylinux2014_x86_64 \
    --target . \
    --only-binary=:all: \
    --implementation cp \
    --python-version 3.10 \
    --no-deps
Enter fullscreen mode Exit fullscreen mode

3. Dockerfile Base

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"]
Enter fullscreen mode Exit fullscreen mode

4. Build and extract

# image build
docker build -t lambda-layer .

# extract layer.zip
docker run --rm -v "$(pwd)":/out lambda-layer cp /layer.zip /out/
Enter fullscreen mode Exit fullscreen mode

5. using the layer

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)}'
        }

Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)