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

Transcript

Hi. In this video, we're going to be having a look at how we can deploy a Flask API using Google's Cloud Platform, or GCP. So it's actually a very simple process, and we're going to jump straight into it. So we want to go to Google. And the first thing I'm going to do is actually go to the GCP console.

So we just write gcp console, and we just click here. And that should bring you through to this page. If you already have an account, if you don't, you will need to obviously create an account. But once you have your account, you come over here. You can either come up here to create a project, or you just click on this button here, Create Project.

And I'm just going to call mine Flask API. You will need to set up a billing account if you haven't already. Mine is already set, so I don't need to do anything there. And I just come to Create. That will take a moment for it to actually create a project.

But once it is done, we will see we have this project ID here. We're going to need to keep that. So I'm just going to copy it now, so we're going to be using it later. And we need to initialize the Cloud Build API. So we come up here.

And we just want to click on this first link here. And what we do is enable this API for this specific project. So you need to make sure at the top here you have your Flask API project set. So again, that will take a moment to actually install. And the final thing that we need to set up is we need to install the GCP SDK.

So all I'm going to type into Google is GCP SDK install. And you can see, OK, we have this first link here, installing Cloud SDK. Click on that. And you just follow through. These instructions are pretty basic for whichever OS you're using. So for me, I'm on Mac. Click here, install this.

And then I have the G Cloud SDK on my terminal. So I'm going to switch over to VS Code. And we'll take a look at what all this actually looks like in our code and actually creating our API and deploying it. OK, so I'm in VS Code now. I have this little directory.

It's just an API. We created this in the previous video series and an article. So you can have a look at that if you want. But the content of this isn't particularly important. I just want to note we're importing some data from some files here. And we have these two endpoints, users and locations.

That's really it. There isn't-- there's nothing else particularly important in here. It's just a Flask API. So what I want to do is open a terminal window here. So I'm assuming my base environment here. And I've already installed the Cloud SDK. So that means I can use these G Cloud calls.

So I'm going to write gcloud app create project. And I need this to be equal to the project ID that I copied before. So I have abstract ring 230710. And this will just create an app inside the project that we created in the console before. So I'm going to Enter.

And you also need to select the region that you are going to be using. For me, I'm just Europe West. So I'll put 11. And that will create the App Engine app in that chosen project, which is an abstract ring, which we called Flask API. So this is the ID of that project.

The actual name is Flask API. So the app is now created. Please use gcloud app deploy to deploy your first app. Now, before we actually do that, we need to install the App Engine extension for Python, because we're on Python right now. And by default, the Python version of that is not installed or included.

So we need to do gcloud components install App Engine. Sorry, I realize you probably can't see. So gcloud components install App Engine Python. OK, that's it. And we see-- so in my case, I've already installed the App Engine Python in my gcloud SDK. So it's just saying I already have it installed.

If you just install it, it will install for you. So that's everything we need for deploying our app. I think the last few things we need to include here-- so we have our actual script, our API script. But we do need a few other things as well. So I want to-- before we actually try and deploy our API, we should just test that it actually works.

So we can simulate the same environment that the App Engine will be running our API within by using something called Goonicorn, or Goonicorn, maybe. So to install that, we just pip install Goonicorn. I already have it installed, so I'm not going to. Do it again. And once you have installed that, you just need to write Goonicorn b.

You select a host port. So I'm going to go with 8,000. And then you need to write the name of your Python API file. So mine is main. And then if you come down here, you can see that we have this app run. So where did I define app?

It should be at the top here. So app, which is the Flask app. So after this colon here, we need to include that. And that will initialize and run our API. Now you can see here-- let's make this a little smaller-- you can see here that we have the actual location.

So let's open that. And if we just open it on this first page, we will see that it just says not found. Now, this isn't actually a problem. It's because we only have the two endpoints, users, and locations. So let's open that, and you'll see that we get this JSON response, which is what we'd expect.

Now, the content of that is not important. We just want to see that, OK, the API is actually running when we run it with Goonicorn. So that's good. So let's switch back over to VS Code. We'll stop that from running. And that means that the core of the API will run in the Cloud App Engine environment.

So that's good. The only thing is, OK, we have our Python environment here. So I'm using Python 3.8 down here. And up here, I have a few modules I've imported as well. So Flask, Flask RESTful, Pandas. How is the App Engine going to know that we're using this environment?

It doesn't. We need to tell the App Engine what our environment looks like and what environment it should set up. So to do that, we need to use two files. So we'll come over here, and then create a new file. And I'm going to call it app.yaml. And inside here, we just want to set our runtime.

So the runtime is just going to be Python 3.8. That's it. Nothing else is needed there. Now I'm going to create another new file. And I'm going to call it requirements.txt. And in here, I need to write which modules we're using, which packages. Ours is pretty simple. So Flask, Flask RESTful, and we also have Pandas.

Now save that. And beyond that, there isn't anything else. So we can switch back over to the terminal window. And we can now do the gcloud deploy. So we write gcloud app deploy. And you see also just within the API folder, so this is the folder that ordered directory that contains all of these files here.

Deploy. And this will take a moment. I'm going to tell it where I want to deploy. Deploy 11. OK, and this is actually quite useful. So I've got this error that's popped up because I am currently in another project which no longer exists. So I'm getting this error. So I need to make sure that I'm trying to deploy to the right project.

So I just write gcloud app deploy. I can specify the app.yaml. And I just write project equals. And then in here, I need the project name from before. So I'm just going to go and copy that again. So it comes here. So it's this abstract ring. And I just write in here abstract ring.

OK, now run that. That should come up with this here. So we have come down here. We have the descriptor and then the target project. So it's correct. We're now going to the correct place. And this URL here is where we will be deploying the API to. So I'm going to click Continue, Yes.

And this can take a little bit of time for it to run. So just wait a moment. One thing that's actually pretty useful is when you first deploy, gcloud will create this gcloud ignore file. It's like a git ignore. And in here, it will just ignore the typical things that you do actually want to ignore.

So you have pycache here, which has been created up here. So we don't need to exclude that from the upload by default, which is useful, I think. So see how that is going. Still uploading. OK, so good to go. I'm going to copy this, open it in the browser.

And we should see-- so what we saw before with this, we should end up seeing the same. So we'll go first to that address. We get that not found that we saw before, so it's expected. And if we just go to Users, there we go. So we see that our API is now deployed to Google Cloud.

We can also see that over here. So if I refresh, we should see the requests pop up over here. Or they will do, eventually. So this will give you a summary of how many requests are made to your API. That's it for this video. I hope it's been useful.

And I will see you in the next one. Bye.