Instance Name:
socket.gethostname() or platform.node() should return an instance name. You may need to make out a little depending on your OS.
This worked for me on Debian and Ubuntu systems:
import socket gce_name = socket.gethostname()
However, in the CoreOS instance, the hostname command gave the instance name plus zone information, so you have to do some analysis.
Instance ID / Name / Greater (recommended):
The best way to do this is to use a metadata server . This is the easiest way to get information about an instance, and it works with almost any programming language or direct CURL. Here is a Python example using queries .
import requests metadata_server = "http://metadata/computeMetadata/v1/instance/" metadata_flavor = {'Metadata-Flavor' : 'Google'} gce_id = requests.get(metadata_server + 'id', headers = metadata_flavor).text gce_name = requests.get(metadata_server + 'hostname', headers = metadata_flavor).text gce_machine_type = requests.get(metadata_server + 'machine-type', headers = metadata_flavor).text
Again, you may need to do some analysis here, but it's really easy!
Links: How can I use Python to get the system hostname?
source share