Skip to content

Guide and template to deploy FastAPI application on AWS Lambda with Docker together with the continuous integration.

Notifications You must be signed in to change notification settings

kurkurzz/fastapi-lambda-docker-blueprint

Repository files navigation

Deploy FastAPI Application to AWS Lambda with Docker Documentation

deploy to lambda flow

Why use Lambda

Pros

  • Pay per use. No charge incurred if there is no activity. Cheaper for most use case.
  • Easy integration with other AWS services. Can invoke using function's url or other AWS activities.
  • Can configure memory and /tmp storage.
  • Can use docker (max of 10 GB of project size).
  • Built-in function URL.

Limitation

Steps

  1. Setup project

    • Wrap fastapi

      • Run pip install mangum
      • Wrap fastapi with mangum. Refer sample code: main.py
    • Create dockerfile. It is not the normal dockerfile but it is built for for Lambda environment

    • Create aws.yml file for Github Action (You need to complete steps 2, 3, 4 first)

      • Create a file .github/workflow/aws.yml
      • Refer aws.yml. Follow the instruction and dont forget to change ENV variables to the correct value
  2. Create a Github repository

    This repository will store your project's code

  3. Create AWS ECR Repository

  4. Create Lambda function

  5. Create Function URL

    • Go to Lambda function's configuration tab
    • Go to Function URL tab
    • Set relevant CORS information

Resources

ARCHIVE

  1. Create API Gateway No longer relevant since AWS introduced built-in function url

    • Go to https://ap-southeast-1.console.aws.amazon.com/apigateway/main/precreate?region=ap-southeast-1

    • Click Build on REST API

    • Give it a name and leave everything as default and click Create API

      Here, the path (/, /ping) is called as resources

    • In / resource, create an ANY method Integration type: Lambda Function, Use Lambda Proxy integration: ✅, Select relevant region and the lambda function you just created. Click Save.

    • In / resource, create a new resource. Configure as proxy resource: ✅ You can give it any name, default also ok. Click Create resource.

    • In the new resource (probably /{proxy+}), create an ANY method. For integration type, select Lambda Function Proxy Select relevant region and the lambda function you just created. Click Save

    • Click Deploy API. Select or create a stage. You can name it prod or staging or whatever Click Deploy

About

Guide and template to deploy FastAPI application on AWS Lambda with Docker together with the continuous integration.

Resources

Stars

Watchers

Forks

Languages