It's easy to get started developing Ruby on Rails apps that run on App Engine flexible environment. Because the apps you create run on the same infrastructure that powers all of Google's products, you can be confident that they can scale to serve all of your users, whether there are a few or millions of them.
This tutorial assumes you are familiar with Rails web development. It walks you through deploying a new Rails app.
This tutorial requires Ruby 2.3.3 or newer.
Before you begin
Check off each step as you complete it.
Create a project in the Google Cloud Platform Console.
If you haven't already created a project, create one now. Projects enable you to manage all Google Cloud Platform resources for your app, including deployment, access control, billing, and services.
- Open the GCP Console.
- In the drop-down menu at the top, select Create a project.
- Click Show advanced options. Under App Engine location, select a United States location.
- Give your project a name.
- Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.
Enable billing for your project, and sign up for a free trial.
If you haven't already enabled billing for your project, enable billing now, and sign up for a free trial. Enabling billing allows the app to consume billable resources such as running instances and storing data. During your free trial period, you won't be billed for any services.
Install the Cloud SDK.
If you haven't already installed the Cloud SDK, install and initialize the Cloud SDK now. The Cloud SDK contains tools and libraries that enable you to create and manage resources on GCP.
Set up your local environment for Rails
Before you begin, take the following steps:
Alternatively, you can use Cloud Shell, which comes with Ruby, Rails, and the Cloud SDK already installed.
For additional information on installing Rails and its dependencies, see the official Getting started with Rails guide.
After you complete the prerequisites, you can create and deploy a Rails app. The following sections guide you through configuring, running, and deploying an app.
Create a new app
Scaffold a new Rails sample app.
rails new appengine_example
Go to the directory that contains the sample code.
Run the app locally
To run the new Rails app on your local computer:
Install dependencies by using Bundler.
Start a local web server.
bundle exec rails server
In your browser, go to http://localhost:3000/.
You see the "Yay! You’re on Rails!" message from the sample app displayed on the page.
Add a simple page
Now add a Welcome page to the generated Rails app.
To generate the scaffolding for a new page, create a new Rails controller named
bundle exec rails generate controller Welcome index
Open the file
app/views/welcome/index.html.erbto see the following boilerplate HTML.
<h1>Welcome#index</h1> <p>Find me in app/views/welcome/index.html.erb</p>
Modify the file however you like. For example, you can use the following content:
<h1>Welcome</h1> <p>This is a home page for a new Rails App on Google Cloud Platform!</p>
indexcontroller action as the root action for Rails. Then, whenever a user visits the Rails app, they see your welcome page.
Open the file
config/routes.rbto see the following generated content.
Rails.application.routes.draw do get 'welcome/index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
To modify this file, add
Rails.application.routes.draw do get 'welcome/index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root 'welcome#index' end
Save the file and close it. Test the Rails app as you did before.
Deploy the app to App Engine flexible environment
App Engine flexible environment uses a file called
to describe an app's deployment configuration. If this file isn't present, the
gcloud command line tool tries to guess the deployment configuration. However,
it is a good idea to provide this file because Rails requires a secret key in a
To configure the sample app for deployment to App Engine, create a new
app.yaml at the root of your sample app directory and add the
entrypoint: bundle exec rackup --port $PORT env: flex runtime: ruby
Configure the Rails secret key
When you deploy a Rails app in the production environment, set the environment
SECRET_KEY_BASE to a secret key that is used to protect
user session data. This environment variable is read in the
Generate a new secret key.
bundle exec rails secret
Copy the generated secret key. You use the secret key in the next step.
Open the file
app.yamlthat you created earlier, and add an
env_variablessets environment variables in the
productionenvironment in App Engine flexible environment. Your
app.yamlshould look like the example below, with
[SECRET_KEY]replaced with your copied secret key.
entrypoint: bundle exec rackup --port $PORT env: flex runtime: ruby env_variables: SECRET_KEY_BASE: [SECRET_KEY]
Set up an App Engine flexible environment app
If this is the first time you are deploying an app, you need to create an App Engine flexible environment app to help you select the region in which to run the Rails app. You can read more about regions and zones.
Create an App Engine app.
gcloud app create
Select a region that supports App Engine flexible environment for Ruby apps.
Deploy to App Engine flexible environment
Before you deploy, precompile your Rails assets.
RAILS_ENV=production bundle exec rails assets:precompile
After the assets finish compiling, deploy the sample.
gcloud app deploy
Wait for the message that notifies you that the update has completed. This can take several minutes.
Access the deployed Rails app
To retrieve your project ID, run
In your browser, go to
[PROJECT_ID]is the project ID you retrieved in the first step.
The following content is displayed.
This time, your request is served by the Rails app running in App Engine flexible environment.
This command deploys the app as described in
app.yaml and sets the newly
deployed version as the default version, causing it to serve all new traffic.
As the app deploys, you might see several repeated messages while the
platform checks whether the app is serving. This is normal. Wait for the
message that notifies you that the update of the app is complete.
If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources.
Read App Engine logs
Now that you have deployed your Rails app, you may want to read the logs. You
can read the app logs by using the
located in the GCP Console, or by using
gcloud app logs read.
You can learn more about
reading logs by using the Cloud SDK.
Clean up resources
After you've finished the Running Rails 5 in the App Engine tutorial, you can clean up the resources you created on Google Cloud Platform so they won't take up quota and you won't be billed for them in the future. The following sections describe how to delete or turn off these resources.
The easiest way to eliminate billing is to delete the project you created for the tutorial.
To delete the project:
- In the GCP Console, go to the Projects page.
- In the project list, select the project you want to delete and click Delete delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete an App Engine version
To delete an app version:
- In the GCP Console, go to the Versions page for App Engine.
- Select the checkbox for the non-default app version you want to delete.
- Click Delete deleteto delete the app version.
Learn how to use Cloud SQL for MySQL with Rails.
Learn how to use Cloud SQL for PostgreSQL with Rails.
Learn how to run the Ruby Bookshelf sample on Compute Engine.
Learn how to run the Ruby Bookshelf sample on GKE.