arrow_back

Using Ruby on Rails with Cloud SQL for PostgreSQL on Cloud Run

ログイン 参加
知識をテストして、コミュニティで共有しましょう
done
700 を超えるハンズオンラボ、スキルバッジ、コースへのアクセス

Using Ruby on Rails with Cloud SQL for PostgreSQL on Cloud Run

ラボ 1時間 20分 universal_currency_alt クレジット: 5 show_chart 中級
info このラボでは、学習をサポートする AI ツールが組み込まれている場合があります。
知識をテストして、コミュニティで共有しましょう
done
700 を超えるハンズオンラボ、スキルバッジ、コースへのアクセス

GSP943

Google Cloud self-paced labs logo

Overview

In this Qwiklab, you will learn how to deploy a sample Rails application to Cloud Run and how to integrate managed databases, object storage, encrypted secrets, and build pipelines with serverless compute.

Google Cloud SQL for PostgreSQL is a fully-managed database service that makes it easy to set up, maintain, manage, and administer your PostgreSQL relational databases on Google Cloud.

Cloud Run is a managed compute platform that enables you to run containers that are invocable via requests or events. Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.

Deploying Rails applications involves integrating multiple services together to form a cohesive project. Here's a visual example of the application you will deploy for this lab:

Architecture of Ruby on Rails with Cloud SQL for PostgreSQL on Cloud Run

The Rails site is served from Cloud Run, which uses multiple backing services to store different data types (relational database information, media assets, configuration secrets, and container images). The backend services are updated by Cloud Build as part of a build and migrate task.

What you'll learn

  • How to create and connect a Cloud SQL database to Active Record
  • How to create and use Secret Manager to store and access a Rails master key securely
  • How to host user-uploaded media and files on Cloud Storage from Active Storage
  • How to set up a Rails app with PostgreSQL
  • How to use Cloud Build to automate build and database migrations
  • How to deploy a Rails app to Cloud Run

What you'll need

  • A browser, such Chrome or Firefox
  • Familiarity with standard Linux text editors such as Vim, Emacs or Nano
  • Familiarity using Ruby

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Preparing your environment

Cloning the Rails app

The code for the Rails sample app is in the GoogleCloudPlatform/ruby-docs-samples repository on GitHub.

  1. Clone the repository:
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
  1. Go to the directory that contains the sample code and run the following commands to ensure the application is properly set up with the required gems and dependencies:
cd ruby-docs-samples/run/rails bundle install

Task 2. Preparing the backing services

This lab uses a number of Google Cloud services to provide the database, media storage, and secret storage that support the deployed Rails project. These services are deployed in a specific region. For efficiency between services, it is best that all services are deployed in the same region.

Note: If running this in a personal project, you would need to ensure the Cloud Run, Cloud SQL, Cloud Build, Secret Manager, and Compute Engine APIs are enabled and that Ruby >= 2.7 with Rails >= 6.0 is installed. Your Qwiklabs project automatically enables the necessary APIs and your Cloud Shell comes pre-installed with Ruby on Rails.

Set up a Cloud SQL for PostgreSQL instance

Rails supports multiple relational databases, including several offered by Cloud SQL. This lab uses PostgreSQL, an open source database commonly used by Rails apps.

The following sections describe the creation of a PostgreSQL instance, database, and database user for your Rails app.

  1. Define re-usable environment variables for your instance name, region, and database name:
INSTANCE_NAME=postgres-instance DATABASE_NAME=mydatabase REGION={{{project_0.default_region | Region}}}
  1. Create your PostgreSQL Instance:
gcloud sql instances create $INSTANCE_NAME \ --database-version POSTGRES_12 \ --tier db-g1-small \ --region $REGION
  1. Create the database within the recently created instance:
gcloud sql databases create $DATABASE_NAME \ --instance $INSTANCE_NAME
  1. Generate a random password for the database user, and write it to a file called dbpassword:
cat /dev/urandom | LC_ALL=C tr -dc '[:alpha:]'| fold -w 50 | head -n1 > dbpassword
  1. Create the user (named qwiklabs_user) within the recently created instance and set its password to be the content of dbpassword:
gcloud sql users create qwiklabs_user \ --instance=$INSTANCE_NAME --password=$(cat dbpassword)

