Skip to content

Commit

Permalink
propagate cache option to docker builds
Browse files Browse the repository at this point in the history
  • Loading branch information
EC2 Default User committed Apr 23, 2024
1 parent 511c34e commit b2e83ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion infrastructure/aws/cdk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions infrastructure/aws/lambda/Dockerfile.redis
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit b2e83ec

Please sign in to comment.