Connecting to Cloud SQL from Cloud Run
What is Cloud SQL?
Cloud SQL is a MySQL database service that is hosted on Google Cloud and does not require any software installation and maintenance because the service is provided by Google Cloud. The Google Cloud should be maintained, managed and administered by itself.Just as regular MySQL database, Google Cloud SQL also lets you create, update, configure and utilize a relational database.
What is Cloud Run?
Cloud Run is a fully managed serverless execution environment and it takes care of all infrastructure management including provisioning, configuring, scaling, and managing servers.
Process for connecting to Cloud SQL from Cloud RUN:
In order to connect to a service running in a Cloud Run instance to a Cloud SQL for that you need to have your service containerized and uploaded to the Container Registry.
If you already have a containerized service in your container registry then try to move ahead by creating a Cloud SQL instance. Otherwise you need to build a containerized service and upload in the Container Registry.
Creating a Cloud SQL instance:
You can create a Cloud SQL instance by using the gcloud command as
gcloud sql instances create [INSTANCE_NAME] --tier=[MACHINE_TYPE] --region=[REGION]
gcloud sql instances create instance1 --tier=db-n1-standard-2 --region=europe-west2
Set the password for “root@%” MySQL user:
gcloud sql users set-password root --host=% --instance [INSTANCE_NAME] --password [PASSWORD]
Once the instance creation is done then you can find the INSTANCE_CONNECTION_NAME for the instance under the Instance details page. It uses the format PROJECT_ID:REGION:INSTANCE_ID.
Also, you have to enable the Cloud SQL Admin API, if it is not enabled already.
Configuring Cloud Run
You can configure Cloud Run using the Console or Command line. Here is the configuring process for Cloud Run using the Command line:
For deploying a new container, use the following command:
gcloud run deploy --image IMAGE --add-cloudsql-instances INSTANCE-CONNECTION-NAME --set-env-vars INSTANCE_CONNECTION_NAME=”INSTANCE-CONNECTION-NAME”
For updating an existing service, use the following command:
gcloud run services update SERVICE-NAME --add-cloudsql-instances INSTANCE-CONNECTION-NAME --set-env-vars INSTANCE_CONNECTION_NAME=”INSTANCE-CONNECTION-NAME”
You need to change the following attributes in above commands while configuring the Cloud Run:
IMAGE – Replace with the image you are deploying
SERVICE-NAME – Replace with the name of your Cloud Run service
INSTANCE-CONNECTION-NAME – Replace with the instance connection name of your Cloud SQL instance, or a comma delimiter list of connection names.
Cloud Run using a Service Account to authorize your connections to Cloud SQL and the service account which requires the correct IAM permissions to successfully connect. The default service account is in the format is PROJECT_NUMBERfirstname.lastname@example.org
Cloud SQL Connection:
Once done with the Cloud SQL configuration, then will connect service to the Cloud SQL instance by using the format as /cloudsql/INSTANCE_CONNECTION_NAME
For connecting to the Cloud SQL you need to setup the configuration. For more details regarding the configuration setup refer this link
The Connections are encrypted automatically without using any additional configuration.