# Using Ruby on Rails with Cloud SQL for PostgreSQL on Cloud Run - GSP943

## **Overview**

In this lab, 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](https://cdn.qwiklabs.com/ed6%2BORg6hBpumoUp9K9Yjsx%2BmBzeXPO7x5UogdGl36w%3D align="left")

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](https://www.google.com/chrome/browser/desktop/) or [Firefox](https://www.mozilla.org/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 are made available to you.

This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials 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 (recommended) or private browser window to run this lab. This prevents 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:** Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that 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 dialog opens for you to select your payment method. On the left is the Lab Details pane 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.
    
    ```apache
    student-04-6705ed08575c@qwiklabs.net
    ```
    
    You can also find the Username in the Lab Details pane.
    
4. Click **Next**.
    
5. Copy the **Password** below and paste it into the **Welcome** dialog.
    
    ```apache
    URBgn3JnwlAv
    ```
    
    You can also find the Password in the Lab Details pane.
    
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 access Google Cloud products and services, click the **Navigation menu** or type the service or product name in the **Search** field.

![Navigation menu icon and Search field](https://cdn.qwiklabs.com/9Fk8NYFp3quE9mF%2FilWF6%2FlXY9OUBi3UWtb2Ne4uXNU%3D align="left")

### 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** at the top of the Google Cloud console.
    
2. Click through the following windows:
    
    * Continue through the Cloud Shell information window.
        
    * Authorize Cloud Shell to use your credentials to make Google Cloud API calls.
        

When you are connected, you are already authenticated, and the project is set to your **Project\_ID**, `qwiklabs-gcp-03-84bb4c46d415`. The output contains a line that declares the **Project\_ID** for this session:

```apache
Your Cloud Platform project in this session is set to qwiklabs-gcp-03-84bb4c46d415
```

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

3. (Optional) You can list the active account name with this command:
    

```apache
gcloud auth list
```

4. Click **Authorize**.
    

**Output:**

```apache
ACTIVE: *
ACCOUNT: student-04-6705ed08575c@qwiklabs.net

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
```

5. (Optional) You can list the project ID with this command:
    

```apache
gcloud config list project
```

**Output:**

```apache
[core]
project = qwiklabs-gcp-03-84bb4c46d415
```

**Note:** For full documentation of `gcloud`, in Google Cloud, refer to [the gcloud CLI overview guide](https://cloud.google.com/sdk/gcloud).

## **Task 1. Preparing your environment**

### Cloning the Rails app

The code for the Rails sample app is in the [GoogleCloudPlatform/ruby-docs-samples](https://github.com/GoogleCloudPlatform/ruby-docs-samples) repository on GitHub.

1. Clone the repository:
    

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

2. 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:
    

```apache
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 &gt;= 2.7 with Rails &gt;= 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:
    

```apache
INSTANCE_NAME=postgres-instance
DATABASE_NAME=mydatabase
REGION=us-east1
```

2. Create your PostgreSQL Instance:
    

```apache
gcloud sql instances create $INSTANCE_NAME \
  --database-version POSTGRES_12 \
  --tier db-g1-small \
  --region $REGION
```

3. Create the database within the recently created instance:
    

```apache
gcloud sql databases create $DATABASE_NAME \
  --instance $INSTANCE_NAME
```

4. Generate a random password for the database user, and write it to a file called `dbpassword`:
    

```apache
cat /dev/urandom | LC_ALL=C tr -dc '[:alpha:]'| fold -w 50 | head -n1 > dbpassword
```

5. Create the user (named `qwiklabs_user`) within the recently created instance and set its password to be the content of dbpassword:
    

```apache
 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

Check my progress

### 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:
    

```apache
BUCKET_NAME=$DEVSHELL_PROJECT_ID-ruby
gsutil mb -l us-east1 gs://$BUCKET_NAME
```

2. After creating a bucket, to make the uploaded images public, change the permissions of image objects to be readable by everyone:
    

```apache
gsutil iam ch allUsers:objectViewer gs://$BUCKET_NAME
```

Click *Check my progress* to verify the objective.

Set up a Cloud Storage bucket

Check my progress

## **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:
    

```apache
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.

2. Copy and paste the necessary fields for your database at the end of the credentials file:
    

```apache
gcp:
  db_password: REPLACE_WITH_DB_PASSWORD
```

Do not close the editor yet. You will get the **db\_password** value next.

3. Open a new tab in your Cloud Shell by pressing the **+** button.
    
4. Run this command to review the password saved in your `dbpassword` file:
    

```apache
cat ~/ruby-docs-samples/run/rails/dbpassword
```

5. Copy your output from the previous command.
    
6. Return to your first Cloud Shell tab with your **credentials** file open in an editor.
    
7. Replace the **REPLACE\_WITH\_DB\_PASSWORD** value with the password you copied.
    

Your credentials file should look similar to this:

```apache
# 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_password]` should return your database password.

8. 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.

9. Create a new secret with the value of the `config/master.key`:
    

```apache
gcloud secrets create rails_secret --data-file config/master.key
```

If prompted to enable API service, enter `Y`

10. To confirm the creation of the secret, check it:
    

```apache
gcloud secrets describe rails_secret

gcloud secrets versions access latest --secret rails_secret
```

11. Get the value of the project number:
    

```apache
PROJECT_NUMBER=$(gcloud projects describe $DEVSHELL_PROJECT_ID --format='value(projectNumber)')
```

12. Grant access to the secret to the Cloud Run service account:
    

```apache
gcloud secrets add-iam-policy-binding rails_secret \
  --member serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \
  --role roles/secretmanager.secretAccessor
```

13. Grant access to the secret to the Cloud Build service account:
    

```apache
 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:
    

```apache
cat .env
```

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

2. To configure the Rails app to connect with the database and storage bucket, run this command:
    

```apache
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:
    

```apache
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

Check my progress

## **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:
    

```apache
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.

2. Create a cloud-run-source-deploy Repository using the following gcloud command:
    

```apache
gcloud artifacts repositories create cloud-run-source-deploy --repository-format=docker --location=$REGION
```

3. Enable the **Cloud Run Admin API** using the following gcloud command:
    

```apache
gcloud services enable run.googleapis.com
```

4. Using the supplied `cloudbuild.yaml`, use Cloud Build to build the image, run the database migrations, and populate the static assets:
    

```apache
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.

5. 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:
    

```apache
 gcloud run deploy $APP_NAME \
     --platform managed \
     --region $REGION \
     --image $REGION-docker.pkg.dev/$DEVSHELL_PROJECT_ID/cloud-run-source-deploy/$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:

```powershell
 Service [rubyapp] revision [rubyapp-00001-tug] has been deployed
  and is serving 100 percent of traffic at https://rubyapp-HASH-uc.a.run.app
```

6. To see the deployed service, go to the service URL.
    

![Web page showing an empty cat photo album](https://cdn.qwiklabs.com/njEXPmjOkbyyuguVAUuNXYfCSE3sSsN7WyzgHqMsS%2BI%3D align="left")

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

7. 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](https://cdn.qwiklabs.com/QE8qKcnlvx4trQT%2Bw6PRSuhlcu8w4ir%2FmoEl9yI7dCU%3D align="left")

Click *Check my progress* to verify the objective.

Deploy the app to Cloud Run.

Check my progress

## **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:

```apache
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`:

```apache
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`:

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

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

```apache
# 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`:

```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: us-east1
  _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`:

```apache
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](https://cloud.google.com/run/docs/continuous-deployment-with-cloud-build). This requires IAM changes, as described.

---

## Solution of Lab

%[https://www.youtube.com/watch?v=clfiLt-GhvQ&ab_channel=QuickLab%E2%98%81%EF%B8%8F] 

```apache
curl -LO raw.githubusercontent.com/quiccklabs/Labs_solutions/refs/heads/master/Using%20Ruby%20on%20Rails%20with%20Cloud%20SQL%20for%20PostgreSQL%20on%20Cloud%20Run/quicklabgsp943.sh
sudo chmod +x quicklabgsp943.sh
./quicklabgsp943.sh
```
