Deploy FastAPI Apps with DeployHQ
Deploying a FastAPI application efficiently can be challenging, especially if you can't run commands to generate a production build directly on the server, like with shared hosting via FTP, Amazon S3, or Rackspace Cloud Files.
One way to get around this is to generate the production build locally and manually upload the files to each server. However, this approach has several downsides:
- Security risks and frustration: Every team member needs to have server credentials saved and kept in sync on their own machine, which is a security risk and can be incredibly frustrating to manage.
- Dependency headaches: If someone wants to deploy a change, they must have all the necessary dependencies installed (like Python and Pip) to generate a new production build.
- Time-consuming and error-prone: Building locally is a manual process, making it far more time-consuming and susceptible to errors.
DeployHQ was designed to solve these problems by helping developers automate their deployment process for FastAPI projects! All you need to do is create a project, add a server, and configure a couple of build commands.
Getting Started
If you don't have a DeployHQ account, you'll need to sign up for a new account.
Note: You can deploy 1 project up to 3 times a day on the free plan!
Once you're logged in, go to the Projects tab and click New Project.
Follow the wizard to connect your repository to your project. If your repository hosting provider isn't listed, you can manually enter your repository details.
If you run into any difficulties, please check out this article on our support center.
Setting Up a Server
DeployHQ needs to know where to upload your application files.
From the sidebar, select Servers, then click New Server.
Choose your protocol and enter the required server information. You can find detailed guides for:
- Configuring an FTP server
- Configuring an FTPS server
- Configuring an SSH/SFTP server
- Configuring an Amazon S3 Bucket
- Configuring Digital Ocean Droplet
- Configuring AWS ElasticBeanstalk
- Configuring Heroku Dyno
Adding the Build Commands
Next, we'll add the commands needed to prepare your FastAPI application for production. These will be executed in order on a secure, isolated environment with Python pre-installed.
From the sidebar, choose Build Commands, then click New Build Command. Add the following build command for a typical FastAPI project to install dependencies:
-
pip install -r requirements.txt
Configuring Build Caching
By default, DeployHQ starts from a clean slate on every deployment. This can increase the time it takes to build your FastAPI project because all dependencies in your requirements.txt
file must be re-downloaded each time.
To prevent this, DeployHQ lets you specify a list of files and folders to preserve between deployments. Select Cached Build Files from the sidebar, then click New Cached File.
Enter the path to your Python virtual environment (e.g., venv/**) to cache your project dependencies and save.
Change the Deployment Subdirectory
Most Python/FastAPI projects don't have a build step that generates files into a separate subdirectory like dist
or build
. Therefore, you likely won't need to change the Deployment Subdirectory from its default setting.
However, if your project does have a build process that outputs to a specific directory, you can update this setting.
Go to Configuration under the Repository heading in the sidebar, then scroll down to Deployment Subdirectory. Enter the name of your build directory here if necessary, but for most FastAPI projects, this step can be skipped.
Additional Deployment Considerations
For FastAPI applications, you'll want to ensure that:
- Your environment-specific configurations are properly managed.
- Necessary environment variables are set on the server.
- Your web server (like Gunicorn or Uvicorn) is configured to run your FastAPI application.
Run Your First Deployment
All the configuration is doneāit's time to deploy! Click Deploy Project in the header.
The server you created should be preselected, along with the start and end revisions. Once you're happy with everything, scroll down and click Deploy to begin the deployment.
Now, sit back and relax. You just saved yourself a lot of time!