Organize resources using labels

Labels are key-value pairs that can be used on Google Cloud to group relatedor associated resources. For example, on Compute Engine, you can use labelsto group VMs in categories such as production, staging, or development so thatyou can search for resources that belong to each development stage.

After adding labels to your resources, you can take advantage of thenestedfiltering feature to perform more precise searches for yourresources using labels.

Before you begin

What are labels?

A label is a key-value pair that you can assign to Google Cloud instances.They help you organize these resources and manage your costs at scale, with thegranularity you need. You can attach a label to each resource, then filter theresources based on their labels. Information about labels is forwarded to the billing system thatlets you break down your billed charges by label. With built-inbilling reports,you can filter and group costs by resource labels. You can also use labels toquerybilling data exports.

Requirements for labels

The labels applied to a resource must meet the followingrequirements:

  • Each resource can have up to 64 labels.
  • Each label must be a key-value pair.
  • Keys have a minimum length of 1 character and a maximum length of 63characters, and cannot be empty. Values can be empty, and have a maximum lengthof 63 characters.
  • Keys and values can contain only lowercase letters, numeric characters,underscores, and dashes. All characters must use UTF-8 encoding, andinternational characters are allowed. Keys must start with a lowercase letter orinternational character.
  • The key portion of a label must be unique within a single resource.However, you can use the same key with multiple resources.

These limits apply to the key and value for each label, and to theindividual Google Cloud resources that have labels. Thereis no limit on how many labels you can apply across all resourceswithin a project.

Common uses of labels

Here are some common use cases for labels:

  • Team or cost center labels: Add labels based on team orcost center to distinguish instances owned by differentteams (for example,team:research andteam:analytics). You can use thistype of label for cost accounting or budgeting.

  • Component labels: For example,component:redis,component:frontend,component:ingest, andcomponent:dashboard.

  • Environment or stage labels: For example,environment:production andenvironment:test.

  • State labels: For example,state:active,state:readytodelete, andstate:archive.

  • Ownership labels: Used to identify the teams that areresponsible for operations, for example:team:shopping-cart.

  • Virtual machine labels: A label can be attached toa virtual machine. Virtual machine tags that you defined in the past appearas a label without a value.

Note: Don't include sensitive information in labels, includingpersonally identifiable information, such as an individual's name or title.Labels are not designed to handle sensitive information.

We don't recommend creating large numbers of unique labels, such asfor timestamps or individual values for every API call.The problem with this approach is that when the values change frequently or withkeys that clutter the catalog, this makes it difficult to effectively filter andreport on resources.

Labels and tags

Labels can be used as queryable annotations for resources, but can't be usedto set conditions on policies. Tags provide a way to conditionally allow ordeny policies based on whether a resource has a specific tag, by providing fine-grainedcontrol over policies. For more information, see theTags overview.

Use labels on Compute Engine

You can apply labels to the following Compute Engine resources:

  • Virtual machine (VM) instances
  • Images
  • Persistent disks
  • Persistent disk snapshots

