From b2e83ec01494eacdca570101e38bb022a9d5c535 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 23 Apr 2024 14:46:22 +0000 Subject: [PATCH] propagate cache option to docker builds --- infrastructure/aws/cdk/app.py | 5 +++- infrastructure/aws/lambda/Dockerfile.redis | 33 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 infrastructure/aws/lambda/Dockerfile.redis diff --git a/infrastructure/aws/cdk/app.py b/infrastructure/aws/cdk/app.py index b0cd20e..e90f9b8 100644 --- a/infrastructure/aws/cdk/app.py +++ b/infrastructure/aws/cdk/app.py @@ -132,14 +132,17 @@ def __init__( {"TITILER_XARRAY_CACHE_HOST": redis_cluster.attr_redis_endpoint_address} ) + dockerfile_name = "Dockerfile.redis" if settings.enable_cache else "Dockerfile" + lambda_function = aws_lambda.Function( self, f"{id}-lambda", runtime=runtime, code=aws_lambda.Code.from_docker_build( path=os.path.abspath(context_dir), - file="infrastructure/aws/lambda/Dockerfile", + file=f"infrastructure/aws/lambda/{dockerfile_name}", platform="linux/amd64", + build_args={"ENABLE_CACHE": settings.enable_cache}, ), handler="handler.handler", memory_size=memory, diff --git a/infrastructure/aws/lambda/Dockerfile.redis b/infrastructure/aws/lambda/Dockerfile.redis new file mode 100644 index 0000000..41d1635 --- /dev/null +++ b/infrastructure/aws/lambda/Dockerfile.redis @@ -0,0 +1,33 @@ +ARG PYTHON_VERSION=3.10 + +FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION} + +WORKDIR /tmp + +COPY pyproject.toml pyproject.toml +COPY LICENSE LICENSE +COPY README.md README.md +COPY titiler/ titiler/ + +# Install dependencies +# HACK: aiobotocore has a tight botocore dependency +# https://github.com/aio-libs/aiobotocore/issues/862 +# and becaise we NEED to remove both boto3 and botocore to save space for the package +# we have to force using old package version that seems `almost` compatible with Lambda env botocore +# https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html +RUN pip install --upgrade pip + +RUN pip install ".[cache]" "mangum>=0.10.0" "botocore==1.29.76" "aiobotocore==2.5.0" -t /asset --no-binary pydantic; + +# Reduce package size and remove useless files +RUN cd /asset && find . -type f -name '*.pyc' | while read f; do n=$(echo $f | sed 's/__pycache__\///' | sed 's/.cpython-[0-9]*//'); cp $f $n; done; +RUN cd /asset && find . -type d -a -name '__pycache__' -print0 | xargs -0 rm -rf +RUN cd /asset && find . -type f -a -name '*.py' -print0 | xargs -0 rm -f +RUN find /asset -type d -a -name 'tests' -print0 | xargs -0 rm -rf +RUN rm -rdf /asset/numpy/doc/ /asset/bin /asset/geos_license /asset/Misc +RUN rm -rdf /asset/boto3* +RUN rm -rdf /asset/botocore* + +COPY infrastructure/aws/lambda/handler.py /asset/handler.py + +CMD ["echo", "hello world"]