![](https://cdn.qwiklabs.com/assets/labs/start_lab-f45aca49782d4033c3ff688160387ac98c66941d.png)
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you restart it, you'll have to start from the beginning.
- On the top left of your screen, click Start lab to begin
Create a custom role using a YAML file
/ 20
Create a custom role using flags
/ 20
Update a custom role using a YAML file
/ 15
Update a custom role using flags
/ 15
Disabling a custom role
/ 15
Undeleting a custom role
/ 15
Cloud IAM provides the right tools to manage resource permissions with minimum fuss and high automation. You don't directly grant users permissions. Instead, you grant them roles, which bundle one or more permissions. This allows you to map job functions within your company to groups and roles. Users get access only to what they need to get the job done, and admins can easily grant default permissions to entire groups of users.
There are two kinds of roles in Cloud IAM:
Predefined roles are created and maintained by Google. Their permissions are automatically updated as necessary, such as when new features or services are added to Google Cloud.
Custom roles are user-defined, and allow you to bundle one or more supported permissions to meet your specific needs. Custom roles are not maintained by Google; when new permissions, features, or services are added to Google Cloud, your custom roles will not be updated automatically.You create a custom role by combining one or more of the available Cloud IAM permissions. Permissions allow users to perform specific actions on Google Cloud resources.
In this lab, you create, update, delete, and restore custom roles.
Familiarity with IAM roles is recommended to optimize your learning.
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Run the following command to set the region for your project:
Cloud IAM also provides the ability to create customized Cloud IAM roles. You can create a custom Cloud IAM role with one or more permissions and then grant that custom role to users. Cloud IAM provides a UI and API for creating and managing custom roles.
Key Point: Custom roles enable you to enforce the principle of least privilege, ensuring that the user and service accounts in your organization have only the permissions essential to performing their intended functions.
You create a custom role by combining one or more of the available Cloud IAM permissions. Permissions allow users to perform specific actions on Google Cloud resources.
In the Cloud IAM world, permissions are represented in the form:
For example, the compute.instances.list
permission allows a user to list the Compute Engine instances they own, while compute.instances.stop
allows a user to stop a VM.
Permissions usually, but not always, correspond 1:1 with REST methods. That is, each Google Cloud service has an associated permission for each REST method that it has. To call a method, the caller needs that permission. For example, the caller of topic.publish()
needs the pubsub.topics.publish
permission.
Custom roles can only be used to grant permissions in policies for the same project or organization that owns the roles or resources under them. You cannot grant custom roles from one project or organization on a resource owned by a different project or organization.
To create a custom role, a caller must have the iam.roles.create
permission.
Users who are not owners, including organization administrators, must be assigned either the Organization Role Administrator role (roles/iam.organizationRoleAdmin) or the IAM Role Administrator role (roles/iam.roleAdmin). The IAM Security Reviewer role (roles/iam.securityReviewer) enables the ability to view custom roles but not administer them.
The custom roles user interface is in the Cloud Console under IAM Roles. It is only available to users who have permissions to create or manage custom roles. By default, only project owners can create new roles. Project owners can control access to this feature by granting IAM Role Administrator role to others on the same project; for organizations, only Organization Administrators can grant the Organization Role, Administrator role.
Before you create a custom role, you might want to know:
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.
Output:
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 Cloud Console or the IAM API.
[ROLE_NAME]
with the role. For example: roles/viewer
or roles/editor
:Example output (for roles/viewer):
Use the gcloud iam list-grantable-roles
command to return a list of all roles that can be applied to a given resource.
gcloud
command to list grantable roles from your project:Your output will look something like this:
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.
Use the gcloud iam roles create
command to create new custom roles in two ways:
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.
In the next sections you create custom roles at the project level.
Create a YAML file that contains the definition for your custom role. The file must be structured in the following way:
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.Then save and close the file by pressing CTRL+X, Y and then ENTER.
Execute the following gcloud
command:
If the role was created successfully, the following response is returned:
Click Check my progress to verify the objective.
Now you'll use the flag method to create a new custom role. The flags take a similar form to the YAML file, so you’ll recognize how the command is being built.
gcloud
command to create a new role using flags:Example output:
Click Check my progress to verify the objective.
gcloud
command to list custom roles, specifying either project-level or organization-level custom roles:Example output:
To list deleted roles, you can also specify the --show-deleted
flag.
gcloud
command to list predefined roles: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.
Use the gcloud iam roles update
command to update custom roles in one of two ways:
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.
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.
gcloud
command, replacing [ROLE_ID]
with editor.The describe
command returns the following output:
Copy the output to use to create a new YAML file in the next steps.
Create a new-role-definition.yaml
file with your editor:
includedPermissions
:Your YAML file looks like this when you're finished:
Save and close the file CTRL+X, Y and then ENTER.
Now you’ll use the update
command to update the role. Execute the following gcloud
command, replacing [ROLE_ID]
with editor:
If the role was updated successfully, the following response is returned:
Click Check my progress to verify the objective.
Each part of a role definition can be updated using a corresponding flag. For a list of all possible flags from the SDK reference documentation, see the topic gcloud iam roles update.
Use the following flags to add or remove permissions:
--add-permissions
: Adds one or more comma-separated permissions to the role.--remove-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.
gcloud
command to add permissions to the viewer role using flags:If the role was updated successfully, the following response is returned:
Click Check my progress to verify the objective.
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.
The easiest way to disable an existing custom role is to use the --stage
flag and set it to DISABLED.
gcloud
command to disable the viewer role:If the role was updated successfully, the following response is returned:
Click Check my progress to verify the objective.
gcloud iam roles delete
command to delete a custom role. Once deleted the role is inactive and cannot be used to create new IAM policy bindings:Example output:
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. After 37 days, the Role ID is available to be used again.
--stage
flag:Click Check my progress to verify the objective.
In this lab, you created and managed custom roles in IAM.
Learn more about IAM from the IAM article, Cloud Identity and Access Management.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated April 15, 2024
Lab Last Tested July 13, 2023
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.