Click Check my progress to verify the objective. Set up a Cloud SQL for PostgreSQL instance

Set up a Cloud Storage bucket

You can host Rails static assets and user-uploaded media in highly available object storage using Cloud Storage.

  1. Assign a Bucket Name and create a Cloud Storage bucket:
BUCKET_NAME=$DEVSHELL_PROJECT_ID-ruby gsutil mb -l {{{project_0.default_region | Region}}} gs://$BUCKET_NAME
  1. After creating a bucket, to make the uploaded images public, change the permissions of image objects to be readable by everyone:
gsutil iam ch allUsers:objectViewer gs://$BUCKET_NAME

Click Check my progress to verify the objective. Set up a Cloud Storage bucket

Task 3. Store secret values in Secret Manager

Now that the backing services are configured, Rails needs secure information, such as passwords, to access these services. Instead of putting these values directly into the Rails source code, this lab uses Rails Credentials and Secret Manager to store this information securely.

Create encrypted credentials file and store key as Secret Manager secret

Rails stores secrets in an encrypted file called config/credentials.yml.enc. The file can be decrypted with the local config/master.key or the environment variable ENV[“RAILS_MASTER_KEY”]. In the credentials file, you can store the Cloud SQL Instance database password and other access keys for external APIs.

You can store this key securely in Secret Manager. Then, you can grant Cloud Run and Cloud Build access to the key by granting access to their respective service accounts. Service accounts are identified by an email address that contains the project number.

  1. Generate the config/credentials.yml.enc file with the following command:
EDITOR="nano" bin/rails credentials:edit

The command will create a config/master.key if no master key is defined, and create a config/credentials.yml.enc file if the file does not exist. This will open a temporary file in the assigned $EDITOR with the decrypted contents for the secrets to be added.

  1. Copy and paste the necessary fields for your database at the end of the credentials file:
gcp: db_password: REPLACE_WITH_DB_PASSWORD

Do not close the editor yet. You will get the db_password value next.

  1. Open a new tab in your Cloud Shell by pressing the + button.

  2. Run this command to review the password saved in your dbpassword file:

cat ~/ruby-docs-samples/run/rails/dbpassword
  1. Copy your output from the previous command.

  2. Return to your first Cloud Shell tab with your credentials file open in an editor.

  3. Replace the REPLACE_WITH_DB_PASSWORD value with the password you copied.

Your credentials file should look similar to this:

# aws: # access_key_id: 123 # secret_access_key: 345 # Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies. secret_key_base: af9a4995ff8495e9a6bd5a035e674d1dac0fae06ac97577293fcee14147c07ba70a48db9497d3249af0896e1897ae1e12bc5b546b7c5139c333abde15ff43d20 gcp: db_password: EmbfjkFhaExrXlouRIZwjZBpAqzZlaatwqaMPPnpzkppVsXmrp Note: If your editor closed, rerun EDITOR="nano" bin/rails credentials:edit to edit your credentials file again. This command should be run in the ~/ruby-docs-samples/run/rails directory.

Secrets can be accessed with Rails.application.credentials. For example, Rails.application.credentials.secret_key_base should return the application's secret key base and Rails.application.credentials.gcp[:db_passsword] should return your database password.

  1. Press control + x and then y to save and exit your credentials.

The config/credentials/yml.enc is stored encrypted, but config/master.key can be stored in Secret Manager.

  1. Create a new secret with the value of the config/master.key:
gcloud secrets create rails_secret --data-file config/master.key

If prompted to enable API service, enter Y

  1. To confirm the creation of the secret, check it:
gcloud secrets describe rails_secret gcloud secrets versions access latest --secret rails_secret
  1. Get the value of the project number:
PROJECT_NUMBER=$(gcloud projects describe $DEVSHELL_PROJECT_ID --format='value(projectNumber)')
  1. Grant access to the secret to the Cloud Run service account:
gcloud secrets add-iam-policy-binding rails_secret \ --member serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role roles/secretmanager.secretAccessor
  1. Grant access to the secret to the Cloud Build service account:
gcloud secrets add-iam-policy-binding rails_secret \ --member serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com \ --role roles/secretmanager.secretAccessor

Connect Rails app to production database and storage

