Building a serverless Python app in minutes with GCP
UPDATE: Python 3 is now available. This article is going to be rewritten asap. You can have a look at “Deploying a Python serverless function in minutes with GCP” for a more recent article.
After getting a Python “Hello World” to run with the App Engine (see previously), deploying an actual app should be as easy. Let’s create an app that returns available ebooks by a given author.
Create a New Project
First, let’s create a new project in GCP console (IAM & Admin / All projects):
The current project visible in GCP web console does not necessarily correspond to the default project in the gcloud console. In other words, a new project created from the web console will not be the default project in the gcloud console.
Check the List of Projects
$ gcloud projects list
PROJECT_ID NAME PROJECT_NUMBER
get-ebooks-by Get Ebooks By 154726733013
yet-another-gcp-test Yet Another GCP Test 539486276394
...
Very useful to quickly visualize your different projects and their IDs. Unfortunately, it does not indicate the current default project.
Check the Default Project
$ gcloud config list
Your active configuration is: [default][core]
...
project = yet-another-gcp-test
The default project is not the new one. By default, all gcloud
commands would apply to another project, which is not desired here.
Change the Default Project
$ gcloud config set project get-ebooks-by
Updated property [core/project].
If you are scripting different commands, working with different projects at the same time (e.g. continuous integration) or simply do not wish to change the default project, the --project
flag can be used (see note below when deploying): gcloud COMMAND --project PROJECT_ID
.
Prepare the Source Code
Locally on your computer:
- Create directory
get_ebooks_by
- Change current directory to
get_ebooks_by
- Add deployment file
app.yaml
- Add Python stub file
main.py
and set file encoding toUTF-8-BOM
. If your editor does not support it, encode your source inUTF-8
(no BOM) and add# -*- coding: utf-8 -*-
as the first line.
Define Entry Points
Let’s define 2 entry points for the web app:
- The main page
/
will list best-selling authors - The sub page
/author
will list all available ebooks by this author
Adapt the Python stub file accordingly:
Test Locally
$ dev_appserver.py app.yaml
Display the Author Page
Adapt the ListAuthors
class to display a list of (arbitrary) best-selling authors:
Retrieve Ebook Data
Retrieving available ebooks written by a given author can be achieved via the Google Books API, queryable/testable with an HTTP GET request. The following function gets the list of expected ebooks:
Print Ebook Data
The following function prints the ebook list in plain text:
Display the Ebook Page
Adapt the ListEbooksByAuthor
class to display the list of available ebooks:
Deploy to Production
$ gcloud app deploy
Note that, if your GCP project is not the default one, you need to be fully explicit: gcloud app deploy --project PROJECT_ID
Level Complete!
- The app is already up and running! https://get-ebooks-by.appspot.com/
- Repository: https://github.com/PicardParis/get_ebooks_by
- This app is mainly dependent on network connectivity to Google Books API and definitely not a CPU stresser. Performances are similar (or better) on GCP (try “James Patterson” for a long query) compared to tests on local 900Mbps-download/200Mbps-upload network.
Next…
Stay tuned