Skip to content

Commit 2a44cdc

Browse files
committed
Use a multi-stage build for the Lambda image
Part of this project's goals is to support the option of running a bespoke image as a Lambda and not just using deployment packages. Since we care about the created image due to this we should build it similar to how we would build other Docker images. Thus we switch to a multi- stage build so that the only things included in the final image are what is absolutely necessary to run the Lambda (per the dependency files). With this configuration we saw space savings of almost 10% by switching to a multi-stage build.
1 parent bde97e0 commit 2a44cdc

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

Dockerfile

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
ARG PY_VERSION=3.9
22

3-
FROM amazon/aws-lambda-python:$PY_VERSION
3+
FROM amazon/aws-lambda-python:$PY_VERSION as install-stage
44

55
# Declare it a second time so it's brought into this scope.
66
ARG PY_VERSION=3.9
77

8-
# This must be present in the image to generate a deployment artifact.
9-
ENV BUILD_PY_VERSION=$PY_VERSION
8+
# Install the Python packages necessary to install the Lambda dependencies.
9+
RUN python3 -m pip install --no-cache-dir \
10+
pip \
11+
setuptools \
12+
wheel \
13+
# This version of pipenv is the minimum version to allow passing arguments
14+
# to pip with the --extra-pip-args option.
15+
&& python3 -m pip install --no-cache-dir "pipenv>=2022.9.8"
16+
17+
WORKDIR /tmp
18+
19+
# Copy in the dependency files.
20+
COPY src/py$PY_VERSION/ .
21+
22+
# Install the Lambda dependencies.
23+
#
24+
# The --extra-pip-args option is used to pass necessary arguments to the
25+
# underlying pip calls.
26+
RUN pipenv sync --extra-pip-args="--no-cache-dir --target ${LAMBDA_TASK_ROOT}"
27+
28+
FROM amazon/aws-lambda-python:$PY_VERSION as build-stage
1029

1130
###
1231
# For a list of pre-defined annotation keys and value types see:
@@ -21,29 +40,18 @@ ENV BUILD_PY_VERSION=$PY_VERSION
2140
LABEL org.opencontainers.image.authors="[email protected]"
2241
LABEL org.opencontainers.image.vendor="Cybersecurity and Infrastructure Security Agency"
2342

24-
WORKDIR ${LAMBDA_TASK_ROOT}
25-
RUN mkdir build output
43+
# Declare it a third time so it's brought into this scope.
44+
ARG PY_VERSION=3.9
2645

27-
# Install the Python packages necessary to install the Lambda dependencies.
28-
RUN python3 -m pip install --no-cache-dir \
29-
pip \
30-
setuptools \
31-
wheel \
32-
# This version of pipenv is the minimum version to allow passing arguments
33-
# to pip with the --extra-pip-args option.
34-
&& python3 -m pip install --no-cache-dir "pipenv>=2022.9.8"
46+
# This must be present in the image to generate a deployment artifact.
47+
ENV BUILD_PY_VERSION=$PY_VERSION
3548

36-
# Copy in the build files.
37-
COPY src/py$PY_VERSION/ build
38-
COPY src/lambda_handler.py .
49+
COPY --from=install-stage ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}
3950

40-
# Install the Lambda dependencies.
41-
#
42-
# The --extra-pip-args option is used to pass necessary arguments to the
43-
# underlying pip calls.
44-
WORKDIR ${LAMBDA_TASK_ROOT}/build
45-
RUN pipenv sync --extra-pip-args="--no-cache-dir --target .."
51+
WORKDIR ${LAMBDA_TASK_ROOT}
52+
53+
# Copy in the handler.
54+
COPY src/lambda_handler.py .
4655

4756
# Ensure our handler is invoked when the image is used.
48-
WORKDIR ${LAMBDA_TASK_ROOT}
4957
CMD ["lambda_handler.handler"]

0 commit comments

Comments
 (0)