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
- The CatTime app will provide users with on-demand cat images until their mood is improved or they leave the site.
- It utilizes Sinatra and “The Cat API”.
- The source code is hosted at GitHub.
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.
1. You get a free $300 credit for signing up.
2. There’s no autocharge when the trial ends.
1. Have a Google account. Sign up for one if you don’t have one.
1. Go to:
2. Click TRY IT FREE.
3. Log in with your Google account.
4. Select your country and accept the terms:
Setting up the Google Cloud SDK
Now, we need to set up the SDK in our local dev environment.
1. Go to:
2. Click Install for Linux.
3. Open your terminal and ensure that Python 2.7 is installed on your system.
brandon@linuxacademy ~ $ python -V
4. Download the appropriate tar file for your system’s architecture (x86 or x86_64).
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
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
10. Start a new shell session for the changes to take effect.
Configuring the Project
Now, we can configure our Google Cloud Project.
1. Go to the Google Cloud Console in your browser and log in:
2. Click on the active project in the top left section (likely My First Project).
3. Click the + in the popup menu:
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 firstname.lastname@example.org: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:
2. Install the required gems:
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.
3. Back in the root of the repo, initialize it for Google Cloud:
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: [email@example.com].
Pick cloud project to use:
Please enter numeric choice or text value (must exactly match list
7. You may be prompted to enable the compute API. Type `Y` then press **Enter**.
API [compute_component] not enabled on project . 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
If you do not specify a zone via a command line flag while working
with Compute Engine resources, the default is assumed.
 Do not set default zone
Please enter numeric choice or text value (must exactly match list
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.
entrypoint: bundle exec rackup -p 8080
- The name of the App Engine runtime environment.
- We’re using the “ruby” runtime environment because we’re running a Ruby application.
- 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.
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!