How to connect Cloud Run and Cloud SQL internally
Tags: cloud, vpc, gcp
Published: 2023-11-28 11:15:20

---
title: How to connect Cloud Run and Cloud SQL internally
published: true
description: We are going to learn how to change a Cloud SQL that it is using an external IP to connect to Cloud Run to an internal IP and a serverless Cloud Connector.
tags: cloud, vpc, gcp
cover_image: https://assets.mkdev.me/posts/covers/000/000/259/original/how-to-connect-cloud-run-and-cloud-sql-internally.png
# Use a ratio of 100:42 for best results.
# published_at: 2023-11-28 11:10 +0000
---
One of the most complex things in Google Cloud is understanding how the network works, and how services like Cloud Run that are not connected to any customer VPC can talk with components that are using a customer VPC.

To solve this issue there are two possible solutions. The first option is based on a public IP in Cloud SQL and after Cloud Run talks with this public IP. We need to remember that even with a public IP, this doesn’t mean that the connection from Cloud Run to Cloud SQL is leaving the Google network, sounds magical but packages will never leave Google.

The second way to do that is using an internal IP in the cloud SQL and connecting Cloud run to the database using this internal IP. Problem here is that as you know Cloud Run has no user VPC because is considered a serverless component, but, yes, an internal Google Cloud VPC invisible to us. Then to be able to connect the two components we need to create a Serverless VPC Connection via Private IP. It sounds complex but is actuallu simple and today we are going to learn how to do that.

But we are going to learn how to do that in a real environment, not in a fake laboratory. We are going to change a database with a Public IP connected to Cloud Run to use a private IP in our time tracking tool, Claimora.

Let's start. The first thing that we are going to check is our Cloud Run application. As you can see this beautiful tool is deployed ideally and when we go to connections we will see that the Cloud SQL is there and that currently, we don’t have any VPC connector.

![](https://mkdev.me/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcW9EIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--0b7001c09b67cdaaa6ef50b94195ce144cca8b37/HtCCRaCSI-01.png)

Now if we go to our Cloud SQL database we will see that this database is using a public IP to accept inbound connections and that is working in this way.

![](https://mkdev.me/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcXNEIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--08970e66026db8dd103f3439a5cef379acab9a0d/HtCCRaCSI-02.png)

And the last step is the serverless VPC access that we will use to connect our Cloud Run without user VPC with the Cloud SQL. If we click on create a connector, now we only need to give a name, the region where is located the sub-network that is using Cloud SQL, the VPC that Cloud SQL is using and now we choose a custom IP range, in our case 10.8.0.0/28

![](https://mkdev.me/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcXdEIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1897cc75777716cba0420f9b0fdaacf7e819df33/HtCCRaCSI-03.png)

Now if we go back to our Cloud SQL and we edit, and then go to connections, click on private IP and choose our VPC, default in our case, unclick on public IP, save and wait more than 20 minutes our database will be now working with a public IP, simple.

![](https://mkdev.me/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcTBEIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--aec64cfbf1be3e927b125e128afa4ace8b003fa3/HtCCRaCSI-04.png)

If we go back to Cloud Run and click on edit, we only need to go to connections, and in the VPC connector add the VPC connector that we created previously. After we deploy, the next step is to wait for our revision to be deployed in our service.

![](https://mkdev.me/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcTREIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--7a3680a252ca7b670645ce7d3b20e786354fd931/HtCCRaCSI-05.png)

It's simple. We can open our page, stg.claimora.com, and, magically, everything is working. How beautiful is Claimora!

![](https://mkdev.me/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcThEIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--b01b037c58850e92989b014ee559a8db105a7cc0/HtCCRaCSI-06.png)

Today we have learned how to connect a serverless component like Cloud Run with an element with a VPC and to do the magic we need a serverless VPC connector.

***

*Here's the same article in video form for your convenience:*

{% embed https://www.youtube.com/watch?v=MeynQIt3TD8 %}

*Here's [the original article](https://mkdev.me/posts/how-to-connect-cloud-run-and-cloud-sql-internally) by Pablo Inigo Sanchez on mkdev.me*