Why is it required to provide external IPs to Cloud SQL services for authorization? - google-cloud-platform

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.

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.

Unable to move Cloud SQL to different subnet or vpc

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.

Google Cloud domain name for instance (like EC2)

On creating an EC2 instance on AWS, you can access it via IP address or a domain name provided by Amazon out of the box:
Is there a similar thing available for Google Cloud out of the box? I'm on a network that blocks IP addresses, and wildcard DNS like xip.io, so I was curious to know about it. Also, is there a specific term this is called which I'm missing?
When you create a GCP Compute Engine instance (EC2 equivalent) you can declare that you want it to have a public IP address. This is an IP that you can use over the Internet to access your instance. GCP gives you two types of IP ... static (stable) or ephemeral. A static IP is yours until you explicitly release it. There is no charge for this as long as your compute engine is running. An ephemeral IP is one which is allocated to you dynamically and may change following a restart of your compute engine instance.
GCP does not (currently ... things could always change) create a DNS entry that will resolve to your IP address over the Internet. It does create a DNS entry that can be used inside your GCP VPC network to allow one compute engine to call another within the GCP environment.
If you want to reach your Compute Engine via a DNS name it is your responsibility to create a DNS "A" record in your own DNS server. If you don't have a DNS server that you can use, then you can obtain a domain name for a few dollars and then create an instance of a GCP Cloud DNS Server and add an "A" record for your compute engine to that server.
See also:
Cloud DNS
Internal DNS
Yes, you can forumate internal dns for instance using [INSTANCE_NAME].[ZONE].c.[PROJECT_ID].internal
See the following link for further information: https://cloud.google.com/compute/docs/internal-dns
Please ensure the ports are open.
You can visit the documentation as below for your reference, hope this helps.
https://cloud.google.com/vpc/docs/firewalls

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.

Unable to connect to azure vm with internal IP

I have two vnets that are connected using a gateway. VnET1 and VNET2. VNET2 has a VM which hosts a mongodb instance. I have a webjob running within an App service environment which is deployed into a subnet within VNET1. From this subnet i am able to access the VM in VNET2 with its DNS. But i am unable to access the VM's internal IP. Any suggestions are welcome.
An internal IP address is internal to a VNET, and VNETs are isolated from one another by design. See this site for a good overview.. https://azure.microsoft.com/en-us/documentation/articles/virtual-networks-overview/. If you want to connect internally you might want to consider having multiple subnets within the same VNET instead.
At present, connecting two vnets using a gateway allows IP communication but doesn't allow DNS name resolution. In this scenario we recommend managing a local DNS server. This page shows the requirements for using your own DNS server in Azure.
Hth, Gareth