How do list instances with specific label using Compute API in GCP?

Viewed 469

I am using the GCP Compute API Method for Python and trying to get a list of all instances with a specific tag. The syntax for listing instances is:

service.instances().list(project=project, zone=zone).execute()

However whenever I add filter=my-label:label or anything similar I am getting an error. Wondering if anyone else was able to get a list and if so how?

1 Answers

You can use this syntax:

request = service.instances().list(project=project, zone=zone, filter='labels.my-label=my-value')

Make sure at least one instance match the filtering criteria, otherwise response[items] will not be defined.

Related