Skip to content

AWS Lambda

TiTiler is built on top of FastAPI, a modern, fast, Python web framework for building APIs. It doesn't work natively with AWS Lambda and API Gateway because FastAPI understands HTTP requests, not API Gateway's event and context JSON objects. However, we can make our FastAPI application work on Lambda by wrapping it with the awesome mangum module, which translates API Gateway events into HTTP requests.

from mangum import Mangum
from titiler.main import app

handler = Mangum(app, enable_lifespan=False)

Deploy

The Lambda stack is also deployed by the AWS CDK utility. Under the hood, CDK will create the deployment package required for AWS Lambda, upload it to AWS, and handle the creation of the Lambda and API Gateway resources.

  1. Install CDK and connect to your AWS account. This step is only necessary once per AWS account.

    # Download titiler repo
    git clone https://github.com/developmentseed/titiler.git
    cd titiler/deployment/aws
    
    # Create a virtual environment
    python -m pip install --upgrade virtualenv
    virtualenv .venv
    source .venv/bin/activate
    
    # Install CDK dependencies
    python -m pip install -r requirements-cdk.txt
    
    # Install NodeJS dependencies
    npm install
    
    $ npm run cdk -- bootstrap # Deploys the CDK toolkit stack into an AWS environment
    
    # or in specific region
    $ npm run cdk -- bootstrap aws://${AWS_ACCOUNT_ID}/eu-central-1
    
  2. Pre-Generate CFN template

    $ npm run cdk -- synth  # Synthesizes and prints the CloudFormation template for this stack
    
  3. Update settings (see intro.md)

    export TITILER_STACK_NAME="mytiler"
    export TITILER_STACK_STAGE="dev"
    export TITILER_STACK_MEMORY=512
    

    Available settings for AWS Lambda:

    timeout: int = 10
    memory: int = 1536
    
    # The maximum of concurrent executions you want to reserve for the function.
    # Default: - No specific limit - account limit.
    max_concurrent: Optional[int]
    
  4. Deploy

    $ npm run cdk -- deploy mytiler-lambda-dev # Deploys the stack(s) titiler-lambda-dev in cdk/app.py
    
    # Deploy in specific region
    $ AWS_DEFAULT_REGION=eu-central-1 AWS_REGION=eu-central-1 npm run cdk -- deploy mytiler-lambda-dev