After you have created a label, you can also add it as a column in your VM list view.
How do you filter with labels?
After adding labels to your resources, you can filter these resources using your labels. Just type a key or value into the Search box in the Developers Console and the filter box will auto suggest the right queries and allow you to preview results.
Common Uses
Here are some typical ways for you to use labels:
Label based on team or Cost Center – To distinguish projects owned by different teams. This can be used in cost accounting or budgeting. For example team:marketing, team:research
Label based on component – For example, component:redis, component:frontend
Label based on environment or stage – For example, environment:prod, environment:test
Label based on owner or contact– If there’s an individual who’s responsible for a resource and is the primary contact for the resource
Label based on state – For example, state:inuse, state:readyfordeletion
Elements of a label
A label is a key:value pair that can be attached to a project or a VM
Label keys and non-empty label values can contain lowercase letters, digits and hyphens, must start with a letter, and must end with a letter or digit. The regular expression is [a-z]([-a-z0-9]*[a-z0-9])
The length of labels keys and values is up to 63 characters
There can be a maximum of 64 labels per resource
VM tags you defined in the past will also show up as value-less labels
What about APIs?
You can access labels in APIs as well as the Developers Console. Below is an example that creates and filters VMs by labels. You might also want to read the API documentation for project labels and VM labels.
Setting VM labels
You can update the labels on an instance with a POST request to the setLabels path. You’ll need the project ID, zone and instance name for the request, and you’ll need an OAUTH token for authentication. The JSON request body should contain the new set of labels plus the labels fingerprint. The fingerprint is a hash of the existing labels, which is returned when you do a GET on the resource. This performs optimistic locking and prevents multiple requests from being made on the same resource.
Note that setLabels replaces ALL the labels for an instance, following the read-modify-write pattern. If you want to update a subset of the labels, you’ll want to read out the labels, make the changes needed and then call setLabels with the full list of labels.
After you've set the labels, you can write a filter such as: “labels.env eq prod” which will only return instances that have the “env” label set to “prod.”