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.
-
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 # Install NodeJS dependencies npm install -g aws-cdk@2.159.1 uv run cdk -- bootstrap # Deploys the CDK toolkit stack into an AWS environment # or in specific region uv run cdk -- bootstrap aws://${AWS_ACCOUNT_ID}/eu-central-1 -
Pre-Generate CFN template
uv run cdk -- synth # Synthesizes and prints the CloudFormation template for this stack -
Update settings (see intro.md)
export TITILER_STACK_NAME="mytiler" export TITILER_STACK_STAGE="dev" export TITILER_STACK_MEMORY=512Available 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] -
Deploy
uv 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 uv run cdk -- deploy mytiler-lambda-dev