back to indexAPI Series #3 - How to Deploy Flask APIs to the Cloud (GCP)
Chapters
0:0
0:18 The Gcp Console
0:39 Create a Project
1:14 Initialize the Cloud Build Api
1:53 Install the Gcp Sdk
2:29 Vs Code
4:43 Install the App Engine Extension for Python
11:50 Git Ignore
00:00:02.220 |
a look at how we can deploy a Flask API using Google's Cloud 00:00:16.780 |
And the first thing I'm going to do is actually go to the GCP 00:00:20.780 |
So we just write gcp console, and we just click here. 00:00:28.580 |
And that should bring you through to this page. 00:00:30.580 |
If you already have an account, if you don't, 00:00:32.500 |
you will need to obviously create an account. 00:00:35.820 |
But once you have your account, you come over here. 00:00:38.860 |
You can either come up here to create a project, 00:00:41.080 |
or you just click on this button here, Create Project. 00:00:53.660 |
Mine is already set, so I don't need to do anything there. 00:00:59.700 |
That will take a moment for it to actually create a project. 00:01:04.500 |
But once it is done, we will see we have this project ID here. 00:01:13.540 |
And we need to initialize the Cloud Build API. 00:01:21.460 |
And we just want to click on this first link here. 00:01:32.060 |
And what we do is enable this API for this specific project. 00:01:44.660 |
So again, that will take a moment to actually install. 00:01:57.580 |
So all I'm going to type into Google is GCP SDK install. 00:02:05.200 |
And you can see, OK, we have this first link here, 00:02:11.740 |
These instructions are pretty basic for whichever 00:02:20.940 |
And then I have the G Cloud SDK on my terminal. 00:02:31.020 |
And we'll take a look at what all this actually 00:02:34.500 |
looks like in our code and actually creating our API 00:02:48.460 |
We created this in the previous video series and an article. 00:02:55.700 |
But the content of this isn't particularly important. 00:03:04.260 |
And we have these two endpoints, users and locations. 00:03:09.020 |
There isn't-- there's nothing else particularly 00:03:16.500 |
So what I want to do is open a terminal window here. 00:03:31.940 |
So I'm going to write gcloud app create project. 00:03:51.500 |
And this will just create an app inside the project 00:04:02.460 |
And you also need to select the region that you 00:04:20.700 |
in that chosen project, which is an abstract ring, which 00:04:35.100 |
Please use gcloud app deploy to deploy your first app. 00:04:43.500 |
need to install the App Engine extension for Python, 00:04:57.420 |
So we need to do gcloud components install App Engine. 00:05:11.860 |
So gcloud components install App Engine Python. 00:05:26.940 |
installed the App Engine Python in my gcloud SDK. 00:05:31.660 |
So it's just saying I already have it installed. 00:05:35.460 |
If you just install it, it will install for you. 00:05:39.780 |
So that's everything we need for deploying our app. 00:05:45.500 |
I think the last few things we need to include here-- 00:05:47.980 |
so we have our actual script, our API script. 00:05:55.540 |
So I want to-- before we actually try and deploy our API, 00:06:05.780 |
that the App Engine will be running our API within 00:06:09.380 |
by using something called Goonicorn, or Goonicorn, maybe. 00:06:14.180 |
So to install that, we just pip install Goonicorn. 00:06:20.340 |
I already have it installed, so I'm not going to. 00:06:36.620 |
And then you need to write the name of your Python API file. 00:06:55.780 |
So after this colon here, we need to include that. 00:07:15.380 |
you can see here that we have the actual location. 00:07:27.500 |
It's because we only have the two endpoints, users, 00:07:33.780 |
that we get this JSON response, which is what we'd expect. 00:07:41.980 |
We just want to see that, OK, the API is actually 00:07:59.540 |
will run in the Cloud App Engine environment. 00:08:06.660 |
The only thing is, OK, we have our Python environment here. 00:08:23.580 |
How is the App Engine going to know that we're 00:08:29.180 |
We need to tell the App Engine what our environment looks 00:08:39.980 |
So we'll come over here, and then create a new file. 00:08:49.100 |
And inside here, we just want to set our runtime. 00:08:52.500 |
So the runtime is just going to be Python 3.8. 00:09:12.420 |
And in here, I need to write which modules we're using, 00:09:21.260 |
So Flask, Flask RESTful, and we also have Pandas. 00:09:34.420 |
So we can switch back over to the terminal window. 00:09:56.780 |
directory that contains all of these files here. 00:10:15.740 |
because I am currently in another project which 00:10:22.580 |
So I need to make sure that I'm trying to deploy 00:10:38.180 |
And then in here, I need the project name from before. 00:11:15.580 |
We have the descriptor and then the target project. 00:11:23.100 |
And this URL here is where we will be deploying the API to. 00:11:31.940 |
And this can take a little bit of time for it to run. 00:11:43.780 |
is when you first deploy, gcloud will create this gcloud ignore 00:11:53.540 |
And in here, it will just ignore the typical things 00:12:00.460 |
So you have pycache here, which has been created up here. 00:12:03.580 |
So we don't need to exclude that from the upload by default, 00:12:21.260 |
I'm going to copy this, open it in the browser. 00:12:27.620 |
And we should see-- so what we saw before with this, 00:12:35.860 |
We get that not found that we saw before, so it's expected. 00:12:49.480 |
So we see that our API is now deployed to Google Cloud. 00:12:57.540 |
So if I refresh, we should see the requests pop up over here. 00:13:10.540 |
So this will give you a summary of how many requests