Deploying a Sinatra Web App with the Google Cloud App Engine

What We’re Doing

You are a Systems Engineer for CatTime, an advocacy group that promotes “well-being through the use of feline-related media.”

The Web Dev team has a minimum viable product that your manager wants to deploy as soon as possible. You know about the benefits of a cloud-based infrastructure and decide to give the Google Cloud App Engine a shot. You’ve heard it’s one of the fastest ways to deploy a web application with many self-scaling capabilities built-in.

About the App

Sinatra

The application is built with the Sinatra web framework. It’s great for building simple, lightweight Ruby web applications.

The app is comprised primarily of a .rb file, and an .erb template. The .rb file provides the primary functionality, while the erb template provides the dynamically-produced HTML.

The Cat API

On fetch, a function is invoked that retrieves a cat image from the amazing “The Cat API” through the “cat_api” rubygem.

Alright, let’s deploy this thing!

Signing up for the Google Cloud Platform

The first thing we need to do is sign up for Google Cloud.

Notes:

1. You get a free $300 credit for signing up.

2. There’s no autocharge when the trial ends.

Prerequisites:

1. Have a Google account. Sign up for one if you don’t have one.

Procedure

1. Go to:

https://cloud.google.com

2. Click TRY IT FREE.


user_71763_5911e7fef167f.png


3. Log in with your Google account.

4. Select your country and accept the terms:

user_71763_5911f139a8c21.png


Setting up the Google Cloud SDK

Now, we need to set up the SDK in our local dev environment.

1. Go to:

https://cloud.google.com/sdk/

2. Click Install for Linux.

user_71763_5911f11865cca.png

3. Open your terminal and ensure that Python 2.7 is installed on your system.

brandon@linuxacademy ~ $ python -V
Python 2.7.12

4. Download the appropriate tar file for your system’s architecture (x86 or x86_64).


user_71763_5911f100c538b.png

5. Extract the tar.gz file on your file system.

brandon@linuxacademy ~/Downloads $ tar -xvf google-cloud-sdk-152.0.0-linux-x86_64.tar.gz

6. Run the installer to configure your bash profile for the SDK.

brandon@linuxacademy ~/Downloads $ ./google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!

7. Choose to participate (or not) in Google’s usage data collection.

To help improve the quality of this product, we collect anonymized usage
data and anonymized stacktraces when crashes are encountered; additional
information is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose to opt
out of this collection now (by choosing 'N' at the below prompt), or at
any time in the future by running the following command:
gcloud config set disable_usage_reporting true
Do you want to help improve the Google Cloud SDK (Y/n)?

8. Choose to update your $PATH with the SDK shell commands.

Modify profile to update your $PATH and enable shell command
completion? (Y/n)?

9. Enter the path to an rc file to update. The default is likely the one to use.

The Google Cloud SDK installer will now prompt you to update an rc
file to bring the Google Cloud CLIs into your environment.
Enter a path to an rc file to update, or leave blank to use
[/home/brandon/.bashrc]:

10. Start a new shell session for the changes to take effect.

Configuring the Project

Now, we can configure our Google Cloud Project.

Procedure

1. Go to the Google Cloud Console in your browser and log in:

https://cloud.google.com

2. Click on the active project in the top left section (likely My First Project).

user_71763_5911f09685840.png

3. Click the + in the popup menu:

user_71763_5911f03dc95d5.png

4. Set a project name.

  • Use something like: “cattime-sinatra-your-la-username”
  • Be sure to substitute your LA username.

5. Take note of the Project ID. GCP will automatically generate one for you. You can change this if you like, but it must be unique to the world of GCP projects.

6. Click Create.

7. There will be a bit of a delay in the creation of the project. After a few moments, you should be able to select it from the Select a Project menu at the top left of the web console view. The Select view will show Recent by default. You may have to click on the All tab to reach your newly created project.

Adding a Source Code Repository and Initializing the Google Cloud SDK

1. Clone the repo to your local dev environment:

git clone git@github.com:labmac/content-gcp-essentials-cattime-sinatra.git

2. [Optional] Run the app locally to test. (Note: You’ll need to have Ruby 2.3.3 installed. That’s the current version for the Google App Engine Runtime Environment at the time of writing this guide.)

