brave-hair-402
02/17/2023, 6:44 PMenough-analyst-54434
02/17/2023, 6:47 PMbrave-hair-402
02/17/2023, 6:52 PM# Define global args
# ARG FUNCTION_DIR="/app"
ARG BUILD_DIR="/build"
ARG RUNTIME_VERSION="3.10"
ARG DISTRO_VERSION="slim-bullseye"
FROM public.ecr.aws/docker/library/python:${RUNTIME_VERSION}-${DISTRO_VERSION} as base-image
ENV PYTHONFAULTHANDLER=1 \
PYTHONHASHSEED=42 \
PYTHONUNBUFFERED=1 \
POETRY_VERSION=1.1.14
# Stage 2 - build function and dependencies
FROM base-image AS build-image
# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev
WORKDIR /app
RUN pip install "poetry==$POETRY_VERSION" && python -m venv /build/venv
COPY . .
RUN . /build/venv/bin/activate && cd projects/some_service/lambda_function && poetry install --no-dev --no-root
COPY projects/some_service/lambda_function/app.py /build/
FROM base-image
WORKDIR /app
COPY --from=build-image build /app
ENTRYPOINT [ "/app/venv/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]
enough-analyst-54434
02/17/2023, 6:57 PMpex_binary(include_tools=True, complete_platforms=[...])
/ Pex (--complete-platform=... --include-tools
), it would be:
COPY dist/my.pex /my.pex
RUN PEX_TOOLS=1 /my.pex venv /build/venv
Instead of the RUN pip install ...
and `RUN . /build/venv/bin/activate ...`lines; otherwise the same.enough-analyst-54434
02/17/2023, 7:00 PMcomplete_platforms
field has docs on this that you'll need to follow over to Pex docs, but I can also give you a 1-liner if that's still unclear.
Alternatively, in Pants 2.15.x, there is support for performing certain build actions in containers. One of those is building a PEX. That is described here: https://www.pantsbuild.org/v2.15/docs/environments
This latter environments option is experimental, but the most solid approach.enough-analyst-54434
02/17/2023, 7:02 PMenough-analyst-54434
02/17/2023, 7:03 PMenough-analyst-54434
02/17/2023, 7:06 PMenough-analyst-54434
02/17/2023, 7:06 PMenough-analyst-54434
02/17/2023, 7:07 PMbrave-hair-402
02/17/2023, 7:26 PMbrave-hair-402
02/19/2023, 12:51 PMdocker_image(name="docker_image", dependencies=[":bin"])
python_sources()
docker_environment(
name="python_bullseye",
platform="linux_x86_64",
image="python:3.10-slim-buster",
)
python_requirement(
name="awslambdaric",
requirements=["awslambdaric==2.0.4"],
)
pex_binary(
name="bin",
entry_point="app.py",
include_tools=True,
dependencies=[":awslambdaric"],
environment="python_bullseye"
)
But I get the following error:
`UnrecognizedEnvironmentError: Unrecognized environment name python_bullseye
from the environment
field from the target`brave-hair-402
02/19/2023, 5:11 PM# Define custom function directory
ARG FUNCTION_DIR="/var/task/"
FROM python:3.10-slim-buster as build-image
# Include global arg in this stage of the build
ARG FUNCTION_DIR
RUN mkdir -p ${FUNCTION_DIR}
# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev
# Install the function's dependencies
COPY projects.web_service.binary_name/bin.pex .
RUN PEX_TOOLS=1 ./bin.pex venv ${FUNCTION_DIR}
RUN cp -r ${FUNCTION_DIR}/lib/python3.10/site-packages/binary_name/* ${FUNCTION_DIR}
FROM python:3.10-slim-buster
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
RUN chmod -R 0755 .
ENTRYPOINT [ "/var/task/bin/python", "-m", "awslambdaric" ]
CMD ["app.handler"]