In this lab, you create a Google Compute Engine instance and deploy the Bookshelf application to it using a Linux shell startup script. You continue to use existing storage services you used earlier in the course, including Google Cloud Datastore, and Google Cloud Storage.

What you need

To complete this lab, you need:

Access to a supported Internet browser:

A Google Cloud Platform project

A Source Code repository containing the lab code

A Google Cloud Storage bucket

What you learn

In this lab, you:

In this lab, you explore Google Compute Engine using the Bookshelf sample application. As in the case of Google Container Engine, Compute Engine is only used in this lab to host the frontend Python web component of the application. You continue to use the existing Google Cloud Platform storage services used earlier in the course - Cloud Datastore to host structured data for books, and Cloud Storage to host book cover images.

Compute Engine lets you create and run virtual machines (known as instances), block storage disk devices, and networks on Google infrastructure. Compute Engine instances are created based on a combination of boot image and machine type. No upfront investment is required to get started and you can run thousands of virtual CPUs on infrastructure that is designed to be fast, and offer strong, consistent performance. Compute Engine instances and resources are used as the basis of many other services in Google Cloud Platform. For example, as you observed earlier in the course, Container Engine leverages Compute Engine instances.

Running Bookshelf across a cluster of multiple instances involves a few more steps than an equivalent scalable system in Container Engine. To keep things simple for this introductory lab, you deploy Bookshelf on a single Compute Engine instance. If you would like to learn more about how to deploy Bookshelf, or similar software across multiple instances, then please refer to this getting started guide for Python.

The following diagram illustrates the high-level components and resources that make up the version of Bookshelf you deploy in this lab.

You use a startup script to automate the installation and configuration of Bookshelf on your instance. Startup scripts provide a quick, and easy to use form of configuration management. Alternatively, you could use more sophisticated and open-source configuration management tools such as Puppet, Chef, Salt, and Ansible. Google Cloud Platform also supports automated creation and management of resources using Google Cloud Deployment Manager.

As you work through this lab, consider the development, operational, and support implications of deploying Bookshelf in the different environments you have tested in this course.

Review the source code for the startup script you use to deploy Bookshelf.

To review the code:

Step 1

Open the Google Cloud Platform Console, and if necessary, select the cp100 project.

Step 2

Click Tools > Source Repositories > Source Code.

Step 3

Click compute-engine > startup-scripts >

If you are not used to reading and writing Bash shell scripts, then this file may seem a little daunting at first. Each of the main steps in the script are commented so you can read through it and follow the general flow. In this step we highlight some of the sections of the script that relate specifically to Compute Engine and to the deployment of Bookshelf.

Notice on line 20 that the PROJECTID variable is assigned the output of a command. In this case the curl command is used to query your project ID from the Compute Engine metadata stored in your project.

The project ID is then used on line 45 to clone the source code for Bookshelf from your cloud repository.

Earlier in the course, you manually ran a command to install a number of requirements related to deploying Bookshelf to App Engine. On line 52 of the script these requirements are automatically installed.

The remainder of the script configures a supervisor system that manages the Bookshelf process. Most of these lines are not specific to Compute Engine and might be found in similar scripts to deploy an application in a Linux environment.

One possible disadvantage of using a startup script in this way, is that it takes a relatively long time (minutes) to start the application. A potential solution might be to create an image with most of the required software already installed and then apply any configuration changes required using a startup script.

Step 4

Leave the Cloud Platform Console window open.

Create a Compute Engine instance to host a copy of Bookshelf and use a startup script to deploy the application. Configure a firewall rule to allow access to the external IP address and port used to expose the web application.

To create an instance:

Step 1

Return to the Cloud Platform Console, and in the top right corner of the console window, click the Activate Google Cloud Shell button ().

Step 2

Type the following command to change to the directory containing the code for this lab.

cd ~/cp100/default/compute-engine

Step 3

As with Container Engine, you need to configure the Cloud SDK in Cloud Shell with a default zone in which to create your container cluster. Type the following command to view a list of available zones.

gcloud compute zones list

The output lists the available zones names, the region they are based in, and status.

Select a zone from the region geographically closest to your location.

Step 4

Type the following command to configure the Cloud SDK with a default zone and substitute your chosen value where indicated in the following command.

gcloud config set compute/zone <ZONE>

For example, if you chose the zone us-east1-b then the command would look like the following.

gcloud config set compute/zone us-east1-b

Step 5

