Deploy Flask API to Google Cloud | Towards Data Science

Building that first API is for many of us, a significant step towards creating impactful tools that may one day be used by many developers. But often those APIs don’t make it out of our local machines.
Fortunately, it’s incredibly easy to deploy APIs. Assuming you have no idea what you’re doing right now — you will probably be deploying your first API in around ten minutes.
I’m not joking, it’s super easy. Let’s get started.
There are a couple of prerequisites. These might push you over the ten-minute mark slightly, but it won’t take long I promise! We’re going to be deploying our API using Google’s cloud service, or Google Cloud Platform (GCP).
You will need to enter billing details, but if this is your first time on GCP you will be given a few hundred 💲 worth of free credits. If you want to avoid any billing, make sure that you delete your project after you’ve finished with it.
Sign up for GCP and select/create a project — keep a note of the project name.Enable billingEnable the Cloud Build API.Install and initialize the Cloud SDK.
Sign up for GCP and select/create a project — keep a note of the project name.
Enable billing
Enable the Cloud Build API.
Install and initialize the Cloud SDK.
After initialization, you may need to select the project that you previously created.
We will be using the GCP App Engine service to deploy our Flask API. To use this we will need to add App Engine to our chosen project, and then install the App Engine extension for Python.
From inside your chosen prompt window, we create a new App Engine app using the project name (using your project name) and choose a region.
gcloud app create --project=aurelio-330516
Then we install the App Engine extension for Python with:
gcloud components install app-engine-python
We now have all of the prerequisites required for deploying our API to GCP’s App Engine. Let’s move on to the Flask API itself.
Before blindly throwing our API python script at GCP’s App Engine and hoping for the best — we should confirm that it works. We can do this by simulating the App Engine environment, but first — we need an API.
I will be using this API script. It doesn’t do anything particularly interesting, but it is simple and therefore useful for our example. You can see how I built it here.
Alongside the API script main.py — you will also need to include a ./data directory that contains the two files users.csv and locations.csv. Altogether this means we have a single directory that looks like this:
Back to simulation. To simulate the App Engine environment we will be using gunicorn to run a local Web Server Gateway Interface (WSGI) server. Before this, you may need to pip install gunicorn — then, in the same environment, we run the API:
Then we can head on over to http://0.0.0.0:8080/users. If the API is running as expected we should see a small JSON response that looks like this:
Gunicorn is using our Python environment — so the libraries we’re importing at the top of our main.py script are already installed. And we also know that we’re using (in my case) Python 3.8.
This is a problem when we deploy the API to GCP. App Engine will have no idea that we need to use these packages nor that we’re using Python 3.8. So we must add two more files to our directory:
requirements.txt will contain a list of the Python packages for App Engine to install.
app.yaml will set the runtime to use Python 3.8.
These files will look like this:
And that’s all we need. Now we deploy by running gcloud app deploy inside the directory containing all of our files. This will take a moment to deploy, after which we should see the URL of our deployed API!
Heading over to that URL followed by the /users endpoint will return us the same JSON object we saw earlier:
This means our API is up and running and we can go ahead and make requests as we would with any other API!
That’s all for this quick walkthrough for deploying Flask APIs using GCP’s App Engine.
I hope you enjoyed it! If you have any questions, let me know via Twitter or in the comments below. If you’d like more content like this, I post on YouTube too.
Thanks for reading!
🤖 70% Discount on the NLP With Transformers Course
*All images are by the author except where stated otherwise