Creating and Managing Custom Roles

This page describes how to create and manage a custom role.

Before you begin

  • Read Understanding IAM Custom Roles, which contains information about the permissions required to create custom roles and best practices.
  • If you are using the gcloud command-line utility, make sure that you're using version 188.0.0 or later. To update gcloud to version 188.0.0, run the following command: gcloud components update --version 188.0.0

Viewing the available permissions for a resource

Before you create a custom role, you might want to know what permissions can be applied to a resource. You can get all permissions that can be applied to a resource, and the resources below that in the hierarchy, using the gcloud command-line tool, the Cloud Console, or the IAM API. For example, you can get all permissions that you can apply on an organization and on projects in that organization.

gcloud


Use the gcloud iam list-testable-permissions command to get a list of permissions that can be applied on a target resource. The returned permissions are the permissions that can be used to create a custom role at this resource and any resource below in the hierarchy.

The following example demonstrates how to list testable permissions for a project resource:

gcloud iam list-testable-permissions [PROJECT_ID]

[PROJECT_ID] is the ID of the project in the form of a full resource name: //cloudresourcemanager.googleapis.com/projects/PROJECT_ID, such as //cloudresourcemanager.googleapis.com/projects/my-project-id.

The list-testable-permissions command may return hundreds of results. To limit the results, you can also specify a filter expression. A truncated example of possible results is shown below:

---
name: appengine.applications.create
stage: GA
---
customRolesSupportLevel: TESTING
name: appengine.applications.disable
stage: GA
---
name: appengine.applications.get
stage: GA
---
customRolesSupportLevel: NOT_SUPPORTED
name: appengine.applications.list
onlyInPredefinedRoles: true
stage: GA
---
name: appengine.applications.update
stage: GA
---
name: appengine.instances.delete
stage: GA
---
name: appengine.instances.get
stage: GA
---

Note that each permission indicates whether it is supported in a custom role. For a complete list of supported and unsupported permissions, see Custom Roles Permissions Support.

Console


  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Select your project from the drop-down at the top of the page.
  3. Select the checkbox for a resource's admin role to view all the permissions that you can apply on that resource. For example, when you select the Compute Instance Admin role, the right side panel displays all the permissions that you can apply on a Compute Engine instance.

API


QueryTestablePermissions returns all the permissions that can be applied on a resource. The returned permissions are the permissions that can be used to create a custom role at this resource and any resource below in the hierarchy. The only required input to this request is the full resource name such as //cloudresourcemanager.googleapis.com/projects/my-project.

Optionally, the caller can provide support for pagination if the resource has a long list of permissions.

Example

