I'm running a number of Google Compute Engine instances that run Python code, and I want to find the name or the ID of each instance from inside the instances.
One solution I've found is to get the internal IP of the instance using:
import socket
internal_ip = socket.gethostbyname(socket.gethostname())
Then I list all the instances:
from oauth2client.client import GoogleCredentials
from googleapiclient.discovery import build
credentials = GoogleCredentials.get_application_default()
self.compute = build('compute', 'v1', credentials=credentials)
result = self.compute.instances().list(project=project, zone=zone).execute()
Then I iterate over all the instances to check if the internal IP matches the IP of an instance:
for instance in result["items"]:
if instance["networkInterfaces"][0]["networkIP"] == internal_ip:
internal_id = instance["id"]
This works but it's a bit complicated, is there a more direct way to achieve the same thing, e.g. using Google's Python Client Library or the gcloud command line tool?
In the Google Cloud console, go to the Cloud SQL Instances page. To see the current instance state, hold the pointer over the status icon to the left of an instance name. To open the Overview page of an instance, click the instance name.
Viewing your instance ID and CRN in the IBM Cloud consoleGo to Menu > Resource List, and then click Services to browse a list of your cloud services. Click the table row. This opens the resources table side panel, where you can see the CRN and instance GUID. Click on the instance name link.
[PROJECT_NUMBER] : the unique number for the project where you created the instance. [ZONE] : the zone where the instance is located. [INSTANCE_ID] : the unique ID for the instance to which this token belongs. This ID is unique within the project and zone.
Before renaming the instance, stop it by using the gcloud compute instances stop command. After renaming, start the instance by using the gcloud compute instances start command. Replace the following: INSTANCE_NAME : name of the instance to rename.
Each instance has a property stored in the ‘sys_properties’ (System Properties) table that stores the name of your instance. The name of the property is (unsurprisingly) ‘instance_name’. You can find the value of this property via any server-side script (Business Rules, etc.) by using the ‘gs.getProperty’ method like this…
Your applications can confirm the identities of instances each time they need to transmit credentials. Google's instance authentication methods have the following benefits: Compute Engine creates a unique token each time an instance requests it, and each token expires within one hour.
FEATURESOPEN SOURCEABOUTDOCSLOGINREGISTER google_compute_instance Manages a VM instance resource within GCE. For more information see the official documentationand API. Example Usage
The name of the property is (unsurprisingly) ‘instance_name’. You can find the value of this property via any server-side script (Business Rules, etc.) by using the ‘gs.getProperty’ method like this… If you need to get the full URL for your instance you can get it from the ‘glide.servlet.uri’ property the same way…
Instance Name:
socket.gethostname()
or platform.node()
should return the name of the instance. You might have to do a bit of parsing depending on your OS.
This worked for me on Debian and Ubuntu systems:
import socket
gce_name = socket.gethostname()
However, on a CoreOS instance, the hostname
command gave the name of the instance plus the zone information, so you would have to do some parsing.
Instance ID / Name / More (Recommended):
The better way to do this is to use the Metadata server. This is the easiest way to get instance information, and works with basically any programming language or straight CURL. Here is a Python example using Requests.
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 might need to do some parsing here, but it is really straightforward!
References: How can I use Python to get the system hostname?
To get your instance name, execute the following from your VM:
curl http://metadata.google.internal/computeMetadata/v1/instance/hostname -H Metadata-Flavor:Google
and to get your instance id:
curl http://metadata.google.internal/computeMetadata/v1/instance/id -H Metadata-Flavor:Google
Check out the documentations for other available parameters: https://cloud.google.com/compute/docs/storing-retrieving-metadata#project_and_instance_metadata
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