Unable to move Cloud SQL to different subnet or vpc - google-cloud-platform

I am trying to move Cloud SQL from one subnet to another on my GCP project. Basically, I created a Cloud SQL instance, which used google managed service connection, and the IP range is allocated by Google default.
Where I want to switch to my own CIDR I setup via managed service connections created.
I am following the steps from Changing the private IP address of an existing Cloud SQL instance
Trying to switch to a temporary network/vpc before attaching back to my custom VPC with my own managed service connections.
$gcloud --project=myprj beta sql instances patch mydbid --network=tmp_vpc --no-assign-ip
The following message will be used for the patch API method. {"name": "mydbid", "project": "myprj", "settings": {"ipConfiguration": {"ipv4Enabled": false, "privateNetwork": "https://compute.googleapis.com/compute/v1/projects/myprj/global/networks/tmp_vpc"}}} ERROR: (gcloud.beta.sql.instances.patch) HTTPError 400: This operation is not valid for this instance.

I am assuming you are using a Shared VPC.
Currently, it is not possible to assign a Private IP from a Shared VPC network to an existing Cloud SQL instance.
This operation is only possible when creating a new instance, as explained in the
Quick reference for Private IP topics
You can create Cloud SQL instances with private IP addresses in a
Shared VPC network. However, you cannot assign a private IP address in
a Shared VPC network to an existing Cloud SQL instance.
Here you can find a Feature Request opened for your use case.

Related

Cloud Sql Proxy Private IP External application

Simple question: Is there any way to connect to a GCP SQL Database under private IP through cloud sql proxy from an external application ? (local development environment)
I followed every step in the official tutorial to configure cloud sql proxy with all requirements, but all connections fail in a sort of timeout.
In order to connect to a Cloud SQL instance using only private IP through the Cloud SQL proxy will be to install the proxy within a resource (could be for example a Compute Engine instance) with access to the same VPC network as the Cloud SQL instance. Since your local development environment might not comply to that requirement the connection will fail.
You could move your local development environment to a compute engine instance located within the same VPC network as your Cloud SQL instance or temporarily enable Public IP on the instance and authorize only your IP through the authorized network options and use the Cloud SQL proxy to gain access to the instance.
The Cloud SQL proxy will work with a private IP address as long as it can reach the private IP address.
See the "Connecting from an external source" section on the Configuring Private IP page for a list of steps to set up a VPN that can provide access to your VPC.

How do I share a Google Cloud SQL privately outside its region

Not finding any solid answers that fit within the scope of my question.
I have a custom VPC established to allow communication between my SQL server and instance groups. My issues are limited connectivity to the SQL server from instances within the same region as the server itself.
Basically, I created a Cloud SQL instance within us-east region.
When I create a VM Instance within the same region as the SQL instance, I have no issues connecting to its private IP.
mysql -h{PRIVATE_IP} -uroot
However, running this same command from an instance in a different region results in a timeout. Both instances are configured the exact same and within the same VPC network.
I let Google allocate IP address pool for me when I created the IP. Created the private network connection within my custom VPC settings and tried tutorials provided in the Cloud Console documentation itself with no luck.
Any help getting me on the right track would be much appreciated. Thank you.
As documented, if you want to connect Cloud SQL from a Compute Engine instance using private IP, your instance must be in the same region as your Cloud SQL instance.
Keep in mind that your Cloud SQL instances are not created in your VPC network, those are created in a Google internal VPC network that then is peered to your VPC network.
Hope this helps!

Accessing Cloud SQL from another GCP project