full_resource_name: '//cloudresourcemanager.googleapis.com/projects/my-project'`

Error codes

Error CodeStatus Message
INVALID_ARGUMENTmust be between 0 and 100
INVALID_ARGUMENTInvalid pagination token encoding
INVALID_ARGUMENTInvalid pagination token
INVALID_ARGUMENTPagination token is not for the specified container
INVALID_ARGUMENTInvalid starting point in pagination token
INVALID_ARGUMENTInvalid pagination token cookie
INVALID_ARGUMENTExpired pagination token
INVALID_ARGUMENT{full_resource_name} must be specified
INVALID_ARGUMENT{full_resource_name}, does not match //[a-z0-9.-]/.a-z0-9.-]/.

Getting the role metadata

Before you create a custom role, you might want to get the metadata for both predefined and custom roles. Role metadata includes the role ID and permissions contained in the role. You can view the metadata using the Google Cloud Platform Console or the IAM API.

To view the role metadata, use one of the methods below:

gcloud


Use the gcloud iam roles describe command to view metadata for predefined roles and custom roles.

To view the metadata for a predefined role, execute the following gcloud command:

gcloud iam roles describe [ROLE_NAME]

[ROLE_NAME] is the name of the role, such as roles/viewer.

The following example demonstrates the output of the describe command when executed on the predefined role roles/iam.roleViewer:

gcloud iam roles describe roles/iam.roleViewer

description: Read access to all custom roles in the project.
etag: AA==
includedPermissions:
- iam.roles.get
- iam.roles.list
- resourcemanager.projects.get
- resourcemanager.projects.getIamPolicy
name: roles/iam.roleViewer
stage: GA
title: Role Viewer

To view the metadata for a custom role, first determine whether the custom role was created at the organization or project level. If the custom role was created at the organization level, execute the following gcloud command:

gcloud iam roles describe --organization [ORGANIZATION_ID] [ROLE_NAME]

[ORGANIZATION_ID] is the ID of the organization in the form of: 1234567. [ROLE_NAME] is the name of the custom role, such as myCustomRole.

To view the metadata for a project-level custom role, execute the following gcloud command:

gcloud iam roles describe --project [PROJECT_ID] [ROLE_NAME]

[PROJECT_ID] is the ID of the project, such as my-project-id. [ROLE_NAME] is the name of the custom role, such as myCustomRole.

For more information, see the reference documentation for gcloud iam roles describe.

Console


  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Select your organization or project from the drop-down at the top of the page.
  3. Select the checkbox for one or more roles to view the role permissions. The right side panel displays the permissions contained in the role(s), if any.

The icons beside the role indicate if it's a custom role ("factory" icon) or a predefined role (hexagon icon).

Role icons

If you want to find all the roles that include a specific permission, type the permission name in the Filter box at the top of the Roles list.

API


If you know the role name of the role you want to view, use the roles.get method to get a custom role. If you don't know the role name, use the roles.list method to list all custom roles in an organization or a project.

To call GetRole(), set the following field in the GetRoleRequest:

  • Name of the role such as roles/{ROLE_NAME} or organizations/{ORGANIZATION_ID}/roles/{ROLE_NAME}.

To call ListRoles(), set the following field in the ListRolesRequest:

  • The parent for which you want to get all custom roles, such as organizations/{ORGANIZATION_ID} or projects/{PROJECT_ID}.

Error codes

Error CodeStatus Message
PERMISSION_DENIEDYou don't have permission to get the role at {path}
NOT_FOUNDThe role named {role} was not found.
INVALID_ARGUMENTThe role name must be in the form roles/{role}, or organizations/{organization_id}/roles/{role}.
PERMISSION_DENIEDYou don't have permission to list roles in {path}.
INVALID_ARGUMENTThe parent{path} is invalid. The parent must be in the form organizations/{organization_id} or empty.
INVALID_ARGUMENTRole view is not valid.

Creating a custom role

To create a custom role, a caller must possess iam.roles.create permission. By default, the owner of a project or an organization has this permission and can create and manage custom roles.

Users who are not owners, including organization admins, must be assigned either the Organization Role Administrator role, or the IAM Role Administrator role.

gcloud


Use the gcloud iam roles create command to create new custom roles. You can use this command in two ways:

  • By providing a YAML file that contains the role definition
  • By using flags to specify the role definition

When creating a custom role, you must specify whether it applies to the organization level or project level by using the --organization [ORGANIZATION_ID] or --project [PROJECT_ID] flags. Each example below creates a custom role at the project level.

To create a custom role using a YAML file:

Create a YAML file that contains the definition for your custom role. The file must be structured in the following way:

title: [ROLE_TITLE]
description: [ROLE_DESCRIPTION]
stage: [LAUNCH_STAGE]
includedPermissions:
- [PERMISSION_1]
- [PERMISSION_2]

Each of the placeholder values is described below:

  • [ROLE_TITLE] is a friendly title for the role, such as "Role Viewer".
  • [ROLE_DESCRIPTION] is a short description about the role, such as "My custom role description".
  • [LAUNCH_STAGE] indicates the stage of a role in the launch lifecycle, such as ALPHA, BETA, or GA.
  • includedPermissions specifies the list of one or more permissions to include in the custom role, such as - iam.roles.get.

Save the YAML file, and then execute the following gcloud command:

gcloud iam roles create [ROLE_ID] --project [PROJECT_ID] \
--file [YAML_FILE_PATH]

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id.
  • [YAML_FILE_PATH] is the path to the location of your YAML file that contains the custom role definition.

The following example YAML file demonstrates how to create a role definition:

title: "Role Viewer"
description: "My custom role description."
stage: "ALPHA"
includedPermissions:
- iam.roles.get
- iam.roles.list

The following example demonstrates how to create a role using the YAML file:

gcloud iam roles create viewer --project my-project-id \
--file my-role-definition.yaml

If the role was created successfully, the following response is returned:

Created role [viewer].
description: My custom role description.
etag: BwVkBX0sQD0=
includedPermissions:
- iam.roles.get
- iam.roles.list
name: projects/my-project-id/roles/viewer
stage: ALPHA
title: Role Viewer

To create a custom role using flags:

Execute the following gcloud command:

gcloud iam roles create [ROLE_ID] --project [PROJECT_ID] \
--title [ROLE_TITLE] --description [ROLE_DESCRIPTION] \
--permissions [PERMISSIONS_LIST] --stage [LAUNCH_STAGE]

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id.
  • [ROLE_TITLE] is a friendly title for the role, such as Role Viewer.
  • [ROLE_DESCRIPTION] is a short description about the role, such as "My custom role description.".
  • [PERMISSIONS_LIST] contains a comma-separated list of permissions you want to include in the custom role. For example: iam.roles.get,iam.roles.list.
  • [LAUNCH_STAGE] indicates the stage of a role in the launch lifecycle, such as ALPHA, BETA, or GA.

The following example demonstrates how to create a role using flags:

gcloud iam roles create viewer --project my-project-id \
--title "Role Viewer" --description "My custom role description." \
--permissions iam.roles.get,iam.roles.list --stage ALPHA

If the role was created successfully, the following response is returned:

Created role [viewer].
description: My custom role description.
etag: BwVkBX0sQD0=
includedPermissions:
- iam.roles.get
- iam.roles.list
name: projects/my-project-id/roles/viewer
stage: ALPHA
title: Role Viewer

Console


To create a new custom role from scratch:

  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Select your organization from the Organization drop-down.
  3. Click Create Role.
  4. Enter a Name, a Title, and Description for the role.
  5. Click Add Permissions.
  6. Select the permissions you want to include in the role and click Add Permissions. Use the All Services and All Types drop-downs to filter and select permissions by services and types.

Creating a custom role based on an existing curated role:

  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Select your organization from the Organization drop-down.
  3. Select the roles based on which you want to create the new custom role.
  4. Click Create Role from Selection.
  5. Enter a Name, a Title, and Description for the role.
  6. Uncheck the permissions you want to exclude from the role.
  7. Click Add Permissions to include any permissions.
  8. Click Create.

API


Use the create method to create a new custom role.

Set the following required parameters in the request:

  • The role_id that you want to use for the new custom role, such as appengine.myCustomStorageAuditor.
  • Description of the custom role, such as "This role grants access to list storage resources, their capacity, and access policies".
  • A list of permissions you want to associate with this role.
  • Note that setting the name field in the role will result in an error.

We recommend that you also set the following optional parameters:

  • Title for the custom role, such as "Example Custom Role Editor".
  • Set a value for the stage, such as GA.

stage takes the following values: ALPHA, BETA, GA, DEPRECATED, or DISABLED.

Some predefined roles contain deprecated permissions or permissions that are otherwise not permitted in custom roles. Creating a custom role based on a predefined role that contains any deprecated or restricted permissions will fail.

Example

parent: '[PARENT_NAME]'
role_id: '[ROLE_ID]'
role {
    name: ''
    title: '[ROLE_TITLE]'
    description: '[ROLE_DESCRIPTION]'
    included_permissions: '[PERMISSION]'
    included_permissions: '[PERMISSION]'
})",

Where:

  • [PARENT_NAME] is the name of either the organization in which you're creating the custom role, for example organizations/0000000000000001, or the project ID in which you're creating the custom role, for example projects/my-project.
  • [ROLE_ID] is the ID of the custom role. For example, appengine.myCustomStorageAuditor.
  • [ROLE_TITLE] is the title for the role. For example, Storage Auditor.
  • [ROLE_DESCRIPTION] is the description for what the role does.
  • [PERMISSION] is the permission you want to include in the custom role.

Error codes

Error CodeStatus Message
PERMISSION_DENIEDYou don't have permission to create a role in {parent}.
ALREADY_EXISTSA role named {role_id} in {parent} already exists.
INVALID_ARGUMENTThe parent {parent} is invalid. The parent must be in the form organizations/{organization_id}.
INVALID_ARGUMENTThe role_id {role_id} is invalid. It doesn't match pattern {pattern}.
INVALID_ARGUMENTThe number of permissions in the role is greater than the maximum of {max}.
INVALID_ARGUMENTThe role.stage {stage} is invalid.

Editing an existing custom role

Read-Modify-Write

A common pattern for updating a resource's metadata, such as a custom role, is to read its current state, update the data locally, and then send the modified data for writing. This pattern could cause a conflict if two or more independent processes attempt the sequence simultaneously. For example, if two owners for a project try to make conflicting changes to a role at the same time, some changes could fail. Cloud IAM solves this problem using an etag property in custom roles. This property is used to verify if the custom role has changed since the last request. When you make a request to Cloud IAM with an etag value, Cloud IAM compares the etag value in the request with the existing etag value associated with the custom role. It writes the change only if the etag values match.

When you update a role, first get the role using roles.get(), update the role, and then write the updated role using roles.patch(). Use the etag value when setting the role only if the corresponding role in roles.get() contains an etag value.

gcloud


Use the gcloud iam roles update command to update custom roles. You can use this command in two ways:

  • By providing a YAML file that contains the updated role definition
  • By using flags to specify the updated role definition

When updating a custom role, you must specify whether it applies to the organization level or project level by using the --organization [ORGANIZATION_ID] or --project [PROJECT_ID] flags. Each example below creates a custom role at the project level.

To update a custom role using a YAML file:

Get the current definition for the role by executing the following gcloud command:

gcloud iam roles describe [ROLE_ID] --project [PROJECT_ID]

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role to update, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id.

The describe command returns the role's definition and includes an etag value that uniquely identifies the current version of the role. The etag value should be provided in the updated role definition to ensure that any concurrent role changes are not overwritten.

The describe command returns the following output:

description: [ROLE_DESCRIPTION]
etag: [ETAG_VALUE]
includedPermissions:
- [PERMISSION_1]
- [PERMISSION_2]
name: [ROLE_ID]
stage: [LAUNCH_STAGE]
title: [ROLE_TITLE]

Each of the placeholder values is described below:

  • [ROLE_DESCRIPTION] is a short description about the role, such as "My custom role description".
  • [ETAG_VALUE] is the unique identifier for the current version of the role, such as BwVkBkbfr70=.
  • includedPermissions specifies the list of one or more permissions to include in the custom role, such as - iam.roles.get.
  • [ROLE_ID] is the hierarchical ID for the role, depending on whether it was created at the project or organization level. For example: projects/my-project-id/roles/viewer
  • [LAUNCH_STAGE] indicates the stage of a role in the launch lifecycle, such as ALPHA, BETA, or GA.
  • [ROLE_TITLE] is a friendly title for the role, such as "Role Viewer".

To update the role, either include the outputted role definition to a YAML file or update the original YAML file with the outputted etag value.

Consider the following example YAML file, which contains the output from the describe command and adds two Cloud Storage permissions:

description: My custom role description.
etag: BwVkBkbfr70=
includedPermissions:
- iam.roles.get
- iam.roles.list
- storage.buckets.get
- storage.buckets.list
name: projects/my-project-id/roles/viewer
stage: ALPHA
title: Role Viewer

Save the YAML file, and then execute the following gcloud command:

gcloud iam roles update [ROLE_ID] --project [PROJECT_ID] \
--file [YAML_FILE_PATH]

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role to update, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id.
  • [YAML_FILE_PATH] is the path to the location of your YAML file that contains the updated custom role definition.

The following example demonstrates how to update a role using the YAML file:

gcloud iam roles update viewer --project my-project-id \
--file my-role-definition.yaml

If the role was updated successfully, the following response is returned:

description: My custom role description.
etag: BwVkBwDN0lg=
includedPermissions:
- iam.roles.get
- iam.roles.list
- storage.buckets.get
- storage.buckets.list
name: projects/my-project-id/roles/viewer
stage: ALPHA
title: Role Viewer

To update a custom role using flags:

Each part of a role definition can be updated using a corresponding flag. See the gcloud iam roles update topic for a list of all possible flags.

You can use the following flags to add or remove permissions:

  • --add-permissions [PERMISSIONS]: Adds one or more comma-separated permissions to the role.
  • --remove-permissions [PERMISSIONS]: Removes one or more comma-separated permissions from the role.

Alternatively, you can simply specify the new permissions using the --permissions [PERMISSIONS] flag and providing a comma-separated list of permissions to replace the existing permissions list.

To update other aspects of the role definition, execute the following gcloud command:

gcloud iam roles update [ROLE_ID] --project [PROJECT_ID] \
--title [ROLE_TITLE] --description [ROLE_DESCRIPTION] \
--stage [LAUNCH_STAGE]

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id.
  • [ROLE_TITLE] is a friendly title for the role, such as Role Viewer.
  • [ROLE_DESCRIPTION] is a short description about the role, such as "My custom role description.".
  • [LAUNCH_STAGE] indicates the stage of a role in the launch lifecycle, such as ALPHA, BETA, or GA.

The following example demonstrates how to add permissions to a role using flags:

gcloud iam roles update viewer --project my-project-id \
--add-permissions storage.buckets.get,storage.buckets.list

If the role was updated successfully, the following response is returned:

description: My custom role description.
etag: BwVkBwDN0lg=
includedPermissions:
- iam.roles.get
- iam.roles.list
- storage.buckets.get
- storage.buckets.list
name: projects/my-project-id/roles/viewer
stage: ALPHA
title: Role Viewer

Console


  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Select your organization from the Organization drop-down.
  3. Click on a custom role.
  4. Click Edit Role.
  5. Click Add Permissions to add new permissions to the role.
  6. Uncheck permissions to remove permissions from the role.
  7. Click Update to save the edited role.

API


Use the Role UpdateRole(UpdateRoleRequest) method to edit an existing custom role.

If you know the role ID of the custom role you wish to edit, get the role using the roles.get() method and then update the role using roles.patch().

If you don't know the role ID of the custom role you wish to edit, list all the roles using ListRoles() to identify the role. roles.list() returns a list of all roles referencing the resource. Then update the role using roles.patch().

Set the following required parameter in the roles.patch():

  • Name of the role such as organizations/{ORGANIZATION_ID}/roles/{ROLE_ID}.

Optionally set the update_mask field to specify the field(s) that can be updated in the future.

Example

name: '[ROLE_NAME]'
role {
  name: '[ROLE_NAME]'
  title: '[ROLE_TITLE]'`
  description: '[ROLE_DESCRIPTION]'
  included_permissions: '[PERMISSION]'
  included_permissions: '[PERMISSION]'
})"

