Search code examples
pythongoogle-cloud-platformgoogle-cloud-python

google-cloud get instance id and zone with python api


How can I get the current running instanceId, and it's zone in the running Python application.

In Go and Java it's done in the following way, what is the equivilant in Python?

Go,

import metadata "cloud.google.com/go/compute/metadata"

func main(){
    println(metadata.InstanceID))
}

Java

String instanceId = com.google.cloud.MetadataConfig.getInstanceId()

Solution

  • I found an easy solution:

     requests.get("http://metadata/computeMetadata/v1/instance/id",
             headers={'Metadata-Flavor': 'Google'}).text