Deploying FastAPI application in Google App Engine in Standard Environment
By: Pujan Thapa
Tags: Python, Fastapi, App Engine, Google Cloud Platform, Deployment

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. To learn more about FastAPI, you can visit the docs of FastAPI by clicking here.

FastAPI also provides Swagger UI by default in the {base_url}/docs for testing apis.

Installation

pip install fastapi

You will also need an ASGI server, for production such as Uvicorn or Hypercorn. We will be using uvicorn for this article.

Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools.

Until recently Python has lacked a minimal low-level server/application interface for asyncio frameworks. The ASGI specification fills this gap, and means we’re now able to start building a common set of tooling usable across all asyncio frameworks.

To install uvicorn:

pip install uvicorn

This will install uvicorn with minimal (pure Python) dependencies.

pip install uvicorn[standard]

This will install uvicorn with “Cython-based” dependencies (where possible) and other “optional extras”.

In this context, “Cython-based” means the following:

the event loop uvloop will be installed and used if possible.

will be installed and used if possible. the http protocol will be handled by httptools if possible.

I prefer using uvicorn[standard] as it installs cython-based dependencies which will prevent error related to uvloop and httptools while running in production.

Lastly, you should also install Gunicorn as it is probably the simplest way to run and manage Uvicorn in a production setting. Uvicorn includes a gunicorn worker class that means you can get set up with very little configuration. You do not need to install Gunicorn while running locally.

To install Gunicorn:

pip install gunicorn

Freezing Requirements File

After installing every required dependencies inside virtualenv, do not forget to freeze the requirements file to update before deploying as App Engine installs dependencies from requirements.txt file.

To freeze the requirements file:

pip freeze > requirements.txt

Configuring app.yaml file

Your python version should be above 3.6 for FastAPI to work. Here is my configurations for my project:

runtime: python37

entrypoint: gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app

instance_class: F2

You can have any python version above 3.6 and instance_class as per your need. The following will start Gunicorn with four worker processes:

gunicorn -w 4 -k uvicorn.workers.UvicornWorker

main is my main.py file and app is the instance of my FastAPI application. App Engine handles the port number but you can define your desired port number.

Confirming before deployment

After you completed all the above steps, confirm the following for the last time:

Your virtualenv is activated and all the requirements are installed by activating the environment. Make sure you have installed only the necessary dependencies and included .gcloudignore to ignore unnecessary files and folders during deployment. Freeze the requirements.txt file before deploying so that you don’t miss adding newly installed dependencies in the requirements file. Your app.yaml file is properly configured. Your service account json files have necessary access.

Deploying in App Engine

If you have not installed Google Cloud SDK, then you must install and configure the sdk. You can follow this link to properly configure your google cloud sdk.

After installing the sdk, you need to initialize the sdk. To initialize Cloud SDK:

Run gcloud init from the terminal.

After initializing, make sure you select your correct project id. To select the project from google cloud, you have to run gcloud config set project [project_id]

Finally, to deploy your FastAPI application in the selected project-id:

gcloud app deploy

You will get the url to view the application in your terminal.