7
votes

I'm running an occasionally-up server on Google Compute Engine. I'd prefer not to pay for a static IP address and just make use of an ephemeral IP address when the instance is running. Does GCE publish into DNS a mapping from instance name to ephemeral IP that I can use or CNAME to? E.g. something like instancename.projectname.googlecompute.com with a low enough TTL that it can be resolved to point to my instance's IP address fairly soon after it starts?

I can't find anything like this in the docs, but it's very surprising to me if it doesn't exist.

2
your-ip.bc.googleusercontent.com, ex 178.125.96.3.bc.googleusercontent.comMarilu
Thanks, that's great and all, but it's not better than an IP address. I was hoping for something that would remain the same as my instance changed IP addresses, because I don't want to pay for static IP. It looks like the only way is to have software on the instance register a dynamic DNS mapping at startup.Tim Dierks

2 Answers

3
votes

Yes, the only way to reach your instance by a DNS name is to create a DNS entry yourself, such as with a static A record pointing to a static IP address or a dynamic DNS entry pointing to your ephemeral address (which is what I do for my instances).

Compute Engine does not provide a pre-defined hostname, like App Engine does, pointing to app_name.appspot.com.

My DNS provider, freedns.afraid.org, makes DDNS rather simple. For a host you want to name, create a static A record, then click the DDNS link and it give you a wget or curl command line to run from a cron job on your VM instance.

But there are plenty of other DNS providers out there who can do something similar.

2
votes

Given that the answer to the actual question is no, this answer might still help those for whom it is sufficient to be able to obtain the ip adddress through scripting rather than needing to looking it up manually.

If having the gcloud command installed and setup, outputting the ip address of a running instance is possible using a list of rather long arguments. To easily show the currently assigned address, add a function like this to your shell initialization file:

get_gce_ip()
{
  local instance="${1%%.*}"
  gcloud compute instances list --filter="$instance" \
      --format='value(networkInterfaces[0].accessConfigs[0].natIP)'
}

With that function in place, accessing any host gets convenient using ssh instancename.googl after simply adding these two lines to .ssh/config

Host *.googl
  ProxyCommand nc `get_gce_ip "%h"` %p

Clearly these are not full substitutes for DNS entries, but in some cases they might help.