Where:

  • [ROLE_NAME] is the name of the role. For example, organizations/123456/roles/appengine.customRoleEditor. Can be in the form roles/{ROLE_NAME}, organizations/{ORGANIZATION_ID}/roles/{ROLE_NAME}, or projects/{PROJECT_ID}/roles/{ROLE_NAME}

Note: Leaving the [ROLE_NAME] that is inside the role empty is recommended. The method will return an error if the two names are not identical and the name inside role is not empty.

  • [ROLE_TITLE] is the title for the role. For example, New custom editor.
  • [ROLE_DESCRIPTION] is a description for the role. For example, "My new long description of editor".
  • [PERMISSION] is the permission that you want to include in the role. For example, storage.objects.update.

Error codes

Error CodeStatus Message
PERMISSION_DENIEDYou don't have permission to update the role.
INVALID_ARGUMENTPredefined roles cannot be updated.
INVALID_ARGUMENTThe name in the request ([ROLE_NAME]) and the name in the role ([ROLE_NAME]) must match.
INVALID_ARGUMENTPermission [PERMISSION] is not valid.
ABORTEDThere were concurrent policy changes, since the etag did not match. Please retry the entire read-modify-write with exponential backoff.

Some predefined roles contain deprecated permissions or permissions that are otherwise not permitted in custom roles. Creating a custom role based on a predefined role that contains any deprecated or restricted permissions will fail.

