Connect to CloudSQL using Private IP from a desktop client - google-cloud-platform

I connect to our CloudSQL instances via CloudSQL proxy from my desktop. But all the instances are currently using Public IP (with SSL)
If we configure Private IP on CLoudSQL, can we still use the Proxy to connect from desktop clients (i.e. outside the vpc)?
I understand that we won't be able to connect to instances using the Private IP explicitly from outside the vpc.
Thanks

Using proxy requires access to the same VPC that the CloudSQL is in. The proxy only lets you connect without whitelisting in firewall the client ip, but it still requires the ip to be accessible. At docs https://cloud.google.com/sql/docs/postgres/sql-proxy under Using the proxy with private IP you can find:
If the proxy is using the same VPC network as the Cloud SQL instance, and the instance has a private IP address, the proxy can connect using private IP.
I assume if you have connected your desktop to your private VPC with Cloud VPN or Interconnect, you could be able to connect to private ip with proxy. More info on this can be found here: https://cloud.google.com/vpc/docs/configure-private-google-access-hybrid.

Related

Connecting to a Cloud SQL instance from VM with public and private IPs - how to ensure the right network interface is chosen for the connection?

What I'm trying to set up:
Cloud SQL instance with private IP, Postgresql database
A VM with a public IP, but also one private IP on same VPC network as the SQL instance is on (VM, SQL instance and VPC are all in the same region)
VM has a service account with sufficient Cloud SQL client/viewer permissions
Possibility to connect from VM to SQL instance.
What happens?
Any attempt to actually use the connection, from for example psql client or db-migrate, simply hangs - for example psql --host 10.78.0.3 -U gcp-network-issue-demo-staging-db-user gcp-network-issue-demo-staging-database will not prompt for a password, just sit there.
If I remove the VM's public IP address from the setup, it connects fine. However, I need a publicly accessible VM for other services to connect to it..
I assume the psql connection attempt goes through the wrong network interface or something (this may be just my ignorance about network stuff speaking) - how can I get this working? What am I missing?
PS: this is basically same problem as Connecting to Google Cloud SQL instance on private IP from a VM with both private and public IPs fails but commenters there seem to want one Terraform-related and one connection-issue-related question.
Some screenshots:
VM IPs:
DB IPs:
Network config for VM:
Private IP config for DB instance:
This is the setup of the private network:
I don't understand why the private IP of the DB instance (10.78.0.3) is not an IP from the range of the private network (10.2.0.0-10.2.0.24, right?)..? Is that my problem?
To answer your question:
I don't understand why the private IP of the DB instance (10.78.0.3) is not an IP from the range of the private network (10.2.0.0-10.2.0.24, right?)..?
The Cloud SQL instance is assigned an IP address from the allocated range. When you setup a private services access a VPC peering is created between your VPC gcp-network-issue-demo-staging-network and the service producer VPC network that uses the allocated range 10.78.0.0/16
Also, looking at your VM network config, I see that the VM has two Nics in two different VPCs (default and gcp-network-issue-demo-staging-network). In your case, you can use only one Nic.
As a next step, make sure that your VM is using only the VPC network that you have used to create the private connection. Once that done you should be able to connect to the Cloud SQL instance IP using the command bellow:
telnet 10.78.0.3 3306

Access AWS RDS from OpenVPN using Routing

I have a RDS instance on my VPC which has Public accessibility = No
I have an OpenVPN Access Server on an EC2 instance. And I have configured two users:
First one using NAT on my OpenVPN AS to access my private networks. Using NAT, OpenVPN translate my IP, therefore when I connect to AWS RDS, I'm using a local VPC IP address. This one connects without problems to my RDS instance.
Second one uses Routing on my OpenVPN AS to access my private networks. In this case, I use a VPN IP (different to the private IPs on my VPC). This one can not connect to my RDS instance. I think is because it doesn't resolve the DNS of the endpoint.
For example: VPC -> 172.24.0.0/16
First scenario is like I use: 172.24.1.10 (a local VPC IP) to connect to RDS.
Second scenario is like I use: 172.47.224.100 (an IP from my VPN) to connect to RDS.
Is it possible to access the RDS instance through the VPN using Routing (which is the second scenario)? And how to do it? Thanks.
Update
I just confirmed the following:
If use the first scenario, I can ping an EC2 instance using Private IP and Private DNS.
If I use the second scenario, I can ping using Private IP, but I can't ping using the Private DNS.

Accessing an HTTP Server on a Private Subnet via a Public Subnet

