Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to my Compute Engine MongoDB instance from Google Cloud Functions using the internal IP?

My Google Cloud Functions works perfectly with my MongoDB database running on a Compute Engine VM Instance, but only if I use the instance's external IP address in the Function's MongoClient connection string.

If I use the internal IP, that I would ideally prefer to use for latency and security, it times out after 30 second and shows:

document-create 4990695959542 DocumentCreate error: failed to connect to server [10.*.*.*:27017] on first connect [MongoError: connection 1 to 10.*.*.*:27017 timed out] document-create 4990695959542 
document-create 4990695959542 Function execution took 30025 ms, finished with status: 'timeout' document-create 4990695959542 
document-create 4990695959542 Function execution started document-create 4990695959542

My mongodb.conf is as follows:

# network interfaces
net:
    port: 27017
    bindIp: 127.0.0.1,10.*.*.*

Obviously, 10.*.*.* is my internal Compute Engine instance. The same IP I have used in my MongoClient's connection string in my Google Cloud Function.

If I use my external IP and in mongodb.conf I put 0.0.0.0, it works. I want Cloud Functions to connect to my MongoDB Compute Engine Instance using he VM's internal IP.

Any help?

like image 941
Lazhar Avatar asked Dec 20 '17 20:12

Lazhar


People also ask

What is the difference between internal and external IP addresses within Google Cloud?

Google Cloud uses the following labels to describe different IP address types. For example, an internal IP address is not publicly routed. An external IP address is a publicly routed IP address. You can assign an external IP address to the network interface of a Google Cloud VM.

Which command do you use to connect to a running Compute Engine instance with SSH?

To connect to an instance without an external IP address, use the gcloud compute ssh command with the --internal-ip flag. In the Google Cloud console, go to the VM Instances page and find the internal IP address for the instance that you want to connect to.

How do I find the IP address of my Google Cloud instance?

You view the internal and external IP addresses for your instance through either the Google Cloud console, the Google Cloud CLI, or the Compute Engine API. In the Google Cloud console, go to the VM instances page. If the VM instance has an external IP address, it appears under the External IP column.


1 Answers

Currently this is not possible. Cloud functions exist on a different network than your project's private VPC, therefore only public IP access is available.

There is an issue created to request this feature, you can add your request on it and subscribe for updates.

like image 73
JPS Avatar answered Nov 15 '22 17:11

JPS