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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With