diff --git a/Dockerfile b/Dockerfile index 12faa696..ed43e237 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,16 +11,13 @@ 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 @@ -28,6 +25,18 @@ RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/dd 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 / diff --git a/scripts/check_layer_size.sh b/scripts/check_layer_size.sh index 84752fa1..977c0283 100755 --- a/scripts/check_layer_size.sh +++ b/scripts/check_layer_size.sh @@ -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"