On AWS, I have an EC2 instance with an HTTP server running on port 8888 inside a private subnet, and an EC2 instance that can communicate with it in a public subnet. How can I access the HTTP server running inside the private subnet via the public subnet? I understand that I can use the public EC2 instance as a proxy server via an SSH tunnel, but I am unable to access the server from my browser.
This is the command I am running on the public server:
ssh -R 0.0.0.0:8888:localhost:8888 -N user#private_server
When I access public_server:8888 on my browser, I am unable to connect. I may need to set up the browser to access traffic through a proxy server (via some chrome extension) but this is not what I want. Is there a way to set up the public server such that no work needs to be done by the client to access the server via their browser?
I did some more research, and realized I was looking for a reverse proxy, and could just simplify further by using an Application Load Balancer running in the public subnet that could interface with the instance running in the private subnet.

PgAdmin access to AWS Postgres instance in private subnet

I'm trying to create a realistic network setup for a multi-tiered web application. I've created a new VPC within AWS with 1 x public subnet & 2 x private subnet. I then created a Postgres instance within the private subnet and set it to not publicly accessible. This adds an extra layer of security around the database, but how do I then access the database from my local IP?
I created a security group & assigned my IP to the inbound rules & assigned that to the DB instance during creation:
But I still have no way of connecting to it? Do I need to create a VPN and connect to my VPC via the VPN and then connect to the DB instance? Within the proposed architecture, how do you connect to the DB?
What I'm trying to achieve is an architecture which will allow me to create Lambda functions which communicate with the DB via the API Gateway and serve data to a web frontend. So I want the DB protected via the private subnet. But I also want to be able to connect directly to the DB from my local laptop.
At the moment - the RDS instance is running in the VPC, but I don't know how to connect to it. DoI need to set up an Internet Gateway / VPN / EC2 instance and jump to the DB?
You have implemented excellent security by placing the Amazon RDS database into a private subnet. This means it is not accessible from the Internet, which blocks off the majority of potential security threats.
However, it also means that you cannot connect to it from the Internet.
The most common method to achieve your goals is to launch an Amazon EC2 instance in the public subnet and use it as a Bastion or Jump Box:
You SSH into the Bastion
The Bastion can then connect you to other resources within the VPC
Since you merely wish to connect to a database (as opposed to logging into another server), the best method is to use SSH with port forwarding.
In Windows, this can be done using your SSH client -- for example, if you are using PuTTY, you can configure Tunnelling. See: How to Configure an SSH Tunnel on PuTTY
For Mac/Linux, use this command:
ssh -i YOUR-KEYPAIR.pem -L 5555:RDS-ENDPOINT:5432 ec2-user#YOUR-BASTION-SERVER
You then point the SQL client on your laptop to: localhost:5555
The 5555 can be any number you wish. It is merely the "local port" on your own computer that will be used to forward traffic to the remote computer.
The RDS-ENDPOINT is the Endpoint of your RDS database as supplied in the RDS console. It will be similar to: db.cnrffgvaxtw8.us-west-2.rds.amazonaws.com
BASTION-SERVER is the IP address or DNS name of the Jump Box you will use to connect
Then, any traffic sent to localhost:5555 from your SQL client will be automatically sent over the SSH connection to the Bastion/Jump Box, which will then forward it to port 5432 on the RDS database. The traffic will be encrypted across the SSH connection, and establishment of the connection requires an SSH keypair.
I referred a lot of articles and videos to find this answer.
yes, you can connect to rds instances in private subnets
we have two ways to connect
With server: By using ec2 in the public subnet and using it as a bastion host. we can connect to pg admin by ssh tunneling
Serverless: By using client VPN endpoint. create a client VPN endpoint and associate the subnets and allow the internet to the private subnets. and then download the configuration file and install open VPN GUI and import the configuration file and add the keys and then connect the open VPN. Now try to connect to pgadmin, it will connect.
for steps: https://docs.google.com/document/d/1rSpA_kCGtwXOTIP2wwHSELf7j9KbXyQ3pVFveNBihv4/edit )

Access Database Server hosted in AWS private subnet

I have gone through AWS VPC(Virtual Private Cloud) where I can have public, Private and VPN-only Subnets.
With this, I want to host a Database in Private or VPN-only subnet.
Now that, Private and VPN-only subnet can not have Internet traffic, I wonder if I can access my database server from remote machine (not an EC2 instance nut any random machine on internet) ?
So All I want to know that, how I can access my Database server from random machine using VPN connection ? So the only users those have VPN connections can access the database server. I want this for development purpose.
Any comments links will be helpful.
Note: I am aware that I can do this with VPN EC2 instance, but I don want to have one and directly connect to Database server from remote machine which has VPN connection.
Thanks
When you create a private VPC, all of the subnets you create within it have internal network access to each other by default in their route tables. However you would need to configure a NAT or Bastion instance within the VPC that is public to act as the gateway to the private database.
The private database would need to have an appropriate security group attached to it to allow it to be accessed by the public machine. See about VPCs and Security Groups here.