1. Move into the repo:

cd content-gcp-essentials-cattime-sinatra/

2. Install the required gems:

bundle install

3. Start the app:

bundle exec rackup -p 8080

4. Open a browser, and go to the following URL. You should see the app and a cat.

http://70f3e302.ngrok.io

3. Back in the root of the repo, initialize it for Google Cloud:

gcloud init

4. You’ll be prompted to log in. Type Y and press Enter.

Welcome! This command will take you through the configuration of gcloud.
Your current configuration has been set to: [default]You can skip diagnostics next time by using the following flag:
gcloud init --skip-diagnostics
Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic (1/1 checks) passed.
You must log in to continue. Would you like to log in (Y/n)?

5. You’ll then be taken to a browser window that will ask you to log in to your Google account. Log in to the appropriate account.

6. Once you’re logged in, go back to the terminal. Type the number for your corresponding project (3 in the case below), and press Enter.

You are logged in as: [brandon@linuxacademy.com].
Pick cloud project to use:
[1] project1
[2] project2
[3] cattime-sinatra-brandon423
Please enter numeric choice or text value (must exactly match list
item):

7. You may be prompted to enable the compute API. Type `Y` then press **Enter**.

API [compute_component] not enabled on project [267241611092]. Would
you like to enable and retry? (Y/n)?

8. You’ll then be prompted to configure Google Compute Engine. Type `Y` then **Enter**.

Do you want to configure Google Compute Engine
(https://cloud.google.com/compute) settings (Y/n)? Y

9. Now, you’ll select the regional zone you’d like your Compute resources to exist in. Use a zone that’s close in proximity from this following list:

  • europe-west (supports standard and flexible)
  • us-central (supports standard and flexible)
  • us-east1 (supports standard and flexible)
  • asia-northeast1 (supports standard and flexible)

The above are currently the only zones that support the App Engine. You’ll see more options, like in the list below, but please choose one of the above four.

Which Google Compute Engine zone would you like to use as project
default?
If you do not specify a zone via a command line flag while working
with Compute Engine resources, the default is assumed.
[1] asia-east1-a
[2] asia-east1-b
[3] asia-east1-c
[4] asia-northeast1-b
[5] asia-northeast1-a
[6] asia-northeast1-c
[7] asia-southeast1-a
[8] asia-southeast1-b
[9] europe-west1-c
[10] europe-west1-b
[11] europe-west1-d
[12] us-central1-c
[13] us-central1-f
[14] us-central1-a
[15] us-central1-b
[16] us-east1-d
[17] us-east1-b
[18] us-east1-c
[19] us-west1-a
[20] us-west1-b
[21] Do not set default zone
Please enter numeric choice or text value (must exactly match list
item): 16

10. The Google Cloud SDK should now be set up for your project.

The app.yaml File

You’ll notice the app.yaml file in the repository’s root path. This contains the Google App Engine configuration. It’s pretty simple for this project.

app.yaml

runtime: ruby
env: flex
entrypoint: bundle exec rackup -p 8080

runtime

  • The name of the App Engine runtime environment.
  • We’re using the “ruby” runtime environment because we’re running a Ruby application.
env

  • The type of App Engine Environment
  • We’re utilizing a “flex” as opposed to a “standard” environment

A “standard” environment is as the name indicates, standardized. It allows for some out of the box API functionality that the Flexible environment doesn’t offer. However, it is limited to Java, Python, Go, and PHP, so we’re using “flex” for our Ruby application.

entrypoint
This is the command used to actually run the application.

Deploy the App

1. From the root path of the repo, run the following command:

gcloud app deploy

This will take a bit of time, especially on first deploy. You’ll see something like this once it’s deployed:

Updating service [default]...done.
Deployed service [default] to [https://cattime-sinatra-brandon423.appspot.com]You can stream logs from the command line by running:
$ gcloud app logs tail -s default
To view your application in the web browser run:
$ gcloud app browse

2. View your freshly deployed application at the above-mentioned URL. There may be a delay of a few minutes for it to become available.

You’re All Done!

That’s all there is to it. Your app is now (hopefully) improving the mood and well-being of cat lovers everywhere!

user_71763_5911eff5c51f2.png_800.jpg

Looking for team training?