Disabling a custom role

You can disable a custom role. When a role is disabled, any policy bindings related to the role are inactivated, meaning that the permissions in the role will not be granted, even if you grant the role to a user.

gcloud


Use the gcloud iam roles update command to disable a custom role by setting its launch stage to DISABLED. As described in the gcloud tab of the Editing an existing custom role section, you can update an existing custom role in the following two ways:

  • By providing a YAML file that contains the updated role definition
  • By using flags to specify the updated role definition

The easiest way to disable an existing custom role is to use the --stage flag and set it to DISABLED. Execute the following gcloud command:

gcloud iam roles update [ROLE_ID] --project [PROJECT_ID] \
--stage DISABLED

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id. You can also use the --organization [ORGANIZATION_ID] flag if the role was created at the organization level, such as 1234567.

The following example demonstrates how to disable a role:

gcloud iam roles update viewer --project my-project-id \
--stage DISABLED

If the role was updated successfully, the following response is returned:

description: My custom role description.
etag: BwVkB5NLIQw=
includedPermissions:
- iam.roles.get
- iam.roles.list
name: projects/my-project-id/roles/viewer
stage: DISABLED
title: Role Viewer

Console


  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Click on "Select a project" drop-down at the top of the page.
  3. Select your organization or project.
  4. Select a custom role and click Disable.