You can also use labels on related Google Cloud components such as thefollowing:

  • Cloud Storage buckets
  • Networking resources:
    • Forwarding rules used with internal Application Load Balancers (other load balancer forwardingrules aren't supported)
    • VPN tunnels
    • Static external IP addresses

For example, you can add the following labels as key-value pairs to yourresources:

{ "labels": {    "vmrole": "webserver",    "environment": "production",    "location": "west",...    } }

Create resources with labels

When creating a new resource, you can apply labels to the resource.

Note: You can't add labels to forwarding rules, static external IP addresses,and VPN tunnels when creating these resources. You can only add labels to thesenetworking resources when you're updating the resource. For instructions onadding labels during an update, seeAdd or update labels to existing resourcesin this document.

Console

  1. Go to the resource page that you want to create.

  2. UnderLabels, clickAdd label.

  3. Continue with the creation process.

gcloud

To add a label, use thecreate sub-command with the--labels flag. Youcan add labels to the Compute Engine resources by using thefollowinggcloud commands:

Example

gcloud compute instances create ... \    --labels webserver=backend,media=images

REST

In the API, during thePOST request to add a new resource, add thelabels property in the request body to apply labels to the new resource.

For example, the following snippet makes aPOST request to create a VMinstance with the labelswebserver:backend andmedia:images:

POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances{ "name": "myVM", "machineType": "zones/us-central1-f/machineTypes/custom-2-15360-ext",  ..., "labels": {   "webserver": "backend",   "media": "images"  }}

Add or update labels to existing resources

You can add labels or update existing labels on resources by using theGoogle Cloud console, the gcloud CLI, or the Compute Engine API.To add or update labels for forwarding rules, use the gcloud CLI orthe Compute Engine API.

Console

  1. Go to the resource page for which you want to add labels.

  2. Select the checkboxes next to the resources you want to label.

  3. To expand the labels column, clickShow info panel.

  4. In the panel, selectLabels.

  5. To add labels, clickAddlabel and add the key-value pair.

  6. To update labels, select the existing labels and modify their values.

  7. Save your changes.

gcloud

To add or change a label, use theupdate sub-command with the--update-labels flag. You can update labels for the Compute Engineresources by using the followinggcloud commands:

Example

gcloud compute disks update example-disk \    --update-labels backend=webserver,media=images

If you provide a label key that already exists, the Google Cloud CLI updatesthe existing key with the new label value. If you provide a new key, thetool adds the new key to the list of labels.

REST

To add or update labels, make aPOST request to thesetLabels method of the resource with the latestfingerprintand a full list of labels to apply:

Similar to metadata and tags, if the resource has existing labels you wantto keep, you must include those labels in the request, along with anynew labels that you want to add.

For example, the following snippet makes aPOST request to a VM instanceto set labelsenvironment:test andan-existing-tag:yes:

POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/setLabels{ "labels": {  "environment": "test",  "an-existing-tag": "yes" }, "labelFingerprint": "42WmSpB8rSM="}

View labels

You can view labels for resources by using the Google Cloud console, thegcloud CLI, or the Compute Engine API. To view labels forforwarding rules, use the gcloud CLI or the Compute Engine API.

Console

  1. Go to the resource page.

  2. Click the resource to view its details.

  3. LocateLabels.

gcloud

To view labels, use thedescribe sub-command. You can view labels for theCompute Engine resources by using thefollowinggcloud commands:

Example

gcloud compute disks describe example-disk

The output contains the labels:

...id: '5047929990219134234'kind: compute#disklabelFingerprint: GHZ1Un209U=0labels:  environment: dev  department: finance...

REST

To retrieve labels, make aGET request to the following resource:

For example, the following snippet makes aGET request to retrieve labelsfor a VM instance:

GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME

Replace the following:

  • PROJECT_ID: the project ID
  • ZONE: the zone of the VM
  • VM_NAME: the name of the VM

Get a label fingerprint for API requests

When updating or adding labels in the API, you need to provide the latest labelsfingerprint with your request, to prevent any conflicts with other requests.A fingerprint is only required for API requests; theGoogle Cloud console andthe Google Cloud CLI tool do not require a fingerprint.

Note: You can get the label fingerprint only by making a query directly to theAPI.

To get the latestlabelsFingerprint, make aGET request to the followingresources:

For example, the following snippet gets alabelsFingerprint for a VM instance:

GET https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance

The response contains thelabelFingerprint property:

200 OK{ "kind": "compute#instance", "id": "4392196237934605253", "creationTimestamp": "2015-09-15T14:05:16.475-07:00", "zone": "https://content.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f", "status": "RUNNING", ... "labels": {  "environment": "test" }, "labelFingerprint": "p1ty_9HoBk0="}

Remove a label

You can remove labels from resources by using the Google Cloud console, thegcloud CLI, or the Compute Engine API. To remove labels forforwarding rules, use the gcloud CLI or the Compute Engine API.

Console

  1. Go to the resource page for which you want to remove labels.

  2. Select the checkboxes next to the resources for which you want to removelabels.

  3. To expand the labels column, clickShow info panel.

  4. To delete a label, clickDelete.

  5. Save your changes.

gcloud

To add or change a label, use theupdate sub-command with the--remove-labels flag. You can remove labels for the Compute Engineresources by using the followinggcloud commands:

Example

gcloud compute disks update example-disk \    --remove-labels backend,media

REST

To remove labels, make aPOST request to thesetLabels method of thefollowing resource with the latestfingerprintand a full list of labels to apply:

Provide the currentlabelsFingerprint and an empty list of labels toremove all labels, or provide a list of labels you want to keep (omittingthe labels you want to remove). For example, the following snippet removesall labels from the VM:

POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/setLabels{ "labels": { }, "labelFingerprint": "42WmSpB8rSM="}

Filter searches using labels

You can search your resources and filter results by labels by using theGoogle Cloud console, the gcloud CLI, or the Compute Engine API.To filter forwarding rules by labels, use the gcloud CLI or theCompute Engine API.

Console

  1. Go to the resource page for which you want to get a filtered list ofresources.

  2. If prompted, select your project and clickContinue.

  3. In the search bar, enter your key, value, or key-value pair. Yourresults include any partial matches.

    For example, to show only resources with the labelenv:dev, youcan enter any of the following:

    • Enter the key:env
    • Enter the value:dev
    • Enter the key-value pair:env:dev

gcloud

To filter based on labels, use thelist sub-command of the followingresources with the--filter flag:

The value of--filter flag must be in thelabels.KEY=VALUE format. For example,if you wanted to filter on a label withenv as the key anddev as thevalue, you can run this command:

gcloud compute instances list \    --filter labels.env=dev

For more information about the filter syntax in the gcloud CLI,see thegcloud topic filters documentation.

REST

To filter resources, make aGET request to thelist method of thefollowing resources and include the filter field:

For example, to list all the VMs in the projectmyproject and zoneus-central1-a with a label that has a key-value pair ofenv:dev, set thefilter field tolabels.env:dev in yourlist request. If you include thefilter field as a query string parameter, use a URL-encoded parameter value,filter=labels.env%3Adev:

GET https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances?filter=labels.env%3Adev

For more information, read thefilter documentationin the Compute Engine API reference.

Relationship between VM labels and tags

In the past, labels and tags were related. For example, if you added awebserver:test label to a VM, Compute Engine automaticallyadded awebserver tag to the VM.

Now, labels and tags are separate. If you create a label on a VM,Compute Engine does not create a tag for the VM. If youneed to create a tag on a VM, you must create the tag manually.

To learn more about tags and their uses, seeTags overview.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.