Reader small image

You're reading from  Building Data Science Applications with FastAPI

Product typeBook
Published inOct 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781801079211
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
François Voron
François Voron
author image
François Voron

François Voron graduated from the University of Saint-Étienne (France) and the University of Alicante (Spain) with a master's degree in machine learning and data mining. A full stack web developer and a data scientist, François has a proven track record working in the SaaS industry, with a special focus on Python backends and REST APIs. He is also the creator and maintainer of FastAPI Users, the #1 authentication library for FastAPI, and is one of the top experts in the FastAPI community.
Read more about François Voron

Right arrow

Chapter 10: Deploying a FastAPI Project

Building a good application is great, but it's even better if customers can enjoy it. In this chapter, you'll look at different techniques and the best practices for deploying your FastAPI application to make it available on the web. First, you'll learn how to structure your project to make it ready for deployment by using environment variables to set the configuration options you need, as well as by managing your dependencies properly with pip. Once done, we'll show you three ways to deploy your application: with a serverless cloud platform, with a Docker container, and with a traditional Linux server.

In this chapter, we're going to cover the following main topics:

  • Setting and using environment variables
  • Managing Python dependencies
  • Deploying a FastAPI application on a serverless platform
  • Deploying a FastAPI application with Docker
  • Deploying a FastAPI application on a traditional server
  • ...

Technical requirements

For this chapter, you'll need a Python virtual environment, similar to the one we set up in Chapter 1, Python Development Environment Setup.

You can find all the code examples for this chapter in its dedicated GitHub repository: https://github.com/PacktPublishing/Building-Data-Science-Applications-with-FastAPI/tree/main/chapter10.

Setting and using environment variables

Before deep diving into the different deployment techniques, we need to structure our application to enable reliable, fast, and secure deployments. One of the key things in this process is handling configuration variables: a database URL, an external API token, a debug flag, and so on. When handling those variables, it's necessary to handle them dynamically instead of hardcoding them in your source code. Why?

First of all, those variables will likely be different in your local environment and in production. Typically, your database URL will point to a local database on your computer while developing but will point to a proper production database in production. This is even more true if you want to have other environments such as a staging or pre-production environment. Furthermore, if we need to change one of the values, we'll have to change the code, commit it, and deploy it again. Thus, we need a convenient mechanism to set those...

Managing Python dependencies

Throughout this book, we've installed libraries using pip to add some useful features to our application: FastAPI, of course, but also SQLAlchemy, Tortoise ORM, Pytest, and so on. When deploying a project to a new environment, such as a production server, we have to make sure all those dependencies are installed for our application to work properly. This is also true if you have colleagues that also need to work on the project: they need to know the dependencies they must install on their machines.

Fortunately, pip comes with a solution for this so that we don't have to remember all this in our heads. Indeed, most Python projects define a requirements.txt file, which contains a list of all Python dependencies. It usually lives at the root of your project. pip has a special option for reading this file and installing all the needed dependencies.

When you already have a working environment, such as the one we've used since the beginning...

Deploying a FastAPI application on a serverless platform

In recent years, serverless platforms have gained a lot of popularity and have become a very common way to deploy web applications. Those platforms completely hide the complexity of setting up and managing a server, giving you the tools to automatically build and deploy your application in minutes. Google App Engine, Heroku, and Azure App Service are among the most popular. Even though they have their own specificities, all these serverless platforms work on the same principles. This is why, in this section, we'll outline the common steps you should follow.

Usually, serverless platforms expect you to provide the source code in the form of a GitHub repository, which you push directly to their servers or that they pull automatically from GitHub. Here, we'll assume that you have a GitHub repository with the source code structured like so:

Figure 10.1 – Project structure for serverless deployment...

Deploying a FastAPI application with Docker

Docker is a widely used technology for containerization. Containers are small, self-contained systems running on a computer. Each container contains all the files and configurations necessary for running a single application: a web server, a database engine, a data processing application, and so on. The main goal is to be able to run those applications without worrying about dependency and version conflicts that often happen when trying to install and configure them on the system.

Besides, Docker containers are designed to be portable and reproducible: to create a Docker container, you simply have to write a Dockerfile containing all the necessary instructions to build the small system, along with all the files and configuration you need. Those instructions are executed during a build, which results in a Docker image. This image is a package containing your small system, ready to use, that you can easily share on the internet through registries...

Deploying a FastAPI application on a traditional server

In some situations, you may not have the chance to use a serverless platform to deploy your application. Some security or regulatory policies may force you to deploy on physical servers with specific configurations. In this case, it's worth knowing some basic things so that you can deploy your application on traditional servers.

In this section, we'll consider you are working on a Linux server:

  1. First of all, make sure a recent version of Python has been installed on your server, ideally with the version matching the one you used in development. The easiest way to do this is to set up pyenv, as we saw in Chapter 1, Python Development Environment Setup.
  2. To retrieve your source code and keep it in sync with your latest developments, you can clone your Git repository on your server. This way, you only have to pull the changes and restart the server process to deploy a new version.
  3. Set up a Python virtual...

Summary

Your application is now live on the web! In this chapter, we covered the best practices to apply before deploying your application to production: use environment variables to set configuration options, such as database URLs, and manage your Python dependencies with a requirements.txt file. Then, we showed you how to deploy your application to a serverless platform, which handles everything for you by retrieving your source code, packaging it with its dependencies, and serving it on the web. Next, you learned how to build a Docker image for FastAPI using the base image created by the creator of FastAPI. As you've seen, it allows you to be flexible while configuring the system, but you can still deploy it in a few minutes with a serverless platform that's compatible with containers. Finally, we provided you with some guidelines for manual deployment on a traditional Linux server.

This marks the end of the second part of this book. You should now be confident in writing...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Data Science Applications with FastAPI
Published in: Oct 2021Publisher: PacktISBN-13: 9781801079211
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
François Voron

François Voron graduated from the University of Saint-Étienne (France) and the University of Alicante (Spain) with a master's degree in machine learning and data mining. A full stack web developer and a data scientist, François has a proven track record working in the SaaS industry, with a special focus on Python backends and REST APIs. He is also the creator and maintainer of FastAPI Users, the #1 authentication library for FastAPI, and is one of the top experts in the FastAPI community.
Read more about François Voron