Member-only story
How to access private cloudsql using 1st Gen & 2nd Gen Google Cloud Functions
In this post , we will see how to access a private cloudsql using cloud function. The engine selected for this private cloudsql instance is “MySQL” Engine. To start with, first we need to select a VPC network where the cloud sql will use the private service access to get a private ip.
Private services access is implemented as a VPC peering connection between your VPC network and the underlying Google Cloud VPC network where your Cloud SQL instance resides. You will get the private ip for cloud sql from the internal CIDR range provided by you in the private service access section.
Lets go ahead and see the steps involved.
Provision a custom VPC with a subnet of CIDR range 10.110.0.0/24 in the us-east4 region. (you can select the region as per your choice, but make sure your cloudsql and cloud function are all in the same region of choice. The same goes for the CIDR range too, you can give the RFC-1918 CIDR range of your choice, but make sure there is no overlapping of CIDR ranges.)
Firewall is enabled for all incoming TCP connections ( In a production scenario you need to enable the TCP for certain ports and only for certain source ranges)