I want to connect to Cloud SQL from a different GCP project.
Cloud SQL is location in ProjectSQL and a VPC network is there in ProjectSQL project with name sql_vpc
There is another project ProjectDataflow and this has a vpc dataflow_vpc. I want to connect to cloudSQL from ProjectSQL with the VM launched in ProjectDataflow project
Things I have tried with success and failure.
Private ACCESS:
VPC Peering:
Enable Private IP access in Cloud with the vpc sql_vpc
Creating VPC peering between dataflow_vpc and sql_vpc
This solution does not work because you can not access the Peered Network.
https://cloud.google.com/sql/docs/mysql/private-ip
Status: FAILED
Shared Network
As per doc I can create the CloudSQL in shared VPC network, that says I
have to create the CloudSQL in host project, and to access the Cloud
SQL from VM instance, it has be in the same network as of authorized
private ip network of Cloud SQL
Status: NOT TRIED but looks to be Negative
Public Access:
Create a Cloud NAT in ProjectDataflow with dataflow_vpc with manual IP
Use the Cloud NAT public ip to whitelist in CloudSQL instance
Now I can access the CloudSQL from project ProjectDataflow using CloudSQL Public IP
STATUS: Success
Please share your experience accessing Cloud SQL from another project.
Is there any best practice to connect cloud SQL from another gcp project?
EDIT:
Newer instances seem to be having this option enabled by default and there's no need to contact support anymore. However, if after all the process, the setup is still not working, it may be needed to contact support.
IMPORTANT: The VPC peering option will not work anymore, as stated in the documentation, more precisely in the Considerations topic. Then the only available option to achieve it is using Shared VPCs
The process of interconnecting a Cloud SQL with another GCP project it is pretty straightforward following the documentation. The only thing you need to take into consideration in order to make it work is that you will have to request Google Cloud Support to enable custom routes for your Cloud SQL speckle umbrella instance in which your Cloud SQL is running under otherwise you won’t be able to access your Cloud SQL within your GCP project.
The following steps will work for you:
-Configuring VPC for Cloud SQL instance
Inside the project where you have your Cloud SQL instance, create a
VPC network with the ip address range of your desire. Choose the same
zone for the VPC in which your instance is located.
-Configuring VPC for GCP project
Now switch to the project where your CloudDataflow instance is located
and follow the same process. Create the VPC network being careful that
the IP ranges do not collide between each other. You can use the following tool to
check if the IP addresses range collide. Also take into consideration
that both VPC networks must be in the same zone.
-Connecting VPC of both projects with peering
Once both VPC networks are created it is needed to configure the VPC
network peering from both projects. From the Cloud SQL instance side,
configure the peering specifying the project and VPC network name to
connect with and also select the option to export custom routes. This
way the other part of the peering, in this case your GCP project, will
have visibility of your Cloud SQL instance. Now, from the GCP project
side, configure the peering specifying the Cloud SQL project name and
the VPC network name to connect with. The same way we did with the
Cloud SQL peering, we have to set up the peering to import custom
routes as it will receive exported routes coming from the other side
of the connection, which in our case is your Cloud SQL instance.
Here you can check more information about importing and exporting routes between any VPC network peerings.
-Request Google Cloud Support to enable for you the exchange custom routes for your Cloud SQL
Reach Google Cloud Support and ask them to enable the exchange of
custom routes for your speckle-umbrella VPC network associated with
your instance that is automatically created upon the Cloud SQL
instance is created.
Take into consideration that this last step is very important, all SQL projects run under the umbrella project, hence without requesting Google Cloud Support to enable the exchange custom routes for your instance this will never work.
Shared VPC
As for Shared VPC, the only thing you need to take into consideration is that you need to enable the option once creating your Cloud SQL instance as you can’t add it afterwards.
You will find a configuration guide for Shared VPC in the following link.

Why is it required to provide external IPs to Cloud SQL services for authorization?

I am taking the Google's GCP Fundamentals: Core Infrastructure course on Coursera. In the demonstration video of the Google Storage module, the presenter authorizes a compute engine instance to access a MySQL instance via it's external IP address.
Aren't these two resources part of the same VPC if they are part of the same project ? Why can't this authorization be done using the vm instance's internal IP address ?
Aren't these two resources part of the same VPC if they are part of
the same project ?
A Cloud SQL instance isn't created in one of your project's VPC network but in a Google-managed project, within its own network.
What happens when you enable private IP is that this network will be peered with the network of your choice in your project, where your Compute Engine instance resides:
You can then connect to the Cloud SQL instance from your VM via the internal IP address. The VM is considered trusted if your network configuration allows it to reach the Cloud SQL instance.
When you set an external IP address on the Cloud SQL instance, it means that the instance is accessible to the internet and the connection needs to be authorized. One way to do it is to whitelist the IP address of the caller as you mentioned. This works well if the caller's IP doesn't change. Another (easier) option is to connect via the cloud_sql_proxy, which handles authorization and encryption for you. You then don't need to whitelist the IP.

How to programmatically create Cloud SQL instance with Private IP

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.
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).