back to index

API 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

Whisper Transcript | Transcript Only Page

00:00:00.500 | In this video, we're going to be having
00:00:02.220 | a look at how we can deploy a Flask API using Google's Cloud
00:00:06.740 | Platform, or GCP.
00:00:08.700 | So it's actually a very simple process,
00:00:11.980 | and we're going to jump straight into it.
00:00:14.140 | So we want to go to Google.
00:00:16.780 | And the first thing I'm going to do is actually go to the GCP
00:00:19.980 | console.
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:45.900 | And I'm just going to call mine Flask API.
00:00:50.380 | You will need to set up a billing account
00:00:52.260 | if you haven't already.
00:00:53.660 | Mine is already set, so I don't need to do anything there.
00:00:56.260 | And I just come to Create.
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:08.540 | We're going to need to keep that.
00:01:10.340 | So I'm just going to copy it now,
00:01:11.720 | so we're going to be using it later.
00:01:13.540 | And we need to initialize the Cloud Build API.
00:01:19.940 | So we come up here.
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:37.460 | So you need to make sure at the top
00:01:39.060 | here you have your Flask API project set.
00:01:44.660 | So again, that will take a moment to actually install.
00:01:49.660 | And the final thing that we need to set up
00:01:53.180 | is we need to install the GCP SDK.
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:07.500 | installing Cloud SDK.
00:02:08.700 | Click on that.
00:02:10.100 | And you just follow through.
00:02:11.740 | These instructions are pretty basic for whichever
00:02:14.620 | OS you're using.
00:02:15.780 | So for me, I'm on Mac.
00:02:18.380 | Click here, install this.
00:02:20.940 | And then I have the G Cloud SDK on my terminal.
00:02:29.180 | So I'm going to switch over to VS Code.
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:39.580 | and deploying it.
00:02:40.540 | OK, so I'm in VS Code now.
00:02:43.220 | I have this little directory.
00:02:46.140 | It's just an API.
00:02:48.460 | We created this in the previous video series and an article.
00:02:53.380 | So you can have a look at that if you want.
00:02:55.700 | But the content of this isn't particularly important.
00:02:58.660 | I just want to note we're importing
00:03:01.220 | some data from some files here.
00:03:04.260 | And we have these two endpoints, users and locations.
00:03:07.620 | That's really it.
00:03:09.020 | There isn't-- there's nothing else particularly
00:03:12.980 | important in here.
00:03:13.820 | It's just a Flask API.
00:03:16.500 | So what I want to do is open a terminal window here.
00:03:21.900 | So I'm assuming my base environment here.
00:03:25.060 | And I've already installed the Cloud SDK.
00:03:28.820 | So that means I can use these G Cloud calls.
00:03:31.940 | So I'm going to write gcloud app create project.
00:03:39.100 | And I need this to be equal to the project
00:03:41.620 | ID that I copied before.
00:03:43.500 | So I have abstract ring 230710.
00:03:51.500 | And this will just create an app inside the project
00:03:54.100 | that we created in the console before.
00:03:58.060 | So I'm going to Enter.
00:04:02.460 | And you also need to select the region that you
00:04:07.500 | are going to be using.
00:04:08.540 | For me, I'm just Europe West.
00:04:11.100 | So I'll put 11.
00:04:12.300 | And that will create the App Engine app
00:04:20.700 | in that chosen project, which is an abstract ring, which
00:04:24.540 | we called Flask API.
00:04:25.820 | So this is the ID of that project.
00:04:28.860 | The actual name is Flask API.
00:04:31.820 | So the app is now created.
00:04:35.100 | Please use gcloud app deploy to deploy your first app.
00:04:40.260 | Now, before we actually do that, we
00:04:43.500 | need to install the App Engine extension for Python,
00:04:48.740 | because we're on Python right now.
00:04:50.940 | And by default, the Python version of that
00:04:55.260 | is not installed or included.
00:04:57.420 | So we need to do gcloud components install App Engine.
00:05:08.860 | Sorry, I realize you probably can't see.
00:05:11.860 | So gcloud components install App Engine Python.
00:05:19.660 | OK, that's it.
00:05:24.060 | And we see-- so in my case, I've already
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:52.380 | But we do need a few other things as well.
00:05:55.540 | So I want to-- before we actually try and deploy our API,
00:06:00.140 | we should just test that it actually works.
00:06:02.620 | So we can simulate the same environment
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:22.340 | Do it again.
00:06:23.700 | And once you have installed that,
00:06:27.940 | you just need to write Goonicorn b.
00:06:30.900 | You select a host port.
00:06:33.460 | So I'm going to go with 8,000.
00:06:36.620 | And then you need to write the name of your Python API file.
00:06:43.820 | So mine is main.
00:06:46.060 | And then if you come down here, you
00:06:47.660 | can see that we have this app run.
00:06:49.900 | So where did I define app?
00:06:51.500 | It should be at the top here.
00:06:52.700 | So app, which is the Flask app.
00:06:55.780 | So after this colon here, we need to include that.
00:07:00.900 | And that will initialize and run our API.
00:07:08.740 | Now you can see here--
00:07:11.460 | let's make this a little smaller--
00:07:15.380 | you can see here that we have the actual location.
00:07:18.420 | So let's open that.
00:07:20.820 | And if we just open it on this first page,
00:07:23.460 | we will see that it just says not found.
00:07:25.420 | Now, this isn't actually a problem.
00:07:27.500 | It's because we only have the two endpoints, users,
00:07:30.900 | and locations.
00:07:31.820 | So let's open that, and you'll see
00:07:33.780 | that we get this JSON response, which is what we'd expect.
00:07:38.700 | Now, the content of that is not important.
00:07:41.980 | We just want to see that, OK, the API is actually
00:07:44.540 | running when we run it with Goonicorn.
00:07:48.460 | So that's good.
00:07:49.300 | So let's switch back over to VS Code.
00:07:52.620 | We'll stop that from running.
00:07:54.940 | And that means that the core of the API
00:07:59.540 | will run in the Cloud App Engine environment.
00:08:03.900 | So that's good.
00:08:06.660 | The only thing is, OK, we have our Python environment here.
00:08:11.100 | So I'm using Python 3.8 down here.
00:08:14.500 | And up here, I have a few modules
00:08:17.420 | I've imported as well.
00:08:18.980 | So Flask, Flask RESTful, Pandas.
00:08:23.580 | How is the App Engine going to know that we're
00:08:26.620 | using this environment?
00:08:28.500 | It doesn't.
00:08:29.180 | We need to tell the App Engine what our environment looks
00:08:33.740 | like and what environment it should set up.
00:08:36.340 | So to do that, we need to use two files.
00:08:39.980 | So we'll come over here, and then create a new file.
00:08:44.940 | And I'm going to call it app.yaml.
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:00.660 | That's it.
00:09:02.420 | Nothing else is needed there.
00:09:05.180 | Now I'm going to create another new file.
00:09:08.980 | And I'm going to call it requirements.txt.
00:09:12.420 | And in here, I need to write which modules we're using,
00:09:17.220 | which packages.
00:09:19.140 | Ours is pretty simple.
00:09:21.260 | So Flask, Flask RESTful, and we also have Pandas.
00:09:27.620 | Now save that.
00:09:29.660 | And beyond that, there isn't anything else.
00:09:34.420 | So we can switch back over to the terminal window.
00:09:38.980 | And we can now do the gcloud deploy.
00:09:44.340 | So we write gcloud app deploy.
00:09:49.740 | And you see also just within the API folder,
00:09:54.980 | so this is the folder that ordered
00:09:56.780 | directory that contains all of these files here.
00:10:01.260 | Deploy.
00:10:02.580 | And this will take a moment.
00:10:04.500 | I'm going to tell it where I want to deploy.
00:10:07.500 | Deploy 11.
00:10:10.780 | OK, and this is actually quite useful.
00:10:13.180 | So I've got this error that's popped up
00:10:15.740 | because I am currently in another project which
00:10:20.060 | no longer exists.
00:10:20.980 | So I'm getting this error.
00:10:22.580 | So I need to make sure that I'm trying to deploy
00:10:26.940 | to the right project.
00:10:27.860 | So I just write gcloud app deploy.
00:10:32.260 | I can specify the app.yaml.
00:10:35.300 | And I just write project equals.
00:10:38.180 | And then in here, I need the project name from before.
00:10:41.260 | So I'm just going to go and copy that again.
00:10:45.460 | So it comes here.
00:10:46.380 | So it's this abstract ring.
00:11:02.340 | And I just write in here abstract ring.
00:11:06.420 | OK, now run that.
00:11:08.780 | That should come up with this here.
00:11:12.380 | So we have come down here.
00:11:15.580 | We have the descriptor and then the target project.
00:11:18.940 | So it's correct.
00:11:19.940 | We're now going to the correct place.
00:11:23.100 | And this URL here is where we will be deploying the API to.
00:11:28.220 | So I'm going to click Continue, Yes.
00:11:31.940 | And this can take a little bit of time for it to run.
00:11:37.460 | So just wait a moment.
00:11:40.500 | One thing that's actually pretty useful
00:11:43.780 | is when you first deploy, gcloud will create this gcloud ignore
00:11:50.380 | file.
00:11:51.140 | It's like a git ignore.
00:11:53.540 | And in here, it will just ignore the typical things
00:11:58.340 | that you do actually want to ignore.
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:08.380 | which is useful, I think.
00:12:10.900 | So see how that is going.
00:12:13.980 | Still uploading.
00:12:16.860 | OK, so good to go.
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:31.020 | we should end up seeing the same.
00:12:32.660 | So we'll go first to that address.
00:12:35.860 | We get that not found that we saw before, so it's expected.
00:12:43.140 | And if we just go to Users, there we go.
00:12:49.480 | So we see that our API is now deployed to Google Cloud.
00:12:55.780 | We can also see that over here.
00:12:57.540 | So if I refresh, we should see the requests pop up over here.
00:13:06.740 | Or they will do, eventually.
00:13:10.540 | So this will give you a summary of how many requests
00:13:13.540 | are made to your API.
00:13:15.380 | That's it for this video.
00:13:17.820 | I hope it's been useful.
00:13:21.460 | And I will see you in the next one.