命名空间名称,将组织的数字 ID 或项目的 ID 添加到标记键的短名称。例如,为组织创建的标记键可以具有命名空间名称 123456789012/env。如需了解如何获取组织 ID,请参阅获取组织资源 ID。为项目创建的标记键可以具有命名空间名称 myproject/env。如需了解如何获取项目 ID,请参阅识别项目。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eTags, which are key-value pairs, can be attached to Google Cloud resources to control access through conditional granting or denying of IAM roles and permissions.\u003c/p\u003e\n"],["\u003cp\u003eResources inherit tag values from parent levels (organization, folders, project), allowing for centralized access management across various Google Cloud resources.\u003c/p\u003e\n"],["\u003cp\u003eTag identifiers include permanent IDs (unique and non-reusable), short names (unique within the project/organization), and namespaced names (short name plus organization/project ID), each offering different trade-offs in terms of manageability and risk.\u003c/p\u003e\n"],["\u003cp\u003eTags are commonly used for environment separation (e.g., development, production), categorizing component types (e.g., frontend, batch), and project identification to help organize access.\u003c/p\u003e\n"],["\u003cp\u003eIAM conditions can be configured to check resource tags using either permanent IDs or namespaced/short names, with permanent IDs preferred for minimizing risk of unintended access due to tag name reuse.\u003c/p\u003e\n"]]],[],null,["# Tags and conditional access\n\nThis page describes how to use tags with Identity and Access Management (IAM) to help you\ncontrol access to your Google Cloud resources.\n\nTo learn more about tags, see [Tags overview](/resource-manager/docs/tags/tags-overview).\n\nOverview of tags\n----------------\n\nA tag is a key-value pair that is attached to a\n[Google Cloud resource](/resource-manager/docs/tags/tags-supported-services#supported_service_resources). You can\n[conditionally grant IAM roles](/iam/docs/conditions-overview) or\n[conditionally deny IAM permissions](/iam/docs/deny-overview) based on\nwhether a resource has a specific tag.\n\nResources [inherit tag values](/resource-manager/docs/tags/tags-overview#inheritance) from their parent organization,\nfolders, and project. As a result, you can use tags to manage access to any\nGoogle Cloud resource.\n\nThese are some common use cases for managing access with tags:\n\n- **Development, staging, and production environments.** For example, you could add the tags `environment: dev` to your development environment and `environment: prod` to your production environment.\n- **Component types.** For example, you could add the tags `component: frontend` to front-end resources and `component: batch` to resources for batch processing.\n- **Project names.** For example, if your team is working on a project with the codename Atlas, you could add the tag `project: atlas` to the team's development resources.\n\n\n| **Important:** Do not include sensitive information in tags. Sensitive information includes personally identifiable information (PII), such as an individual's name or job title. Tags are not intended to contain sensitive information.\n\n\u003cbr /\u003e\n\nTag definitions and identifiers\n-------------------------------\n\nBefore you attach tags to resources, you must define the key for the tag, as\nwell as the values that the tag allows. You can create these definitions at\nthe organization or project level. You use [Resource Manager](/resource-manager/docs) to\nmanage tag definitions. To learn more, see\n[Creating and defining a new tag](/resource-manager/docs/tags/tags-creating-and-managing#creating).\n\nEach tag key and value has a few different identifiers:\n\n\n- A *permanent ID* , which is globally unique and can never be reused. For example, a tag key could have the permanent ID `tagKeys/123456789012`, and a tag value could have the permanent ID `tagValues/567890123456`.\n- A *short name* . The short name for each key must be unique within the project or organization under which the key is defined, and the short name for each value must be unique for its associated key. For example, a tag key could have the short name `env`, and a tag value could have the short name `prod`.\n- A *namespaced name* , which adds your organization's numeric ID or project's ID to the short name of a tag key. For example, a tag key created for an organization could have the namespaced name `123456789012/env`. To learn how to get your organization ID, see [Getting\n your organization resource ID](/resource-manager/docs/creating-managing-organization#retrieving_your_organization_id). A tag key created for a project could have the namespaced name `myproject/env`. To learn how to get your project ID, see [Identifying\n projects](/resource-manager/docs/creating-managing-projects#identifying_projects).\n\n\u003cbr /\u003e\n\nAs explained on this page, after you attach tags to a resource, you can\n[write conditions to grant access based on tags](#control-access). To write a\ncondition, you must choose which type of identifier to use in the condition.\nFollow these guidelines to choose between them:\n\n- **To experiment with tags,** consider using the namespaced name (for keys) and the short name (for values). These identifiers are easier to understand and remember, especially as you get started.\n- **If you manage your configuration declaratively,** using a tool such as\n Terraform, consider using the namespaced name (for keys) and the short name\n (for values). You can reuse these identifiers over time, which means that a\n declarative tool can delete and recreate them, and your conditions will\n continue to work.\n\n This approach comes with a tradeoff: Suppose you delete a tag key or value,\n then create a new key or value with the same name but a different meaning.\n If your condition refers to the namespaced name or short name, then the\n condition continues to apply to the new key or value.\n\n In some cases, this behavior might cause principals to get access that you\n did not intend for them to have.\n- **To help minimize risk,** consider using permanent IDs, which can never be\n reused.\n\n Here's why permanent IDs help minimize risk: Suppose you delete a tag key or\n value, then create a new key or value with the same name but a different\n meaning. If your condition refers to the permanent ID, then the condition\n does not apply to the new key or value. As a result, principals are less\n likely to get access that you did not intend for them to have.\n\n One drawback is that if you delete and recreate keys and values, and you\n want to preserve principals' access, you must also update your allow\n policies to refer to the new permanent IDs.\n\nAccess to tagged resources\n--------------------------\n\nYou can use tags with IAM Conditions to grant a role\nconditionally, depending on the tags that are attached to or inherited by a\nresource. If a condition evaluates to `true`, then access is granted; otherwise,\naccess is not granted. To learn more, see the\n[overview of IAM Conditions](/iam/docs/conditions-overview).\n\n\nCertain\nareas of the Google Cloud console don't recognize allow policy role bindings\nwith tag-based conditions. As a result, if you have a role with a tag-based\ncondition, then the Google Cloud console might incorrectly prevent you from\nperforming certain actions. If you encounter this issue, then use an alternate\nmethod, such as the gcloud CLI, to perform the action.\n\n\n| **Note:** Conditions that check the tags for a resource *and* other\n| attributes, such as the resource name or the timestamp of the request, are in preview.\n| Such conditions are subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1). For more information, see\n| the [launch stage descriptions](/products#product-launch-stages).\n|\n|\n| Conditions that check the tags for a resource and *don't* check any other attributes are\n| generally available.\n\n\u003cbr /\u003e\n\nThe following sections show examples of condition expressions that check the\ntags on a resource. The condition calls different functions depending on whether\nit checks the permanent ID or the short name. To learn more about these\nfunctions, see [Resource tags](/iam/docs/conditions-attribute-reference#resource-tags).\n\n### Conditions that use permanent IDs\n\nThis condition grants a role on resources with the tag\n`tagKeys/123456789012: tagValues/567890123456`: \n\n resource.matchTagId('tagKeys/123456789012', 'tagValues/567890123456')\n\nThis condition grants a role on resources that have any tag with the key\n`tagKeys/123456789012`, regardless of its value: \n\n resource.hasTagKeyId('tagKeys/123456789012')\n\nThis condition grants a role on resources that have both the tag\n`tagKeys/123456789012: tagValues/567890123456`, and any tag that uses the\nkey `tagKeys/987654321098`: \n\n resource.matchTagId('tagKeys/123456789012', 'tagValues/567890123456') &&\n resource.hasTagKeyId('tagKeys/987654321098')\n\n### Conditions that use namespaced names and short names\n\nThis condition grants a role on resources with the tag `env: prod`, indicating\nthat the resource is in a production environment: \n\n resource.matchTag('123456789012/env', 'prod')\n\nThis condition grants a role on resources that have any tag with the key `env`,\nregardless of its value: \n\n resource.hasTagKey('123456789012/env')\n\nThis condition grants a role on resources that have both the tag `env: prod` and\nany tag that uses the key `project`: \n\n resource.matchTag('123456789012/env', 'prod') &&\n resource.hasTagKey('123456789012/project')\n\nWhat's next\n-----------\n\n- Learn how to [manage tags and attach tags to resources](/resource-manager/docs/tags/tags-creating-and-managing).\n- Get details about [checking tags in a condition](/iam/docs/conditions-attribute-reference#resource-tags)."]]