DEV Community

Cover image for Google Cloud Run and MySql using Ruby On Rails
Hady Rashwan
Hady Rashwan

Posted on

Google Cloud Run and MySql using Ruby On Rails

We are going to deploy a rails app into Google Cloud Run and Cloud SQL. This will make the rails app serverless.

We will follow the following steps to deploy our app.

When running the app, it took over 20 seconds to respond for the first run. Many configurations can be adjected to improve performance. Please feel free to comment with suggestions.

Setup Google Cloud SQL for Mysql.

  • Find the service network API section using the search bar.
    Search bar

  • Enable the service network API.
    Enable API

  • Go to the Cloud SQL section.
    Cloud SQL section

  • Create a new database instance.
    Click On  create instance

  • Choose Mysql.
    Choose Mysql

  • Setup the database name and password.
    Database setup

  • Click on "show more configuration options". and enable private IP.

Enable private IP

  • Click on create and wait until the process is done.
    Alt Text

  • Write down the private IP.

  • Click on Connect with cloud shell.
    Shell

  • Connect using gcloud by pressing enter in the terminal you should see a similar command shown in your shell.
    gcloud connect

  • Enter the root password that you choice earlier and press enter.

  • Should now be connected.
    Mysql shell

  • Create the database using the following command.
    create the database

  • Check that the database exists.
    database exists

Setup Cloud Run

  • Clone this repository https://github.com/hadyrashwan/ruby-on-rails-getting-started and enter the folder.

  • Generate the secret key using the following command. For demo purposes, we can use this one e2348e9aaf6da01aea29f03eef0a09a6d7c330ac2571043a126335ce62bc087f70931a54e6bf3bc8f6edff93e0fc502fb34c8af2cab31581b00507138088f230

    It is recommended to excuate the following command to get your own secret key.

$ rake secret
  • Update the deploy.sh file with your project ID.
    depoly.sh

  • Install gcloud from gcloud download page.

  • Run the following command to deploy.

./deploy.sh

When asked to enable the cloud build API to enable it and
then you may be asked where to deploy it choose us central.

  • Write down the deployment URL. deployment url

Connect Cloud Run with Cloud Sql.

  • Open Serverless VPC page.
    Serverless VPC.

  • Click on Create Connector.
    Alt Text

  • Setup the connector as follows and click Create.
    Create Connector

  • Now go to the cloud run page.
    Cloud Run Panel

  • Open your app.
    Alt Text

  • Edit deploy.
    Edit deploy

  • Edit the connection section.
    edit connectio

  • Update your environment variables. They should be as follow this table:

Key Value
BLOG_DATABASE_HOST Private IP
BLOG_DATABASE_PASSWORD Password
SECRET_KEY_BASE Secret key
  • It should look like this.

update variables

  • Finally, click Deploy.

Done

  • Your app should be ready now go to the deployment URL on your browser.

  • It may ask you to do a database migration do so.

  • In my test, the response took over 20 seconds coming from a cold start(first time hitting the app) and 254 ms afterwards.

  • I disabled the Webpack caching mechanism and used environment variables instead of the secret manger for the demo purposes.

Google Cloud docs links that helped me.

Top comments (0)