- 
                Notifications
    You must be signed in to change notification settings 
- Fork 7
build: speed up raster and stac lambda builds #547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            ividito
  wants to merge
  8
  commits into
  develop
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feat/fast-synth
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      50dcd1a
              
                build: speed up raster and stac lambda builds
              
              
                ividito d6fd79c
              
                update dockerfile
              
              
                ividito 3529f36
              
                Re-add debug lines
              
              
                ividito af5aea4
              
                Fix indent
              
              
                ividito a0f563e
              
                Try removing import check from dockerfiles
              
              
                ividito 0ad8528
              
                try lambda_handler
              
              
                ividito 35bf56f
              
                fix
              
              
                ividito d804811
              
                Try fixing stac mangum import
              
              
                ividito File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,25 +1,71 @@ | ||
| FROM --platform=linux/amd64 public.ecr.aws/sam/build-python3.12:latest | ||
| ARG PYTHON_VERSION=3.12 | ||
|  | ||
| RUN dnf install -y gcc-c++ | ||
| # Stage 1: application and dependencies | ||
| FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} AS builder | ||
| COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ | ||
|  | ||
| RUN dnf install -y gcc-c++ && dnf clean all | ||
|  | ||
| WORKDIR /tmp | ||
|  | ||
| COPY raster_api/runtime /tmp/raster | ||
| RUN pip install mangum /tmp/raster["psycopg-binary"] -t /asset --no-binary pydantic | ||
|  | ||
| # we could have a `uv export` here, if we converted the project format | ||
| RUN uv pip install mangum /tmp/raster["psycopg-binary"] --target /deps --no-binary pydantic | ||
|  | ||
| RUN <<EOF | ||
| uv pip install \ | ||
| --compile-bytecode \ | ||
| --no-binary pydantic \ | ||
| --target /deps \ | ||
| --no-cache-dir \ | ||
| --disable-pip-version-check \ | ||
| /tmp/raster["psycopg-binary"] \ | ||
| mangum | ||
| EOF | ||
| RUN rm -rf /tmp/raster | ||
| RUN cp /usr/lib64/libexpat.so.1 /asset/ | ||
|  | ||
| # # 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/boto3* /asset/botocore* /asset/bin /asset/geos_license /asset/Misc | ||
| # Aggressive cleanup to minimize size and optimize for Lambda container | ||
| # Clean up app dependencies in /deps | ||
| WORKDIR /deps | ||
| SHELL ["/bin/bash", "-o", "pipefail", "-c"] | ||
| RUN <<EOF | ||
| # Convert .pyc files and remove source .py files for faster cold starts | ||
| find . -type f -name '*.pyc' | while read -r f; do n="$(echo "$f" | sed 's/__pycache__\///' | sed 's/.cpython-[0-9]*//')"; cp "$f" "$n"; done | ||
| find . -type d -a -name '__pycache__' -print0 | xargs -0 rm -rf | ||
| find . -type f -a -name '*.py' -print0 | xargs -0 rm -f | ||
| # Remove unnecessary files for Lambda runtime | ||
| find . -type d -a -name 'tests' -print0 | xargs -0 rm -rf | ||
| find . -type d -a -name 'test' -print0 | xargs -0 rm -rf | ||
| rm -rf numpy/doc/ bin/ geos_license Misc/ | ||
| # Remove unnecessary locale and documentation files | ||
| find . -name '*.mo' -delete | ||
| find . -name '*.po' -delete | ||
| find . -name 'LICENSE*' -delete | ||
| find . -name 'README*' -delete | ||
| find . -name '*.md' -delete | ||
| # Strip debug symbols from shared libraries (preserve numpy.libs) | ||
| find . -type f -name '*.so*' -not -path "*/numpy.libs/*" -exec strip --strip-unneeded {} \; 2>/dev/null || true | ||
| EOF | ||
|  | ||
| # Stage 2: Final runtime stage - minimal Lambda image optimized for container runtime | ||
| FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} | ||
|  | ||
| ENV PYTHONUNBUFFERED=1 \ | ||
| PYTHONDONTWRITEBYTECODE=1 | ||
|  | ||
| COPY --from=builder /deps ${LAMBDA_RUNTIME_DIR}/ | ||
| COPY --from=builder /usr/lib64/libexpat.so.1 ${LAMBDA_RUNTIME_DIR}/ | ||
| COPY raster_api/runtime/handler.py ${LAMBDA_RUNTIME_DIR}/ | ||
|  | ||
| COPY raster_api/runtime/handler.py /asset/handler.py | ||
| RUN dnf remove -y gcc-c++ | ||
|  | ||
| WORKDIR /asset | ||
| RUN python -c "from handler import handler; print('All Good')" | ||
| RUN <<EOF | ||
| chmod 644 "${LAMBDA_RUNTIME_DIR}"/handler.py | ||
| chmod -R 755 /opt/ | ||
| # Pre-compile the handler for faster cold starts | ||
| python -c "import py_compile; py_compile.compile('${LAMBDA_RUNTIME_DIR}/handler.py', doraise=True)" | ||
| # Create cache directories with proper permissions | ||
| mkdir -p /tmp/.cache && chmod 777 /tmp/.cache | ||
| EOF | ||
|  | ||
| CMD ["echo", "hello world"] | ||
| CMD ["handler.lambda_handler"] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,18 +1,58 @@ | ||
| FROM --platform=linux/amd64 public.ecr.aws/sam/build-python3.12:latest | ||
| ARG PYTHON_VERSION=3.12 | ||
|  | ||
| # Stage 1: application and dependencies | ||
| FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} AS builder | ||
| COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ | ||
|  | ||
| RUN dnf install -y gcc-c++ && dnf clean all | ||
|  | ||
| WORKDIR /tmp | ||
|  | ||
| COPY stac_api/runtime /tmp/stac | ||
|  | ||
| RUN pip install "mangum" "plpygis>=0.2.1" /tmp/stac -t /asset --no-binary pydantic | ||
| # we could have a `uv export` here, if we converted the project format | ||
| RUN uv pip install mangum "plpygis>=0.2.1" /tmp/stac["psycopg-binary"] --target /deps --no-binary pydantic | ||
| RUN rm -rf /tmp/stac | ||
|  | ||
| # 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 | ||
| WORKDIR /deps | ||
| SHELL ["/bin/bash", "-o", "pipefail", "-c"] | ||
| RUN <<EOF | ||
| # Convert .pyc files and remove source .py files for faster cold starts | ||
| find . -type f -name '*.pyc' | while read -r f; do n="$(echo "$f" | sed 's/__pycache__\///' | sed 's/.cpython-[0-9]*//')"; cp "$f" "$n"; done | ||
| find . -type d -a -name '__pycache__' -print0 | xargs -0 rm -rf | ||
| find . -type f -a -name '*.py' -print0 | xargs -0 rm -f | ||
| # Remove unnecessary files for Lambda runtime | ||
| find . -type d -a -name 'tests' -print0 | xargs -0 rm -rf | ||
| find . -type d -a -name 'test' -print0 | xargs -0 rm -rf | ||
| rm -rf numpy/doc/ bin/ geos_license Misc/ | ||
| # Remove unnecessary locale and documentation files | ||
| find . -name '*.mo' -delete | ||
| find . -name '*.po' -delete | ||
| find . -name 'LICENSE*' -delete | ||
| find . -name 'README*' -delete | ||
| find . -name '*.md' -delete | ||
| # Strip debug symbols from shared libraries (preserve numpy.libs) | ||
| find . -type f -name '*.so*' -not -path "*/numpy.libs/*" -exec strip --strip-unneeded {} \; 2>/dev/null || true | ||
| EOF | ||
|  | ||
| # Stage 2: Final runtime stage - minimal Lambda image optimized for container runtime | ||
| FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} | ||
|  | ||
| ENV PYTHONUNBUFFERED=1 \ | ||
| PYTHONDONTWRITEBYTECODE=1 | ||
|  | ||
| COPY --from=builder /deps ${LAMBDA_RUNTIME_DIR}/ | ||
| COPY --from=builder /usr/lib64/libexpat.so.1 ${LAMBDA_RUNTIME_DIR}/ | ||
| COPY stac_api/runtime/handler.py ${LAMBDA_RUNTIME_DIR}/ | ||
|  | ||
|  | ||
| COPY stac_api/runtime/handler.py /asset/handler.py | ||
| RUN <<EOF | ||
| chmod 644 "${LAMBDA_RUNTIME_DIR}"/handler.py | ||
| chmod -R 755 /opt/ | ||
| # Pre-compile the handler for faster cold starts | ||
| python -c "import py_compile; py_compile.compile('${LAMBDA_RUNTIME_DIR}/handler.py', doraise=True)" | ||
| # Create cache directories with proper permissions | ||
| mkdir -p /tmp/.cache && chmod 777 /tmp/.cache | ||
| EOF | ||
|  | ||
| CMD ["echo", "hello world"] | ||
| CMD ["handler.lambda_handler"] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI there might be a performance hit on Lambda init time by switching from the zip package to a DockerImageFunction. We were forced into this change in titiler-cmr because we were bumping into the 250 MB limit for the function's code (many heavy dependencies). You should be able to use some of the same patterns from the Dockerfile to improve the build time for a typical
Function, though.