API


Use the roles.patch() method to disable a custom role.

If you know the role ID of the custom role you wish to disable, get the role using the roles.get() method. Change the stage property of the role to DISABLED, and then call the roles.patch() method to update role.

If you don't know the role ID of the custom role you wish to disable, list all the roles using roles.list() to identify the role. roles.list() returns a list of all roles referencing the resource. Identify the role you wish to disable, change its rolelaunchstage property to DISABLED, and then call the roles.patch() method to update role.

To disable a role, set the following fields:

  • Set name to the full name of the role, organizations/{organization-id}/roles/{role}.
  • In the Role, set the stage to DISABLED.
  • Set the update_mask to 'paths: stage'.

To re-enable the role, follow the same process above as disable the role, but set the stage property of role to ALPHA, BETA, or GA.

Example

name: 'organizations/123456/roles/appengine.customRoleEditor'
role {
   name: 'organizations/123456/roles/appengine.customRoleEditor'`
   stage: 'DISABLED'
}
update_mask {
 paths:  stage
}

Error codes

Error CodeStatus Message
PERMISSION_DENIEDYou don't have permission to update the role
INVALID_ARGUMENTCurated roles cannot be updated.
INVALID_ARGUMENTThe name in the request ([ROLE_NAME]) and the name in the role ([ROLE_NAME]) must match.
INVALID_ARGUMENTPermission [PERMISSION] is not valid.
ABORTEDThere were concurrent policy changes. Please retry the whole read-modify-write with exponential backoff.

Listing the roles

gcloud


Use the gcloud iam roles list command to list custom roles and predefined roles for a project or organization.

Execute the following gcloud command to list custom roles, specifying either project-level or organization-level custom roles:

gcloud iam roles list --project [PROJECT_ID]

[PROJECT_ID] is the name of the project, such as my-project-id. You can also use the --organization [ORGANIZATION_ID] flag if the role was created at the organization level, such as 1234567.

To list deleted roles, you can also specify the --show-deleted flag.

Execute the following gcloud command to list predefined roles:

gcloud iam roles list

Console


  1. Go to the Roles page in the GCP Console.

    Open the Roles page


    All the custom roles for the the project are listed on the page.

API


The roles.list() method can be used to list all custom roles defined in an organization or project; it can also be used to list the predefined roles by setting the parent field in the request to "".

To call roles.list(), set the following field in the request:

  • The parent that you want to use to get all custom roles for, such as
    • projects/{PROJECT_ID}
    • organizations/{ORGANIZATION_ID}

If you want the result to contain the permissions of each role set the view field to RoleView::FULL.

If you want the result to contain roles that have been recently deleted set the show_deleted field to true.

If you want to list all curated roles, set the parent to '' (empty string).

Error codes

Error CodeStatus Message
PERMISSION_DENIEDYou don't have permission to list roles in {path}.
INVALID_ARGUMENTThe parent {path} is invalid. The parent must be in the form organizations/{organization_id}, projects/{project_id} or empty.
INVALID_ARGUMENTRole view is not valid.

Viewing the grantable roles on resources

gcloud


Use the gcloud iam list-grantable-roles command to return a list of all roles that can be applied to a given resource.

Execute the following gcloud command to list grantable roles:

gcloud iam list-grantable-roles [RESOURCE]

[RESOURCE] is the full resource name for the desired resource, such as //cloudresourcemanager.googleapis.com/projects/my-project.

Depending on the desired resource, a large number of roles may be returned. To limit the results, you can also specify a filter expression.

Console


  1. Open the IAM page in the GCP Console.

    Open the IAM page

  2. Click on "Select a project" drop-down at the top of the page.
  3. Select the project or organization for which you want to view roles.
  4. Click Add.
  5. Enter the member email in Members.

The Roles drop-down displays all the roles (including the custom roles) that you can grant to the member.

API


QueryGrantableRoles returns a list of all roles that a resource can reference. It does not include roles that have no permissions. The only required parameter in the request is the full resource name such as //cloudresourcemanager.googleapis.com/projects/my-project. Optionally, the caller can provide a RoleView, which determines if the Role includes all permissions that the role contains in the response.

Example

full_resource_name: '//automatedtests.googleapis.com/projects/my-project/buckets/bucket1'`

Error codes

Error CodeStatus Message
INVALID_ARGUMENT{full_resource_name} must be specified
INVALID_ARGUMENT{full_resource_name}, does not match //[a-z0-9.-]/.

Deleting a custom role

gcloud


Use the gcloud iam roles delete command to delete a custom role. The role is suspended and cannot be used to create new IAM policy bindings.

Execute the following gcloud command to delete a custom roles:

gcloud iam roles delete [ROLE_ID] --project [PROJECT_ID]

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id. You can also use the --organization [ORGANIZATION_ID] flag if the role was created at the organization level, such as 1234567.

The role will not be included in gcloud iam roles list, unless the --show_deleted flag is included. Deleted roles are indicated by the deleted: true block in a list response, such as:

---
deleted: true
description: My custom role description.
etag: BwVkB5NLIQw=
name: projects/my-project-id/roles/viewer
title: Role Viewer
---

After the role has been deleted, existing bindings remain, but are inactive. The role can be undeleted within 7 days. After 7 days, the role enters a permanent deletion process that lasts 30 days.

During this process, the role and all bindings associated with the role are permanently removed, and you cannot create a new role with the same role ID. After the role has been permanently deleted -- which takes 37 days from the initial deletion request -- a new role can be created using the deleted role ID.

Console


  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Select the role you wish to delete and click on the trash icon on top of the page.

The role is suspended and cannot be used to create new IAM policy bindings. Existing bindings remain, but are inactive. The role can be undeleted within 7 days. After 7 days, the role enters a permanent deletion process that lasts 30 days.

During this process, the role and all bindings associated with the role are permanently removed, and you cannot create a new role with the same role ID. After the role has been permanently deleted -- which takes 37 days from the initial deletion request -- a new role can be created using the deleted role ID.

API


roles.delete deletes a role. The role is suspended and cannot be used to create new IAM policy bindings.

The name can be in the following formats

  • organizations/{ORGANIZATION_ID}/roles/{ROLE_NAME}
  • projects/{PROJECT_ID}/roles/{ROLE_NAME}

The role will not be included in list, unless show_deleted is set in the request. The role will contain the deleted boolean and it is set to true if the role is in this state.

Existing bindings remain, but are inactive. The role can be undeleted within 7 days. After 7 days, the role enters a permanent deletion process that lasts 30 days.

During this process, the role and all bindings associated with the role are permanently removed, and you cannot create a new role with the same role ID. After the role has been permanently deleted -- which takes 37 days from the initial deletion request -- a new role can be created using the deleted role ID.

Error codes

Error CodeStatus Message
PERMISSION_DENIEDYou don't have permission to delete the role at {path}.
FAILED_PRECONDITIONYou can't delete role {ROLE_NAME} as it is already deleted.
FAILED_PRECONDITIONYou can't delete a role {ROLE_NAME} that is reserved.
INVALID_ARGUMENTCurated roles cannot be in a deleted state.

Undeleting a custom role

gcloud


Use the gcloud iam roles undelete command to undelete a custom role. When you undelete a role, it's returned back to its previous state.

The role can only be undeleted within 7 days of deletion. After 7 days the role is permanently deleted and all bindings associated with the role are removed.

Execute the following gcloud command to undelete a custom role:

gcloud iam undelete [ROLE_ID] --project [PROJECT_ID]

Each of the placeholder values is described below:

  • [ROLE_ID] is the name of the role, such as viewer.
  • [PROJECT_ID] is the name of the project, such as my-project-id. You can also use the --organization [ORGANIZATION_ID] flag if the role was created at the organization level, such as 1234567.

The following example demonstrates how to undelete a custom role:

gcloud iam roles undelete viewer --project my-project-id

If the role was undeleted successfully, the following response is returned:

description: My custom role description.
etag: BwVkCAx9W6w=
includedPermissions:
- iam.roles.get
- iam.roles.list
name: projects/my-project-id/roles/viewer
stage: ALPHA
title: Role Viewer

Console


  1. Go to the Roles page in the GCP Console.

    Open the Roles page

  2. Locate the role you wish to undelete, click on the more icon at the end of the row, and click Undelete.

The role can only be undeleted within 7 days of deletion. After 7 days the role is permanently deleted and all bindings associated with the role are removed.

API


roles.undelete brings a role back to its previous state.

The name can be in the following formats

  • organizations/{ORGANIZATION_ID}/roles/{ROLE_NAME}
  • projects/{PROJECT_ID}/roles/{ROLE_NAME}

The role can only be undeleted within 7 days of deletion. After 7 days the role is permanently deleted and all bindings associated with the role are removed.

Error codes

Error CodeStatus Message
PERMISSION_DENIEDYou don't have permission to undelete the role at {path}.
FAILED_PRECONDITIONA role that is not deleted cannot be undeleted.
FAILED_PRECONDITIONYou can't undelete a role {ROLE_NAME} which is reserved.
INVALID_ARGUMENTPredefined roles cannot be undeleted.

Send feedback about...

Cloud Identity and Access Management Documentation