Before you type the command in this step to create a Compute Engine instance, it is useful to review the various flags and options used:

Type the following command to create a Compute Engine instance called bookshelf.

gcloud compute instances create bookshelf \
  --image-family=debian-8 \
  --image-project=debian-cloud \
  --machine-type=g1-small \
  --scopes userinfo-email,cloud-platform \
  --metadata-from-file startup-script=startup-scripts/ \
  --tags http-server

After some delay, the output describes the progress of creating your Compute Engine instance. Once the command completes, it summarizes the details of the new instance, including the external IP address that you will use to connect to the Bookshelf application.

Step 6

You also need to create a firewall rule to allow access to the Bookshelf application on port 8080 from outside your Compute Engine network. Once again, before you type the command in this step to create the rule, it is useful to review the various flags and options used:

Type the following command (without the trailing newline characters) to create a firewall rule named default-allow-http-8080.

gcloud compute firewall-rules create default-allow-http-8080 \
  --allow tcp:8080 \
  --source-ranges \
  --target-tags http-server \
  --description "Allow port 8080 access to http-server"

After a brief delay, the output summarizes the details of the new rule.

Step 7

As you observed when reviewing the startup script earlier in the lab, there is lot of work that needs to be done by the operating system before the Bookshelf application is ready to use.

You can review the progress of the startup script by requesting the output of operating system from the serial port using the API or command line tools.

Type the following command to get the output of the serial port for the cp100 instance.

gcloud compute instances get-serial-port-output bookshelf

You should repeat this command until you see a message similar to the following sample output, indicating that the startup script has finished running, before proceeding to the next step.

<time-stamp> cp100 systemd[1]: Startup finished in 1.247s (kernel) + 1min 54.620s (userspace) = 1min 55.867s.

Step 8

Type the following command to close your Cloud Shell window.


Step 9

You need to examine the external IP address assigned to the bookshelf instance in order to test the application.

Click Compute > Compute Engine > VM instances.

Step 10

Notice that the details for bookshelf are summarized in the list of VM instances, including a link to the external IP address in the External IP column.

Step 11

In the External IP column, click the IP address for the bookshelf instance.

A new browser tab will open but you will not be able to view Bookshelf just yet.

Step 12

In the address bar, append the port number, 8080, you specified in the firewall rule.

For example, if your external IP address is, then you should modify the rule to look like the following.

Step 13

Leave the Bookshelf tab and the Cloud Platform Console open.

Generate some simple test data in the Bookshelf application, including a sample cover image, to verify that it is working as expected.

To test Bookshelf:

Step 1

Return to the Bookshelf browser tab. You may, or may not see existing book details in the bookshelf, based on any previous labs you may have completed earlier in the course.

Click Add Book.

Step 2

In the ‘Add book' page:

Download cover image

A page loads with the details you recorded for the new book, in addition to the sample cover image.

Step 3

Close the Bookshelf browser tab, but leave the Cloud Platform Console open.

As in the case of Container Engine, and before you start working through the ‘clean up' section of this lab, it is useful to take a few moments to consider the major differences between this version of Bookshelf with the App Engine version. Can you think of any others that are important to you?

Can you think of any advantages of running the different versions of Bookshelf you have tried on Container Engine compared to Compute Engine? You can find some suggestions after the comparison table.

Bookshelf version

App Engine standard runtime

Compute Engine


Sandboxed version of Python 2.7

Custom Python runtime running in Debian Linux

Access to Datastore


Google Cloud Client Library

Access to Cloud Storage

Cloud Storage Client Library

Google Cloud Client Library

Notable advantages

  • Minimal operational effort required
  • Rapid development and deployment experience
  • Highly flexible deployment environment
  • Quick migration of legacy code

Potential advantages of running the different versions of Bookshelf on Container Engine over Compute Engine include:

Can you think of any others?

For more information on comparing compute options, see:

To clean up the resources used in the lab:

Step 1

RIn the Google Cloud Platform Console's left-side menu, click Compute > Compute Engine.

Step 2

Click the check box to the left of the bookshelf instance. This will activate the Delete button at the top of the page.

Step 3

Click Delete to remove the the instance.

Step 4

In the Google Cloud Platform Console's left-side menu, click Networking > VPC Network.

Step 5

Click Firewall rules.

Step 6

In the Firewall rules section, check the default-allow-http-8080 rule, and then click Delete.

Step 7

Close all browser tabs.

©Google, Inc. or its affiliates. All rights reserved. Do not distribute.