I have couple of instances on GCE that I don't really need static addresses for, but I still need to make them accessible via dns name. Since ephemeral external ip addresses change every time an instance is restarted, I thought that I could use some sort of startup script to update dns entry for that instance in Google Cloud DNS (a bit like dyndns).
Did I miss something and there is an easier way to map ephemeral external ip addresses to a dns record via gcloud?
If not, any pointers on how to write such script would be highly appreciated!
It's been a while since you've posted this question, but i'll post my answer here for future reference.
I've had a similar need and I didn't want to use
gcloud
CLI.I've created a simple python script that does pretty much the same as the bash script above does, but uses Apache Libcloud and Google Cloud API credentials (service account and key).
You can find the code in GitHub.
I'm going to take a slight spin on the answer from @MaZe. Also, I'll show using systemd so this script starts automatically at startup if you're on Ubuntu or another distro that uses systemd.
Save it to /path/to/script.sh and start it up in systemd:
Save it in /etc/systemd/system as filename.service and enabled it with:
The following assumes that you are using Google Cloud DNS for foo.bar.com (ie. dns name "foo.bar.com.") with zone name "foo-bar-com" in the same project as your VM and that your VM has configuration option "This instance has full API access to all Google Cloud services." selected. Your VM will be called "my-vm.foo.bar.com" in DNS.
I'm sure this could be appropriately modified to work with DNS in a different project and/or more limited permissions.
Probably worth noting: this assumes you are using 'Google Cloud DNS' and not (just) 'Google Domains' registrar, if you're using the latter (to host your DNS, and not just as a registrar) then they have direct support for synthetic dynamic ip address with some dyndns like update mechanism (but they're more limited in a bunch of other ways).
Also note that for transaction to succeed there already has to be a record with the right IP and the right TTL (ie. the first time you run this you may want to delete any entry by hand via the UI, and run this code with dns_del commented out).