Google Cloud Run and MySql using Ruby On Rails
Tags: ruby, rails, googlecloud, serverless
Published: 2020-08-24 12:42:05

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](https://github.com/hadyrashwan/ruby-on-rails-getting-started#deployment-on-gcp-as-serverless-) 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](https://dev-to-uploads.s3.amazonaws.com/i/605sxs6astw11g0sh23i.png)

- Enable the service network API.
![Enable API](https://dev-to-uploads.s3.amazonaws.com/i/20wml5g94d3mc72kj7vk.png)

- Go to the Cloud SQL section.
![Cloud SQL section](https://dev-to-uploads.s3.amazonaws.com/i/9jeyjw0dyhha0vlhxdns.png)

- Create a new database instance.
![Click On  create instance](https://dev-to-uploads.s3.amazonaws.com/i/ttuxaw25e2vlbvza0izn.png)

- Choose Mysql.
![Choose Mysql](https://dev-to-uploads.s3.amazonaws.com/i/6qaywpz33qo51hpef01h.png)

- Setup the database name and password.
![Database setup](https://dev-to-uploads.s3.amazonaws.com/i/8z7elhe6r5x8eddeh5ut.png)


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

![Enable private IP](https://dev-to-uploads.s3.amazonaws.com/i/9le6a6f6djgosg6qortd.png)

- Click on create and wait until the process is done.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nppwji36khc8ivkjnkkx.png)

- Write down the private IP.

- Click on Connect with cloud shell.
![Shell](https://dev-to-uploads.s3.amazonaws.com/i/fvl4cbhnd7ytne9w6rid.png)

- Connect using gcloud by pressing enter in the terminal you should see a similar command shown in your shell.
![gcloud connect](https://dev-to-uploads.s3.amazonaws.com/i/oz1pup4h8p8uxot2u5ov.png)

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

- Should now be connected.
![Mysql shell](https://dev-to-uploads.s3.amazonaws.com/i/xmigtbuib8ljr5dwctlg.png)

- Create the database using the following command.
![create the database ](https://dev-to-uploads.s3.amazonaws.com/i/4i1vbu9zf4uu4o86gabp.png)

- Check that the database exists.
![database exists](https://dev-to-uploads.s3.amazonaws.com/i/6qutyktjg2wqi3exhiz7.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/i/8h14iyrx2hbn8an314im.png)


- Install gcloud from [gcloud download page](https://cloud.google.com/sdk/gcloud).

- 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](https://dev-to-uploads.s3.amazonaws.com/i/4fl235pwhfzievjgi1y7.png)


## Connect Cloud Run with Cloud Sql.
- Open Serverless VPC page.
![Serverless VPC](https://dev-to-uploads.s3.amazonaws.com/i/xbpu8z1mqbtljmzc9n5n.png).

- Click on `Create Connector`.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fmalf77mtx0wxd512ai4.png)

- Setup the connector as follows and click `Create`.
![Create Connector](https://dev-to-uploads.s3.amazonaws.com/i/qfarabbbcc9w5pzd0sxl.png)

- Now go to the cloud run page.
![Cloud Run Panel](https://dev-to-uploads.s3.amazonaws.com/i/m0vcx99v5b605v8oancm.png)

- Open your app.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gxhsokqkoqd5ypw8csg3.png)


- Edit deploy.
![Edit deploy](https://dev-to-uploads.s3.amazonaws.com/i/h16k8fscovlghczeq1vb.png)

- Edit the connection section.
![edit connectio](https://dev-to-uploads.s3.amazonaws.com/i/zdvx39vim1h2ijztifa7.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/kr4aw5yggmc0qbac8en2.png)


- 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.
* [Setup Google Cloud SQL for Mysql](https://cloud.google.com/sql/docs/mysql/create-instance).
* [Configuring private IP for the database](https://cloud.google.com/sql/docs/mysql/configure-private-ip) .
* [Install gcloud](https://cloud.google.com/sdk/install).
* [Enable Cloud SQL on Cloud Run](https://cloud.google.com/sql/docs/mysql/connect-run).
* [Update your environment variables](https://cloud.google.com/run/docs/configuring/environment-variables).