Creating and managing labels

Stay organized with collections Save and categorize content based on your preferences.

This page gives a brief overview of labels and explains how to create and manage labels using the Resource Manager API and the Google Cloud console.

What are labels?

A label is a key-value pair that helps you organize your Google Cloud resources. You can attach a label to each resource, then filter the resources based on their labels. Information about labels is forwarded to the billing system, so you can break down your billed charges by label.

Labels can be used as queryable annotations for resources, but can't be used to set conditions on policies. Tags provide a way to conditionally allow or deny policies based on whether a resource has a specific tag. For more information, see the Tags overview.

Labels and tags

Tags are a separate tool that are used to provide fine-grained control over policies. For more information about tags, see the Tags overview.

Common uses of labels

We do not recommend creating large numbers of unique labels, such as for timestamps or individual values for every API call. Here are some common use cases for labels:

  • Team or cost center labels: Add labels based on team or cost center to distinguish resources owned by different teams (for example, team:research and team:analytics). You can use this type of label for cost accounting or budgeting.

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

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

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

  • Virtual machine labels: A label can be attached to a virtual machine. Virtual machine tags that you defined in the past will appear as a label without a value.

Requirements for labels

The labels applied to a resource must meet the following requirements:

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

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

Permissions

You can add or modify a label by calling the projects.projects.patch() method. This requires the resourcemanager.projects.update permission.

You can view a project's label by calling the projects.get() method. This requires the resourcemanager.projects.get permission.

Create labels

Console

To add labels to a single project:

  1. Open the Labels page in the Google Cloud console.

    Open the Labels page

  2. Select your project from the Select a project drop-down.

  3. To add a new label entry, click + Add label and enter a label key and value for each label you want to add.

  4. When you're finished adding labels, click Save.

To add labels for more than one project at the same time:

  1. Open the Manage resources page in the Google Cloud console.

    Open the Manage resources page

  2. On the Manage resources page, select the projects for which you want to add labels.

  3. In the info panel, in the Labels tab, click + Add label and enter a label key and value for each label you want to add.

  4. When you're finished adding labels, click Save.

After you add labels, you can filter projects by typing a label key or value in the filter box above the projects list. The filter box will suggest keys and values so you can preview results.

REST

To create a new project with a label, set the labels field when you create the project.

Request:

POST https://cloudresourcemanager.googleapis.com/v3/projects

{
 "labels": {
  "color": "red"
 },
 "name": "myproject",
 "projectId": "our-project-123"
}

Response:


{
 "projectNumber": "333197460082",
 "projectId": "our-project-123",
 "lifecycleState": "ACTIVE",
 "name": "myproject",
 "labels": {
  "color": "red"
 },
 "createTime": "2016-01-12T22:18:28.633Z",
}

To add labels for existing projects:

Once you add labels, you can filter projects based on labels. For details about using labels to filter projects, see Listing Projects.

Update labels

Console

To update labels for a single project:

  1. Open the Labels page in the Google Cloud console.

    Open the Labels page

  2. Select your project from the Select a project drop-down.

  3. Update the labels for your project:

    1. To edit a label, click the value that you want to edit, then make your desired changes.
    2. To delete a label, hold the pointer over the key or value, then click .
  4. When you're finished updating labels, click Save.

To update labels for more than one project at the same time:

  1. Open the Manage resources page in the Google Cloud console.

    Open the Manage resources page

  2. On the Manage resources page, select the projects for which you want to update labels.

  3. In the info panel, click the Labels tab and update labels for the selected projects:

    1. To edit a label, click the value that you want to edit, then make your desired changes.
    2. To delete a label, hold the pointer over the key or value, then click .
  4. When you're finished updating labels, click Save.

REST

To update a project's labels, do the following:

  • Get the project object using the projects.get() method.

  • Modify the labels that you want to update.

  • Call the projects.patch method.

The following example updates the label color:blue:

The following code snippet changes the value of the color label from blue to red.

Request:

PATCH https://cloudresourcemanager.googleapis.com/v3/{project.name=PROJECT_NAME}
{
  updateMask=labels
}

Where PROJECT_NAME is the name of the project you want to update.

Request JSON body:

{
    "labels":
      {
        "color": "red"
      }
}

Response:

{
    "projects": [
    {
        "name": "projects/123456789012",
        "parent": "folders/123456789012",
        "projectId": "my-project",
        "state": "ACTIVE",
        "displayName": "PROJECT_NAME"
        "createTime": "2013-11-13T20:31:53.308Z"
        "updateTime": "2013-11-13T20:35:42.308Z"
        "etag": "BwWUlZ6XEfY="
        "labels": {
            "color": "red"
        },
    }
    ]
}

Services that support labels

The following Google Cloud services support labels:

  • AI Platform
  • App Engine (both Flexible and Standard environments)
  • Artifact Registry
  • BigQuery
  • Certificate Authority Service
  • Cloud Bigtable
  • Cloud Composer
  • Cloud Deployment Manager
  • Cloud Functions
  • Cloud Healthcare API
  • Cloud Key Management Service
  • Cloud Run
  • Cloud Spanner
  • Cloud SQL
  • Cloud Storage
  • Cloud Translation
  • Compute Engine
  • Dataflow
  • Dataproc
  • Filestore
  • Google Kubernetes Engine
  • Memorystore
  • Networking
  • Pub/Sub
  • reCAPTCHA Enterprise
  • Resource Manager (projects only)
  • Transcoder API
  • Vertex AI