Manage tags for resources Stay organized with collections Save and categorize content based on your preferences.
This guide describes how to create and manage tags for Compute Engineresources. A tag is a key-value pair that can be attached to aGoogle Cloud resource. Tags are used for several purposes, including:
- Conditionally allow or deny policies based on whether a resource has aspecific tag.
- Define sources and targets in global networkfirewall policies and regionalnetwork firewall policies.
- Organizing resources in a logical manner.
After creating a tag and granting appropriate access to both the tag and theresource, you can attach the tag as a key-value pair. You can attach exactlyone value to a resource for a given key. For example, if you attach theenvironment: development tag, then you cannot attach theenvironment: production orenvironment: test tags. Each resource can have amaximum of 50 key-value pairs attached.
To attach tags to resources, you must create a tag binding resource thatlinks the tag value to the Google Cloud resource. For more informationon tags and how they work, review theTags overview document.
Before you begin
- Read theTags overviewon the Resource Manager documentation.
- Read theCreating and managing tags on the Resource Manager documentation.
- If you haven't already, set upauthentication. Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.- Set a default region and zone.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
Required roles
To get the permissions that you need to create and manage tags for Compute Engine resources, ask your administrator to grant you the following IAM roles on your organization or project:
- Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) - To create and manage tags:Tag Administrator role (
roles/resourcemanager.tagAdmin) - To manage existing tags:Tag User role (
roles/resourcemanager.tagUser)
For more information about granting roles, seeManage access to projects, folders, and organizations.
These predefined roles contain the permissions required to create and manage tags for Compute Engine resources. To see the exact permissions that are required, expand theRequired permissions section:
Required permissions
The following permissions are required to create and manage tags for Compute Engine resources:
- View tags:
resourcemanager.tagKeys.getresourcemanager.tagKeys.listresourcemanager.tagValues.listresourcemanager.tagValues.getcompute.instances.listTagBindingscompute.instances.listEffectiveTagsresourcemanager.projects.get
- Create tags:
resourcemanager.tagKeys.createresourcemanager.tagValues.createresourcemanager.tagKeys.setIamPolicyresourcemanager.tagValues.setIamPolicy
- Manage tags:
resourcemanager.tagKeys.updateresourcemanager.tagValues.updateresourcemanager.tagKeys.deleteresourcemanager.tagValues.deleteresourcemanager.tagKeys.getIamPolicyresourcemanager.tagValues.getIamPolicyresourcemanager.tagKeys.setIamPolicyresourcemanager.tagValues.setIamPolicy
- Add or remove tags for a compute instance:
compute.instances.createTagBindingcompute.instances.deleteTagBindingresourcemanager.tagValueBindings.createresourcemanager.tagValueBindings.delete
You might also be able to get these permissions withcustom roles or otherpredefined roles.
Supported resources
Compute Engine supports tagging for the following resources:
Only after resource creation:
- Managed instance groups (MIGs)
- Health checks
Both during and after resource creation:
- Virtual machine (VM) instances
- Disks
- Images
- Snapshots
- VPC networks
- VPC subnetworks
- VPC firewall rules
- VPC routes
- Cloud Routers
Adding tags to a resource
You can attach existing tags to certain resources. After the resource iscreated, attach tags to that resource by using the following instructions.
Console
Depending on the resource type, the exact steps might vary. Forexample, the following steps attach a tag to a VM:
In the Google Cloud console, go to theVM instances page.
Select your project and clickContinue.
In theName column, click the name of the VM for which you want to add tags.
From theVM instance details page, complete the following steps:
- ClickEdit.
- In theBasic information section, clickManage tags and add thetags that you want for the instance.
- ClickSave.
gcloud
For information about how to use these flags, readAttaching tags to resourcesin the Resource Manager documentation.
For example, the following command attaches a tag to a VM:
gcloud resource-manager tags bindings create \ --location=LOCATION_NAME \ --tag-value=tagValues/TAGVALUE_ID \ --parent=FULL_RESOURCE_NAME
Replace the following:
LOCATION_NAME: thezonewhere the instance is located. For a global resource, omit the--locationflag.TAGVALUE_ID: the numeric ID of the tag valueFULL_RESOURCE_NAME: thefull resource nameof the target resource; in this example, the full resource name of the VMinstance://compute.googleapis.com/projects/PROJECT_NUMBER/zones/ZONE/instances/INSTANCE_ID
Replace the following:
PROJECT_NUMBER: the numeric ID of your projectthat contains the target resourceZONE: the zone that contains the instanceINSTANCE_ID: theVM instance ID
REST
To attach a tag to a resource, you must first create a JSON representation of atag binding that includes the permanent ID or namespace name of the tag valueand the permanent ID of the resource. For more information about the format of atag binding, see thetagBindings reference.
To attach the tag to a zonal resource, such as a VM instance, usethetagBindings.create method with the regional endpoint where your resourceis located. For example:
POST https://LOCATION_NAME-cloudresourcemanager.googleapis.com/v3/tagBindings
The request body can be one of the following two options:
{ "parent": "FULL_RESOURCE_NAME", "tagValue": "tagValue/TAGVALUE_ID"}{ "parent": "FULL_RESOURCE_NAME", "tagValueNamespacedName":TAGVALUE_NAMESPACED_NAME}Replace the following:
LOCATION_NAME: the zonal or regionallocation of the resource.For a VM instance, specify the zone. For a global resource, omit theLOCATION_NAME-parameterFULL_RESOURCE_NAME: thefull resource nameof the target resource; in this example, the full resource name of the VMinstance://compute.googleapis.com/projects/PROJECT_NUMBER/zones/ZONE/instances/INSTANCE_ID
Replace the following:
PROJECT_NUMBER: the numeric ID of your projectthat contains the target resourceZONE: the zone that contains the instanceINSTANCE_ID: theVM instance ID
TAGVALUE_ID: the permanent ID of the tag value that isattached—for example:4567890123TAGVALUE_NAMESPACED_NAME: the namespace name of the tagvalue that is attached and is of the format:parentNamespace/tagKeyShortName/tagValueShortName
Adding tags to a resource during resource creation
In certain scenarios, you might want to tag resources during resource creation,rather than after the resource is created.
Console
Depending on the resource type, the exact steps might vary.The following steps are for a VM:
In the Google Cloud console, go to theVM instances page.
Select your project and clickContinue.
ClickCreate instance.TheCreate an instance page appears and displays theMachine configuration pane.
In the navigation menu, clickAdvanced. In theAdvanced pane that appears, do the following:
- Expand theManage tags and labelssection.
- ClickAdd tags.
- In theTags pane that opens, follow the instructions to add a tagto the instance.
- ClickSave.
Specify other configuration options for your instance. For moreinformation, seeConfiguration options during instance creation.
To create and start the VM, clickCreate.
gcloud
To attach a tag to a resource during resource creation, add the--resource-manager-tags flag with the respectivecreate command. Forexample, to attach a tag to a VM, use the following command:
gcloud compute instances createINSTANCE_NAME \ --zone=ZONE \ --resource-manager-tags=tagKeys/TAGKEY_ID=tagValues/TAGVALUE_ID
Replace the following:
INSTANCE_NAME: the name of your VM instanceZONE: the zone that contains the VM instanceTAGKEY_ID: the tag key number numeric IDTAGVALUE_ID: the permanent numeric ID of the tag valuethat is attached—for example:4567890123
Specify multiple tags by separating the tags with a comma, for example,TAGKEY1=TAGVALUE1,TAGKEY2=TAGVALUE2.
REST
Make aPOST request to the following URL:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/instances
Include the following request JSON body:
{ "name":INSTANCE_NAME, "params": { "resourceManagerTags": { "tagKeys/TAGKEY_ID": "tagValues/TAGVALUE_ID", }, } // other fields omitted}Replace the following:
INSTANCE_NAME: the name of your VM instanceTAGKEY_ID: the tag key number numeric IDTAGVALUE_ID: the permanent numeric ID of the tag valuethat is attached—for example:4567890123
Use tags with firewall rules
To securely enable network traffic to a specific Compute Engineinstance, you can apply a network tag or a secure tag to the instance. Then,you can create a firewall rule that targets that tag. This is helpful formanaging access to your instances at scale.
Note: Network tags are different from secure tags. For more information aboutthe differences between secure tags and network tags, seeComparison of secure tags and network tags.For example, assume you have a compute instance that runs a web server, and youcreated a new web application that needs to be accessible to the public on thestandard HTTP port (80). You can use a network or secure tag to implement afirewall rule that grants HTTP access only to the compute instance that the webserver runs on.
For instructions on how to create tags for use in firewall policies,see the following:
- To use network tags with VPC firewall rules, seeAdd network tags.
- To use secure tags with hierarchical firewall policies, global and regionalnetwork firewall policies, or for use with sources connected usingVPC Network Peering, seeCreate and manage secure tags.
For information about whether to use service accounts ornetwork tags to define targets and sources for network ingress rules, seeFilter by service account versus network tag.
Detaching a tag from a resource
You can detach a tag from a resource by deleting the tag binding resource.
To review instructions on how to detach tags, seeDetaching a tag from a resourcein the Resource Manager documentation.
Console
Depending on the resource type, the exact steps might be a bit different. Forexample, the following steps detaches a tag from a VM instance:
In the Google Cloud console, go to theVM instances page.
Select your project and clickContinue.
In theName column, click the name of the instance for which you wantto remove tags.
From theVM instance details page, complete the following steps:
- ClickEdit.
- For resource manager tags, in theBasic information section,clickManage Tags. Remove the tags that you want for the instance.You can only remove tags that were directly added to the instance.
- For network tags, in theNetworking section, remove the tag fromtheNetwork tags list.
- ClickSave.
gcloud
To detach a tag from a compute instance, use theresource-manager tags bindings delete command:
gcloud resource-manager tags bindings delete \ --location=LOCATION_NAME \ --tag-value=tagValues/TAGVALUE_ID \ --parent=FULL_RESOURCE_NAME
Replace the following:
LOCATION_NAME: thezonewhere the instance is located. For a global resource, omit theflag--location=LOCATION_NAME \TAGVALUE_ID: the numeric ID of the tag valueFULL_RESOURCE_NAME: thefull resource nameof the target resource; in this example, the full resource name of theinstance://compute.googleapis.com/projects/PROJECT_NUMBER/zones/ZONE/instances/INSTANCE_ID
Replace the following:
PROJECT_NUMBER: the numeric ID of your projectthat contains the target resourceZONE: the zone that contains the instanceINSTANCE_ID: theinstance ID
To update or replace an existing tag binding to another one, detach the oldtag binding and attach the new one.
REST
To delete a tag binding attached to a resource, such as a compute instance,use thetagBindings.delete methodwith the regional endpoint where your resource is located.
DELETE https://LOCATION_NAME-cloudresourcemanager.googleapis.com/v3/{name=TAGBINDINGS_NAME}Replace the following:
LOCATION_NAME: the zonal or regionallocation of the resource.For an instance, specify the zone. For a global resource, omit theLOCATION_NAME-parameterTAGBINDINGS_NAME: the permanent ID of theTagBinding; for example:tagBindings/%2F%2Fcloudresourcemanager.googleapis.com%2Fprojects%2F1234567890/tagValues/567890123456
Viewing tags attached to a resource
To review detailed instructions on how to list tags, seeListing all tags attached to a resourcein the Resource Manager documentation.
Console
Depending on the resource type, the exact steps might be a bit different. Forexample, the following steps show how to view tags for a VM instance:
In the Google Cloud console, go to theVM instances page.
Select your project and clickContinue.
In theName column, click the name of the instance for which you wantto view tags.
From theVM instance details page, look for tags under theTagssection.
gcloud
To get a list of tag bindings directly attached to a resource, use thegcloud resource-manager tags bindings list command. If you add the--effective flag, you will also return a list of tags inherited by thisresource. For example:
gcloud resource-manager tags bindings list \ --location=LOCATION_NAME \ --parent=FULL_RESOURCE_NAME
Replace the following:
LOCATION_NAME: thezonewhere the instance is located. For a global resource, omit theflag--location=LOCATION_NAME \FULL_RESOURCE_NAME: thefull resource nameof the target resource; in this example, the full resource name of theinstance://compute.googleapis.com/projects/PROJECT_NUMBER/zones/ZONE/instances/INSTANCE_ID
Replace the following:
PROJECT_NUMBER: the numeric ID of your projectthat contains the target resourceZONE: the zone that contains the instanceINSTANCE_ID: theinstance ID
If you add the--effective flag to thetags bindings list command, youalso return a list of all tags inherited by this resource.
The output is similar to the following:
namespacedTagKey: 961309089256/environmentnamespacedTagValue: 961309089256/environment/productiontagKey: tagKeys/417628178507tagValue: tagValues/247197504380inherited: true
If all tags evaluated on a resource are directly attached, theinheritedfield is false and is omitted.
REST
To list the tag bindings attached to a regional resource,such as Compute Engine instances, use thetagBindings.list methodwith the regional endpoint where your resource is located. For example:
GET https://LOCATION_NAME-cloudresourcemanager.googleapis.com/v3/tagBindings{ "parent": "FULL_RESOURCE_NAME"}Replace the following:
LOCATION_NAME: the zonal or regionallocation of the resource.For an instance, specify the zone. For a global resource, omit theLOCATION_NAME-parameterFULL_RESOURCE_NAME: thefull resource nameof the target resource; in this example, the full resource name of theinstance://compute.googleapis.com/projects/PROJECT_NUMBER/zones/ZONE/instances/INSTANCE_ID
Replace the following:
PROJECT_NUMBER: the numeric ID of your projectthat contains the target resourceZONE: the zone that contains the instanceINSTANCE_ID: theinstance ID
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 2026-02-19 UTC.