App Engine

Quickstart for Ruby in the App Engine Flexible Environment


This quickstart shows you how to create a small App Engine app that displays a short message.

Before you begin

  1. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

  2. Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

  3. Go to project selector

  4. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  5. Enable the Cloud Build API.
    Enable the API

  6. Install and initialize the Cloud SDK.

Additional prerequisites

  1. Initialize your App Engine app with your project and choose its region:

  2. gcloud app create --project=[YOUR_PROJECT_ID]

  3. When prompted, select the region where you want to locate your App Engine application.

  4. Caution: You cannot change an app's region once it has been set.

  5. Install the following prerequisites:

This quickstart demonstrates a simple Ruby app written with the Sinatra web framework that you can deploy to App Engine. Although this sample uses Sinatra, you can use other frameworks such as Ruby on Rails.

Download the Hello World app

We've created a simple Hello World app for Ruby so you can quickly get a feel for deploying an app to the Google Cloud.

  1. Clone the Hello World sample app repository to your local machine.

  2. git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples

  3. Alternatively, you can download the sample as a zip file and extract it.

  4. Change to the directory that contains the sample code.

  5. cd ruby-docs-samples/appengine/hello_world

Run Hello World on your local machine

This example uses Bundler to run the Hello World app on your local computer:

  1. If you don't already have the Ruby programming language installed, download and install Ruby.
    Download and Install Ruby

  2. Install Bundler:

  3. gem install bundler

  4. Install dependencies for this project:

  5. bundle install

  6. Start a local web server.

  7. bundle exec ruby app.rb -p 8080

  8. In your web browser, enter:
    http://localhost:8080

The Hello World message from the sample app displays on the page. In your terminal window, press Ctrl+C to exit the web server.

Deploy and run Hello World on App Engine

To deploy your app to the App Engine flexible environment:

  1. Deploy the Hello World app by running the following command from the hello_world directory:

  2. gcloud app deploy

  3. Learn about the optional flags.

  4. Launch your browser to view the app at https://PROJECT_ID.REGION_ID.r.appspot.com

  5. gcloud app browse

  6. where PROJECT_ID represents your Google Cloud project ID.

This time, the page that displays the Hello World message is delivered by a web server running on an App Engine instance.

Congratulations! You've deployed your first Ruby app to App Engine flexible environment!

If you encountered any errors deploying your application, check the troubleshooting tips.

See the following sections for information about cleaning up as well as links to possible next steps that you can take.

Clean up

To avoid incurring charges, you can delete your Cloud project to stop billing for all the resources used within that project.

  1. Caution: Deleting a project has the following effects:

    • Everything in the project is deleted. If you used an existing project for this tutorial, when you delete it, you also delete any other work you've done in the project.

    • Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an appspot.com URL, delete selected resources inside the project instead of deleting the whole project.

  2. In the Cloud Console, go to the Manage resources page.
    Go to Manage resources

  3. In the project list, select the project that you want to delete, and then click Delete.

  4. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next

Learn the whole platform

Now that you know what it's like to develop and deploy App Engine apps, you can explore the rest of Google Cloud. You already have the Cloud SDK installed which gives you the tools to interact with products like Cloud SQL, Cloud Storage, Firestore, and more.

For a guided walkthrough that teaches you how to create an app that uses the entire platform, not just App Engine, check out our quickstart on creating the Bookshelf app.

Learn about the App Engine flexible environment

Here are some topics to help continue your learning about App Engine.

Hello World code review

Hello World is the simplest possible App Engine app, as it contains only one service, has only one version, and all of the code is located within the app's root directory. This section describes each of the app files in detail.

app.rb

The Hello World app is a basic one-file Sinatra app:

appengine/hello_world/app.rb

View on GitHub

require "sinatra"


get "/" do

"Hello world!"

end

app.yaml

The app.yaml file describes an app's deployment configuration:

appengine/hello_world/app.yaml

View on GitHub

runtime: ruby

env: flex

entrypoint: bundle exec ruby app.rb


# This sample incurs costs to run on the App Engine flexible environment.

# The settings below are to reduce costs during testing and are not appropriate

# for production use. For more information, see:

# https://cloud.google.com/appengine/docs/flexible/ruby/configuring-your-app-with-app-yaml

manual_scaling:

instances: 1

resources:

cpu: 1

memory_gb: 0.5

disk_size_gb: 10

Here, app.yaml specifies the runtime used by the app, and sets env: flex, specifying that the app uses the flexible environment.

Gemfile

Gemfile is used to specify the app's RubyGem dependencies, and Bundler is used to declare and install dependencies. The Hello World app requires one Ruby gem: the Sinatra web framework:

appengine/hello_world/Gemfile

View on GitHub

source "https://rubygems.org"


gem "sinatra"