Managing access to Compute Engine resources Stay organized with collections Save and categorize content based on your preferences.
This page describes how you can exercise the principle of least privilege bygranting access to specific Compute Engineresources instead of granting access to a parent resource such as aproject, folder, or organization.
You grant access to a resource by setting anIdentity and Access Management (IAM) policyon the resource. The policy binds one or more members, such as a user or aservice account, to one or moreroles. Each rolecontains a list of permissions that let the member interact with the resource.
If you grant access to aparent resource(for example, to a project), you implicitly grant access to all its childresources (for example, to all VMs in that project). To limit access toresources, set IAM policies on lower-level resources whenpossible, instead of at the project level or above.
For general information about how to grant, change, and revoke access toresources unrelated to Compute Engine, for example, to grant access toa Google Cloud project, see the IAM documentation forGranting, changing, and revoking access to resources.
Before you begin
- Review theIAM overview.
- Read the Compute Engineaccess control overview.
- Familiarize yourself withIAM roles for Compute Engine.
- 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 manage access to Compute Engine resources, ask your administrator to grant you theCompute Admin (roles/compute.admin) IAM role on the resource. For more information about granting roles, seeManage access to projects, folders, and organizations.
This predefined role contains the permissions required to manage access to Compute Engine resources. To see the exact permissions that are required, expand theRequired permissions section:
Required permissions
The following permissions are required to manage access to Compute Engine resources:
- To grant or revoke access to resources:
compute.projects.geton the projectcompute.RESOURCE_TYPE.geton the resourcecompute.RESOURCE_TYPE.getIamPolicyon the resourcecompute.RESOURCE_TYPE.setIamPolicyon the resource
- To test caller permissions:
compute.RESOURCE_TYPE.getIamPolicyon the resourceReplaceRESOURCE_TYPE with the resource that you want to manage access to. For example
instances,instanceTemplates, orimages.
You might also be able to get these permissions withcustom roles or otherpredefined roles.
Supported resources
To view a list of Compute Engine resources that support resource-levelaccess control, seeResource types that accept IAM policiesand filter forCompute Engine.
For other Compute Engine resources that don't support resource-levelaccess control, you must manage access to those resources at the project,folder, or organization levels. For information about organizations, folders,or projects, seeResource hierarchy.
Granting access to Compute Engine resources
Aprincipal, such as a user or service account, can access Compute Engineresources. Anidentity is a property of a principal. A principal'sidentity is typically represented by an email address associated with the account.
Before you grant an IAM role to a principal for a resource,check which roles are available to grant on a particular resource. For moreinformation, seeViewing the grantable roles on resources.
To grant permission to access specific Compute Engine resources,set anIAM policy on the resource.
Note: When managing access for users inexternal identity providers, replace instances of Google Account principal identifiers—likeuser:kiran@example.com,group:support@example.com, anddomain:example.com—with appropriateWorkforce Identity Federation principal identifiers.Console
- In the Google Cloud console, go to the respective resource page for whichyou want to add permissions.
- For instances, go to theVM instances page.
- For zonal and regional disks, go to theDisks page.
- For snapshots, go to theSnapshots page.
- For images, go to theImages page.
- For instance templates, go to theInstance templates page.
- For machine images, go to theMachine images page.
- For reservations, go to theReservations page.
- For sole-tenant nodes, go to theSole-tenant nodes page.
- Select the checkboxes next to the resources you want to update.
- Complete the following steps based on the resource page.
- For VM instances, clickPermissions.
- For all other resources, complete the following:
- Check if the info panel is visible. If it is not visible, clickShow info panel.
- Select thePermissions tab.
- ClickAdd principal.
- Add the identity for the principal and select the required role.
- To save your changes, clickSave.
gcloud
To grant a role to a principal on a resource, use that resource'sadd-iam-policy-binding sub-command with the--member and--role flags.
gcloud computeRESOURCE_TYPE add-iam-policy-bindingRESOURCE_NAME \ --member='PRINCIPAL' \ --role='ROLE'
Replace the following:
RESOURCE_TYPE: the type of resource. Valid valuesinclude:disksimagesinstancesinstance-templatesmachine-imagesreservationssole-tenancy node-groupssole-tenancy node-templatessnapshots
RESOURCE_NAME: the name of the resource. Forexample,my_instance.PRINCIPAL: a valid identity for the principalthat you want to grant the role. Must be of the formuser|group|serviceAccount:EMAIL_ADDRESSordomain:DOMAIN_ADDRESS. For example:user:test-user@gmail.comgroup:admins@example.comserviceAccount:test123@example.domain.comdomain:example.domain.com
ROLE: the role to assign this principal.
If you are granting access to a resource that is in preview, useagcloud beta compute command instead.
REST
To modify an IAM policy through the API, do thefollowing:
Read the existing policy with the resource's respective
getIamPolicymethod. For example, the following HTTP request reads the IAM policy of a VM:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME:getIamPolicy
Replace the following:
PROJECT_ID: the project ID of the projectthis VM belongs to.ZONE: the zone of the VM. For regional orglobal resources, replacezones/ZONEwithregions/REGIONorglobal.VM_NAME: the name of the VM instance.
Compute Engine returns the current policy in the response.
Edit the policy with a text editor to add or remove principals and their associated roles. For example, to grant the
compute.adminrole to email@example.com, add the following new binding to policy:{ "members": [ "user:email@example.com" ], "role":"roles/compute.admin"}Write the updated policy with
setIamPolicy():POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME:setIamPolicy
Replace the following:
PROJECT_ID: the project ID of the projectthis VM belongs to.ZONE: the zone of the VM. For regional orglobal resources, replacezones/ZONEwithregions/REGIONorglobal.VM_NAME: the name of the VM instance.
In the body of the request, provide the updated IAMpolicy from the previous step.
Revoking access to resources
As a best practice, after principals no longer need access to yourCompute Engine resources, revoke their access.
Console
- In the Google Cloud console, go to the respective resource page for whichyou want to add permissions.
- For instances, go to theVM instances page.
- For zonal and regional disks, go to theDisks page.
- For snapshots, go to theSnapshots page.
- For images, go to theImages page.
- For instance groups, go to theInstance groups page.
- For instance templates, go to theInstance templates page.
- For machine images, go to theMachine images page.
- For reservations, go to theReservations page.
- For sole-tenant nodes, go to theSole-tenant nodes page.
- Select the checkboxes next to the resources you want to update.
- Complete the following steps based on the resource page.
- For VM instances, clickPermissions.
- For all other resources, complete the following:
- Check if the info panel is visible. If it is not visible, clickShow info panel.
- Select thePermissions tab.
- Click the role card from which you want to remove principals. This expandsthe card and shows users with that role for that resource.
- To remove a principal from that role,clickDelete.
gcloud
To remove a role from a principal for a resource, use the resource'sremove-iam-policy-binding sub-command with the--member and--roleflags.
gcloud computeRESOURCE_TYPE remove-iam-policy-bindingRESOURCE_NAME \ --member='MEMBER' \ --role='ROLE'
Replace the following:
RESOURCE_TYPE: type of resource. Valid valuesinclude:disksimagesinstancesinstance-templatesmachine-imagesreservationssole-tenancy node-groupssole-tenancy node-templatessnapshots
RESOURCE_NAME: name of the resource.For example,my_instance.PRINCIPAL: a valid identity for the principal.Must be of the formuser|group|serviceAccount:EMAIL_ADDRESSordomain:DOMAIN_ADDRESS. For example:user:test-user@gmail.comgroup:admins@example.comserviceAccount:test123@example.domain.comdomain:example.domain.com
ROLE: role from which you want to removethe principal.
If you are revoking access to a resource that is in preview, useagcloud beta compute command instead.
REST
To modify an IAM policy directly through the API, do thefollowing:
Read the existing policy with the resource's respective
getIamPolicymethod. For example, the following HTTP request reads the IAM policy of a VM:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME:getIamPolicy
Replace the following:
PROJECT_ID: the project ID of the projectthis VM belongs to.ZONE: the zone of the VM. For regional orglobal resources, replacezones/ZONEwithregions/REGIONorglobal.VM_NAME: the name of the VM instance.
Compute Engine returns the current policy in the response.
Edit the policy with a text editor to remove members from the associated roles. For example, remove email@example.com from the
compute.adminrole:{ "members": [ "user:owner@example.com" ], "role":"roles/compute.admin"}Write the updated policy with
setIamPolicy():POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME:setIamPolicy
Replace the following:
PROJECT_ID: the project ID of the projectthis VM belongs to.ZONE: the zone of the VM. For regional orglobal resources, replacezones/ZONEwithregions/REGIONorglobal.VM_NAME: the name of the VM instance.
In the body of the request, provide the updated IAMpolicy from the previous step.
Testing whether a caller has permissions
If you don't know what permissions an identity has, use thetestIamPermissions API method to check which permissions are available toan identity.
The method takes a resource URL and a set of permissions as input parameters,and returns the set of permissions that the caller is allowed. You can use thismethod on any of thesupported resources.
Typically,testIamPermissions is intended for integration with yourproprietary software, such as a customized graphical user interface. Youtypically don't calltestIamPermissions if you're using Google Clouddirectly to manage permissions.
For example, if you are building a GUI on top of the Compute Engine API andyour GUI has a "start" button that starts an instance, you could callcompute.instances.testIamPermissions() to determine whether the button shouldbe enabled or disabled.
To test whether a caller has specific permissions on a resource:
Send a request to the resource and include in the request body a list ofpermissions to check for.
For example, on an instance, you might check for
compute.instances.start,compute.instances.stop, andcompute.instances.delete.POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/testIamPermissions { "permissions": [ "compute.instances.start", "compute.instances.stop", "compute.instances.delete" ] }The request returns the permissions that are enabled for the caller.
{ "permissions": [ "compute.instances.start", "compute.instances.stop" ]}
Modifying resource access for multiple members
If you want to modify access to Compute Engine resources for multiplemembers simultaneously, review recommendations on how tomodify an IAM policy programmatically.
What's next
- Learn how tomanage access to custom imageswith IAM.
- Learn more aboutService accounts.
- Learn more aboutCompute Engine IAM roles.
- Learn more about the permissions that are included inpredefinedCompute Engine IAM roles.
- Learn how to create and managecustom roles.
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.