Skip to content

Commit

Permalink
Pre-compile all python code and remove original .py files. (#476)
Browse files Browse the repository at this point in the history
* Remove urllib3 dist-info files as well.

* Pre-compile all python code and remove original .py files.

* Compress at optimization level 2.

* Add PYTHONNODEBUGRANGES=1 to compile step.

* Add comment.

* Use different commandline option to support py3.8

* Update size limits.

* Leave __init__.py contrib files.

* Remove text like files.

* oops
  • Loading branch information
purple4reina committed Apr 22, 2024
1 parent f80f83f commit 21012cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
17 changes: 13 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@ WORKDIR /build
COPY . .
RUN pip install . -t ./python/lib/$runtime/site-packages

# Remove *.pyc files
RUN find ./python/lib/$runtime/site-packages -name \*.pyc -delete

# Remove botocore (40MB) to reduce package size. aws-xray-sdk
# installs it, while it's already provided by the Lambda Runtime.
RUN rm -rf ./python/lib/$runtime/site-packages/botocore*
RUN rm -rf ./python/lib/$runtime/site-packages/setuptools
RUN rm -rf ./python/lib/$runtime/site-packages/jsonschema/tests
RUN find . -name 'libddwaf.so' -delete
RUN rm -rf ./python/lib/$runtime/site-packages/urllib3
RUN rm -rf ./python/lib/$runtime/site-packages/urllib3*
RUN rm ./python/lib/$runtime/site-packages/ddtrace/appsec/_iast/_taint_tracking/*.so
RUN rm ./python/lib/$runtime/site-packages/ddtrace/appsec/_iast/_stacktrace*.so
RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/libdd_wrapper.so
RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/ddup/_ddup.*.so
RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/stack_v2/_stack_v2.*.so
RUN find . -name "*.dist-info" -type d | xargs rm -rf

# Precompile all .pyc files and remove .py files. This speeds up load time.
# Compile with optimization level 2 (-OO) and PYTHONNODEBUGRANGES=1 to redtce
# size of .pyc files.
# See https://docs.python.org/3/tutorial/modules.html#compiled-python-files
# https://docs.python.org/3.11/using/cmdline.html#cmdoption-O
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONNODEBUGRANGES
RUN PYTHONNODEBUGRANGES=1 python -OO -m compileall -b ./python/lib/$runtime/site-packages
# remove all .py files except ddtrace/contrib/*/__init__.py which are necessary
# for ddtrace.patch to discover instrumationation packages.
RUN find ./python/lib/$runtime/site-packages -name \*.py | grep -v ddtrace/contrib | xargs rm -rf
RUN find ./python/lib/$runtime/site-packages/ddtrace/contrib -name \*.py | grep -v __init__ | xargs rm -rf
RUN find ./python/lib/$runtime/site-packages -name __pycache__ -type d -exec rm -r {} \+

FROM scratch
COPY --from=builder /build/python /
5 changes: 2 additions & 3 deletions scripts/check_layer_size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

# Compares layer size to threshold, and fails if below that threshold

# 7 mb size limit
set -e
MAX_LAYER_COMPRESSED_SIZE_KB=$(expr 4 \* 1024)
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(expr 13 \* 1024)
MAX_LAYER_COMPRESSED_SIZE_KB=$(expr 5 \* 1024)
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(expr 12 \* 1024)


LAYER_FILES_PREFIX="datadog_lambda_py"
Expand Down

0 comments on commit 21012cb

Please sign in to comment.