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 i…


This content originally appeared on DEV Community and was authored by Anariak

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

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

Example with pdf2image:

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

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"]

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/

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

References


This content originally appeared on DEV Community and was authored by Anariak


Print Share Comment Cite Upload Translate Updates
APA

Anariak | Sciencx (2024-11-06T02:43:08+00:00) Creating AWS layer with Docker.. Retrieved from https://www.scien.cx/2024/11/06/creating-aws-layer-with-docker/

MLA
" » Creating AWS layer with Docker.." Anariak | Sciencx - Wednesday November 6, 2024, https://www.scien.cx/2024/11/06/creating-aws-layer-with-docker/
HARVARD
Anariak | Sciencx Wednesday November 6, 2024 » Creating AWS layer with Docker.., viewed ,<https://www.scien.cx/2024/11/06/creating-aws-layer-with-docker/>
VANCOUVER
Anariak | Sciencx - » Creating AWS layer with Docker.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/06/creating-aws-layer-with-docker/
CHICAGO
" » Creating AWS layer with Docker.." Anariak | Sciencx - Accessed . https://www.scien.cx/2024/11/06/creating-aws-layer-with-docker/
IEEE
" » Creating AWS layer with Docker.." Anariak | Sciencx [Online]. Available: https://www.scien.cx/2024/11/06/creating-aws-layer-with-docker/. [Accessed: ]
rf:citation
» Creating AWS layer with Docker. | Anariak | Sciencx | https://www.scien.cx/2024/11/06/creating-aws-layer-with-docker/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.