Building a serverless Python app in minutes with GCP

Laurent Picard
Google Cloud - Community
4 min readMar 22, 2017

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):

Let’s call this project “Get Ebooks By”. The project ID changes to “get-ebooks-by”. This will be the appspot.com subdomain.
The new project is ready in less than 30 seconds

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 to UTF-8-BOM. If your editor does not support it, encode your source in UTF-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:

  1. The main page / will list best-selling authors
  2. The sub page /author will list all available ebooks by this author

Adapt the Python stub file accordingly:

Note: the second entry “author” parameter is URL encoded and must be unquoted to get back to plain text.

Test Locally

$ dev_appserver.py app.yaml
Main Page
Sub Page

Display the Author Page

Adapt the ListAuthors class to display a list of (arbitrary) best-selling authors:

When main.py is updated, simply refresh the local page

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:

http://localhost:8080/J.+R.+R.+Tolkien

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

https://get-ebooks-by.appspot.com/
https://get-ebooks-by.appspot.com/Antoine+de+Saint-Exupéry

Level Complete!

Next…

Stay tuned

--

--

Laurent Picard
Google Cloud - Community

Tech lover, passionate about software, hardware, science and anything shaping the future • ⛅ explorer at Google • Opinions my own