Getting the App Engine running with Flask: Part 2
This is the second part on how to deploy a Flask application using App Engine. The first part can be read here.
To deploy the application on App Engine, we have two major steps to take care of:-
- Preparing your app (which we saw earlier)
- Deploying it (which we will cover in this article)
The deploying part involves two tasks:
- Uploading the required files to a cloud storage bucket
- Using the configuration file to test and deploy the app
Again, let’s go through each part at once.
Uploading the files to a cloud storage bucket
In the console, click the navigation menu icon (the one with the three bars). Then in the navigation menu, go to Storage > Browser. A page will appear, similar to the following:
Click the “Create Bucket”. You will be asked to name the bucket. Give it a globally unique name, which we will be using for later. Here, we will name our bucket demo-app-flask1. Click “CREATE” to create the bucket.
A small detour on buckets
Cloud Storage Buckets or Buckets for short are containers used for storing files and folders in the cloud. Each bucket has a globally unique name and a specific geographic region, where the bucket and its contents are stored.
Learn more about buckets here.
Anyways, let’s continue.
After creating the bucket, click the bucket name (Here, it’s demo-app-flask1) to open it. Initially, it will be empty.
Click on “Upload Folder” and upload the entire directory consisting of all the application files we created previously. The folder, along with its contents (the files) will be uploaded.
On clicking the demo-app folder, we would be able to see all the files we created, indicating a successful upload.
Now let’s deploy the app!
Deploying the app
Enable Cloud Shell
Cloud Shell is a virtual machine with a command-line interface, which allows you to manage GCP services. We will be using Cloud Shell for hosting the app.
In the console, click the command line interpreter icon at the top right corner, as shown below.
If any prompt appears, click continue.
After a while, the console appears. Make sure the project ID and the account is set. If not, type gcloud config set project [YOUR_PROJECT_ID]
to set the project, where [YOUR_PROJECT_ID]
is the ID of your project.
Enable App Engine API
Before deploying, we have to enable the App Engine Admin API. In the navigation menu, go to APIs & services > Library.
Type “App Engine API” in the search box, and click App Engine API, when it appears on the list.
Click “Enable”. If the button “Manage” is present beside a green tick instead of “Enable”, it indicates that the API is already enabled and we are good to go.
Now, the next part involves typing a bunch of commands, which we will do step by step:-
Testing the application
We need to copy the files from the bucket we created to our Cloud Shell, where we will be testing and deploying the application. The following command does that.
gsutil -m cp -r gs://demo-app-flask1/demo-app .
The above command, copies the files, folders from the bucket to the working directory of the Cloud Shell. This can be verified by using ls
command, which is used to list files and folders in the working directory.
Next, we have to navigate to the folder where app.yaml
is located.
cd demo-app
To test the application in a local development server, type the following command.
dev_appserver.py app.yaml
Now, we could see the server up and running.
On the right side of the Cloud Shell window, click the “Web Preview” button, then click on “Preview on port 8080”
You will be redirected to a new tab, which will display your application.
Now once you are fine with the application after testing it, Press Ctrl + C to terminate the development server.
It’s now time to deploy!
Deploying the application
Oh yes, this is the part for which we waited for the most. So without any further ado, let’s reveal our marvelous web app to the world!
It all boils down to a single command — the command that does it all!
gcloud app deploy
Select the geographical region in which you want your app to reside. Please note that the choice is permanent. Here, we have selected asia-east2
The source files, the configuration file, the project, and the URL of the to be deployed app will be displayed. press Y
to continue.
And then, we would have a successful upload of files and deployment!
On a side note
If you check out the cloud storage browser, you could see a few buckets, newly created for the new app you deployed with the app engine.
To view your deployed app, type the following command, which will give the URL of the deployed app.
gcloud app browse
Navigate to the URL displayed on the console, and voila, you have the app running and ready to be flaunted! And all it takes is just a few clicks and commands.
And that concludes the two-part series of playing with App Engine!
For a similar hands-on lab for you to try, check the following lab by Qwiklabs:-