This lab uses a PostgreSQL instance as the production database and Cloud Storage as the storage backend. For Rails to connect to the newly created database and storage bucket, you need to specify all the information needed to access them in the .env file.

Our .env file contains the configuration for the application environment variables. The application will read this file using the dotenv gem. Since the secrets are stored in credentials.yml.enc and Secret Manger, the .env doesn’t have to be encrypted because it doesn’t hold any sensitive credentials.

  1. Take a look at your .env file:
cat .env

You'll notice there are key value pairs which you can define to set up your connection.

  1. To configure the Rails app to connect with the database and storage bucket, run this command:
cat << EOF > .env PRODUCTION_DB_NAME: $DATABASE_NAME PRODUCTION_DB_USERNAME: qwiklabs_user CLOUD_SQL_CONNECTION_NAME: $DEVSHELL_PROJECT_ID:$REGION:$INSTANCE_NAME GOOGLE_PROJECT_ID: $DEVSHELL_PROJECT_ID STORAGE_BUCKET_NAME: $BUCKET_NAME EOF

You filled out your .env file with the variables you used to store the values for your database, project, region, instance, and bucket earlier in the lab.

Grant Cloud Build access to Cloud SQL

  • In order for Cloud Build to apply the database migrations, you need to grant permissions for Cloud Build to access Cloud SQL:
gcloud projects add-iam-policy-binding $DEVSHELL_PROJECT_ID \ --member serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com \ --role roles/cloudsql.client

Click Check my progress to verify the objective. Create encrypted credentials file and store key as Secret Manager secret

Task 4. Deploying the app to Cloud Run

With the backing services set up, you can now deploy the app as a Cloud Run service.

  1. Change the Ruby base image in the Dockerfile to use the same version of Ruby Cloud Shell uses:
RUBY_VERSION=$(ruby -v | cut -d ' ' -f2 | cut -c1-3) sed -i "/FROM/c\FROM ruby:$RUBY_VERSION-buster" Dockerfile

These commands store a variable with the major and minor versions of your Ruby version and then reconstruct the line that specifies which image to pull using your Ruby version instead.

  1. Using the supplied cloudbuild.yaml, use Cloud Build to build the image, run the database migrations, and populate the static assets:
APP_NAME=myrubyapp gcloud builds submit --config cloudbuild.yaml \ --substitutions _SERVICE_NAME=$APP_NAME,_INSTANCE_NAME=$INSTANCE_NAME,_REGION=$REGION,_SECRET_NAME=rails_secret --timeout=20m

This first build takes a few minutes to complete. If the build timed out, increase the timeout duration by inserting --timeout=2000s into the build command above.

  1. When the build is completed and successful, deploy the Cloud Run service for the first time, setting the service region, base image, and connected Cloud SQL instance:
gcloud run deploy $APP_NAME \ --platform managed \ --region $REGION \ --image gcr.io/$DEVSHELL_PROJECT_ID/$APP_NAME \ --add-cloudsql-instances $DEVSHELL_PROJECT_ID:$REGION:$INSTANCE_NAME \ --allow-unauthenticated \ --max-instances=3

You should see output that shows the deployment succeeded, with a service URL:

Service [rubyapp] revision [rubyapp-00001-tug] has been deployed and is serving 100 percent of traffic at https://rubyapp-HASH-uc.a.run.app
  1. To see the deployed service, go to the service URL.

Web page showing an empty cat photo album

If the service URL shows Cat Photo Album, you're on the homepage of the app.

  1. Try to upload a new photo.

If the photo successfully uploads, the Rails application has been successfully deployed.

Web page showing cat photo album with one cat image

Click Check my progress to verify the objective. Deploy the app to Cloud Run.

Task 5. Understanding the code

The Rails sample app was created using standard Rails commands. The following commands create the cat_album app and use the scaffold command to generate a model, controller, and views for the Photo resource:

rails new cat_album rails generate scaffold Photo caption:text

Database connection

The config/database.yml file contains the configuration needed to access your databases in different environments (development, test, production).

For example, the production database is configured to run in Cloud SQL for PostgreSQL. The database name and username are set through environment variables in the .env file, while the database password is stored inside the config/credentials.yml.enc file, which requires the RAILS_MASTER_KEY to decrypt.

