Flask Deployment

Getting Started with Flask and Python on Flightcontrol

After following this guide, you will have deployed a basic Flask/Python web application on Amazon Web Services (AWS) using Flightcontrol.

If you need to build a Flask project to follow the guide, we have a short tutorial on Creating a Flask App for you to follow. Otherwise, all you need is your own Python/Flask project in a GitHub repository (either private or public).

We'll use Flightcontrol to deploy the application from that GitHub repository to AWS.

Setting up the Flask Application for Deployment

There are a couple of steps we need to follow to get our Flask application ready for deployment.

Using Gunicorn as the Web Server

To deploy your Flask application in production, you will need a web server to serve your application. We'll use Gunicorn, a Python web server. Install Gunicorn with pip if you need it:

pip install gunicorn

Setting up the Host and Port Number

You will also need to make sure that your Flask application accepts traffic correctly.

At the bottom of your app.py file, add (or modify) the following code to set the host to be '0.0.0.0' and the port to be 3000:

app.py
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=3000)

Creating a wsgi.py File

We'll also need to create a Python file named wsgi.py with the following contents:

wsgi.py
from app import app
 
if __name__ == "__main__":
    app.run()

Adding a Procfile

Nixpacks can use a Procfile to determine how to run your application.

A Procfile is a simple file that contains a one line command, along with the name of the service to use. With a service named web the contents of the Procfile will be:

Procfile
web: gunicorn --bind 0.0.0.0:3000 wsgi:app

Deploying a Flask Application with Flightcontrol

To create a Flightcontrol account, sign up at the Flightcontrol Dashboard (opens in a new tab).

After you have an account created, you can create a new project and deploy your Flask application to AWS.

Creating a Project on Flightcontrol

Each Flightcontrol project contains one or more applications that you want to run on AWS. Within each project, you can have multiple environments. For example, you might have a staging environment and a production environment. Each environment can have one or more services, such as a web front end, a web back end API layer, or a relational database.

On the Flightcontrol dashboard, create a new project from the "Projects" tab in the left hand sidebar.

New Project Page

For a new account, you will see the following form:

New User Landing Page

If you haven't connected your Flightcontrol account to GitHub yet, let's do that now. Click the "Continue with GitHub" button and start the connection process. Be sure to authorize access to the Flask repository you created earlier.

Follow our guide on Connecting Flightcontrol to GitHub to walk you through the necessary steps.

Once you have GitHub connected, you'll see a list of all of the GitHub repositories that the Flightcontrol Ops app can access.

Choose your Flask repository to continue.

Preparing for Launch

The next screen sets your Flightcontrol project up for deployment.

Preparing for Launch

You have the choice of using the Dashboard (also called the GUI) for an interactive configuration, or using a flightcontrol.json file for an infrastructure-as-code configuration.

For the purposes of this Flask Getting Started Guide, we will use the Flightcontrol Dashboard. We'll create one environment, "Production", and create one service in that environment.

Setting up the Flask Application Service

On the dashboard, there are choices for individual services to configure. The Flask application only requires a web server service.

Click the "Add Web Server (Fargate)" button under "Services".

Add Web Server Service

On this screen, we should give the web service a name, such as "Flask".

Flask Service Settings

Once you do this, scroll down to the bottom of the screen and find the "Create Project" button.

Create Project Button

Click "Create Project", and Flightcontrol will start the deployment process! Expect this to take between 10-20 minutes for the initial setup and deployment. Subsequent deployments will be much faster.

Successful Deployment

Now is the time to visit your new Flask application in the browser. You can find the URL for your Flask application after you get a successful deployment in the Flightcontrol dashboard, and it will look similar to this:

Successful Flask Deployment in Dashboard

Once everything is runnning smoothly, go ahead and make a change to your Flask code and push the change to your main branch on GitHub. Flightcontrol will automatically detect the change and deploy it to your environment.

Errors with Deployment

If you run into any errors during the deployment process, you can check the build or deploy logs for the service that failed to deploy. You can find the logs by clicking on the Flightcontrol environment page.

If you're still having troubles, reach out to us for support please get in touch with us in our Discord Channel (opens in a new tab)! We're very happy to help out.