Beembo
Beembo

Reputation: 317

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

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?

Upvotes: 2

Views: 1046

Answers (1)

Neo Anderson
Neo Anderson

Reputation: 6380

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.

Upvotes: 3

Related Questions