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:analytics). You can use this type of label for cost accounting or budgeting.
Component labels: For example,
Environment or stage labels: For example,
State labels: For example,
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.
Managing labels in Pub/Sub
In Pub/Sub, labels are often used to organize fees for a given billing account. Fees are associated with the billing account of the project that contains the requested resource, but you might want to establish additional associations. For example, a subscription that belongs to one project might be created for a topic that belongs to a different project. You can use labels to associate that topic and subscription with a common cost center.
Pub/Sub resources do not inherit labels from each other. In the example above, you would set the same label on both the topic and the subscription in order to associate those charges.
You can use the following features to apply labels to Pub/Sub resources:
- Google Cloud console: Select a resource and change to the LABELS tab.
- Pub/Sub Client Libraries (specifically Java and Python).
- The Google Cloud CLI for Pub/Sub. For instance, see the create topic command.