When the app runs on Cloud Run (fully managed), it connects to the PostgreSQL instance via a socket provided by the Cloud Run environment. When the app runs on your local machine, it connects to the PostgreSQL instance via Cloud SQL Auth proxy.

run/rails/config/database.yml:

production: <<: *default database: <%= ENV["PRODUCTION_DB_NAME"] %> username: <%= ENV["PRODUCTION_DB_USERNAME"] %> password: <%= Rails.application.credentials.gcp[:db_password] %> host: "<%= ENV.fetch("DB_SOCKET_DIR") { '/cloudsql' } %>/<%= ENV["CLOUD_SQL_CONNECTION_NAME"] %>"

Cloud-stored user uploaded media

Rails uses Active Storage to upload files to cloud storage providers.The config/storage.yml and config/environments/production.rb files specify Google Cloud Storage as the service provider in the production environment.

run/rails/config/storage.yml:

google: service: GCS project: <%= ENV["GOOGLE_PROJECT_ID"] %> bucket: <%= ENV["STORAGE_BUCKET_NAME"] %>

run/rails/config/environments/production.rb:

# Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :google

Automation with Cloud Build

The cloudbuild.yaml file performs not only the typical image build steps (creating the container image and pushing that to the container registry), but also the Rails database migrations. These require access to the database, which is performed by using the app-engine-exec-wrapper, a helper for Cloud SQL Auth proxy.

run/rails/cloudbuild.yaml:

steps: - id: "build image" name: "gcr.io/cloud-builders/docker" entrypoint: 'bash' args: ["-c", "docker build --build-arg MASTER_KEY=$$RAILS_KEY -t gcr.io/${PROJECT_ID}/${_SERVICE_NAME} . "] secretEnv: ["RAILS_KEY"] - id: "push image" name: "gcr.io/cloud-builders/docker" args: ["push", "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"] - id: "apply migrations" name: "gcr.io/google-appengine/exec-wrapper" entrypoint: "bash" args: [ "-c", "/buildstep/execute.sh -i gcr.io/$PROJECT_ID/${_SERVICE_NAME} -s ${PROJECT_ID}:${_REGION}:${_INSTANCE_NAME} -e RAILS_MASTER_KEY=$$RAILS_KEY -- bundle exec rails db:migrate" ] secretEnv: ["RAILS_KEY"] substitutions: _REGION: {{{project_0.default_region | Region}}} _SERVICE_NAME: rails-cat-album _INSTANCE_NAME: cat-album _SECRET_NAME: rails-master-key availableSecrets: secretManager: - versionName: projects/${PROJECT_ID}/secrets/${_SECRET_NAME}/versions/latest env: RAILS_KEY images: - "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"

Substitution variables are used in this configuration. Changing the values in the file directly means the --substitutions flag can be dropped at migration time.

In this configuration, only existing migrations in the db/migrate directory are applied. To create migration files, see Active Record Migrations.

To build the image and apply migrations, the Cloud Build configuration needs access to the RAILS_MASTER_KEY secret from Secret Manager. The availableSecrets field sets the secret version and environment variables to use for the secret. The master key secret is passed in as an argument in the build image step and then gets set to be the RAILS_MASTER_KEY in the Dockerfile when building the image.

run/rails/Dockerfile:

ARG MASTER_KEY ENV RAILS_MASTER_KEY=${MASTER_KEY}

To extend the Cloud Build configuration to include the deployment in the one configuration without having to run two commands, see Continuous deployment from git using Cloud Build. This requires IAM changes, as described.

Congratulations!

You learned how to use Cloud SQL for PostgreSQL using Ruby on Rails and then deploy it on the Cloud Run environment.

Finish your quest

A quest is a series of related labs that form a learning path. This self-paced lab is part of the Cloud SQL quest. A quest is a series of related labs that form a learning path. Completing a quest earns you a badge to recognize your achievement. You can make your badge or badges public and link to them in your online resume or social media account.

Enroll in Cloud SQL and get immediate completion credit. Refer to the Google Cloud Skills Boost catalog for all available quests.

Next steps / Learn more

Continue your learning with the given docs:

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated October 31, 2023
Lab Last Tested October 31, 2023

Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

このコンテンツは現在ご利用いただけません

利用可能になりましたら、メールでお知らせいたします

ありがとうございます。

利用可能になりましたら、メールでご連絡いたします