How to programmatically create Cloud SQL instance with Private IP

2/5/2019

I'm trying to programmatically set up a new instance of Cloud SQL which has a Private IP, but the creation fails. I need a Private IP to connect from a GKE Kubernetes cluster.

Programmatically creating a new Cloud SQL instance only succeeds after a Cloud SQL with a Private IP has been created manually first. My assumption is that the manual creation sets up the necessary VPC network peering. However it fails in case no Cloud SQL instance was created manually first.

How do I programatically create the VPC network peering required to create a Cloud SQL instance which has a Private IP?

This is the request I'm making to create the Cloud SQL instance with a private ip.

const res = await client.request({
  url: `https://www.googleapis.com/sql/v1beta4/projects/${projectId}/instances`,
  method: "POST",
  data: {
    name: "my-database-8",
    settings: {
      tier: "db-f1-micro",
      ipConfiguration: {
        privateNetwork: `projects/${projectId}/global/networks/default`,
        ipv4Enabled: true
      }
    },
    databaseVersion: "MYSQL_5_7"
  }
})

I would expect the Cloud SQL instance with private networking to be created successfully, even when no Cloud SQL instance was created manually first.

-- dferber
google-cloud-platform
google-cloud-sql
google-kubernetes-engine
vpc

1 Answer

2/5/2019

My assumption is that the manual creation sets up the necessary VPC network peering.

You are correct.

How do I programatically create the VPC network peering required to create a Cloud SQL instance which has a Private IP?

It involves reserving an IP address range in your VPC, and establish peering with one of networking services. Detailed steps provided in the public doc. (look at gcloud section).

-- Zul K Irshad
Source: StackOverflow