Finally moved this site to Cloud Run
I was determined to lower the GCP cost for my nearly-zero-traffic blog site, it's still ridiculous to spend 50 dollars to host a blog site for nobody to see. In order to do that, I would have to use the lowest performant settings in GCP even if it means total disaster for the performance of my blog site.
Create a Cloud SQL for Cloud Run application
To find the lowest cost to create a Cloud SQL, I used Google Cloud Pricing Calculator. And In the calcualtor, I saw that there is a "db-f1-micro" instance type, which is absolutely the lowest cost for SQL instances. I suppose this is because it's a shared cpu core instance where I don't get to have a dedicated CPU core for mysql process. That's fine. We don't have that much traffic to feed mysql database, might as well just share my computational power with other people.
The instance type seems to dictate the price of Cloud SQL. Once I picked the "db-f1-micro" instance type, the cost estimate became very low. In order to achieve the lowest cost, I only spined up one instance without any backup or replication. This brought the cost down to less than 9 dollars every month.
Connect Spring Boot application with Cloud SQL
Once the mysql database is setup and the database and tables are created, I just have to create a Cloud Run service to run my Spring Boot application.
Since my Spring Boot application was configured to run in App Engine before, there are some code and configurations that have to be updated.
First, we have to remove "war" plugin in gradle build file and remove the "providedRuntime" dependency.
Second, we have to add "org.springframework.cloud:spring-cloud-gcp-starter-sql-mysql:1.2.5.RELEASE" implementation dependency so the Spring Boot application would know how to connect to Cloud SQL.
Third, we have to add
database=mysql
spring.cloud.gcp.sql.enabled=true
spring.cloud.gcp.sql.database-name=blogs
spring.cloud.gcp.sql.instance-connection-name=blogging-343322:us-west1:serverless-db
in the application.properties file.
With all the change above, getting my Spring Boot Application running as Cloud Run service is just as simple as running "gcloud run deploy" in my repository.
Aiming at $10 each month
After all the changes, we disable the App Engine service and remove the VM that hosts mysql service, hopefully, with Cloud Run's automatic scale in and out, my monthly cost would be below 10 dollars.