
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 end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Initialize Google Cloud SDK
/ 10
Create an instance with name as lab-1 in Project 1
/ 5
Update the default zone
/ 5
Create a configuration for Username 2 and name it as user2
/ 10
Restricting Username 2 to roles/viewer in Project 2
/ 5
Create a new role with permissions for the devops team
/ 10
Check binding to roles/iam.serviceAccountUser
/ 5
Bound Username 2 to devops role
/ 10
Create an instance with name as lab-2 in Project 2
/ 5
Check the created service account
/ 5
Check the binding for the service account to roles/iam.serviceAccountUser
/ 10
Check the binding for the service account to roles/compute.instanceAdmin
/ 10
Check lab-3 has the service account attached
/ 10
As a cloud professional, one of the most fundamental concerns in setting up your cloud environment is how you configure access to your resources following the principle of least privilege. Some key questions include:
This lab delves into the fundamentals of Identity and Access Management (IAM) within Google Cloud. You'll learn how to strategically configure access to cloud resources, ensuring adherence to the principle of least privilege. The focus is on using command-line tools to manage user permissions, granting only necessary access, and setting up secure authentication mechanisms for applications and services.
If you're familiar with Azure IAM, this lab will translate those concepts to the Google Cloud environment. You'll explore Google Cloud IAM's unique approach to roles and permissions. The lab emphasizes hands-on practice with the gcloud command-line tool, covering its installation, configuration, and the management of multiple configurations and service accounts.
In this lab, you will learn how to:
gcloud
clientYou start with two user accounts and two projects; user1
is the "owner" of both projects and user2
is the "viewer" of only the first project. There is a Linux virtual machine (vm) running in the first project.
Google Cloud offers Cloud Identity and Access Management (IAM), which lets you manage access control by defining who (identity) has what access (role) for which resource.
With Cloud IAM you can grant granular access to specific Google Cloud resources and prevent unwanted access to other resources. Cloud IAM lets you adopt the security principle of least privilege, so you grant only the necessary access to your resources.
In Cloud IAM, you grant access to members. Members can be of the following types:
Learn more about these identity types from the Concepts related to identity Guide.
In this lab, you use Google accounts, service accounts, and Cloud Identity domain groups.
A role is a collection of permissions. You cannot assign a permission to the user directly; instead you grant them a role. When you grant a role to a user, you grant them all the permissions that the role contains.
There are three kinds of roles in Cloud IAM:
Basic roles: The roles historically available in the Cloud Console will continue to work. These are the Owner, Editor, and Viewer roles.
Predefined roles: Predefined roles are the Cloud IAM roles that give finer-grained access control than the basic roles. For example, the predefined role Pub/Sub Publisher (roles/pubsub.publisher) provides access to only publish messages to a Cloud Pub/Sub topic.
Custom roles: Roles that you create to tailor permissions to the needs of your organization when predefined roles don't meet your needs.
Learn more about roles from the Roles Guide.
The gcloud CLI is a part of the Cloud SDK. You must download and install the SDK on your system and initialize it before you can use the gcloud command-line tool. You can use this tool to perform many common platform tasks either from the command-line or in scripts and other automations.
By default, the SDK installs those gcloud CLI commands that are at the General Availability and Preview levels only. Additional functionality is available in SDK components named alpha and beta. These components allow you to use the gcloud CLI to work with Cloud Bigtable, Google Cloud Dataflow and other parts of the Cloud Platform at earlier release levels than General Availability.
Learn more about gcloud from the gcloud CLI overview Guide.
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.
Your first step is to connect to an existing Google Cloud compute instance then download, install, and configure the gcloud SDK.
The gcloud SDK has a number of utilities that enable administration of the environment. First you configure authentication to provide the utility permission to perform actions. The authentication process differs based on the identity you use. In this example you use your lab-provided identity (which is a user account). Once you are authenticated, you also need to configure the environment to define the project you act upon.
Learn more about authorizing Cloud SDK tools from the Authorize the gcloud CLI Guide.
This lab already has a Compute Engine instance called centos-clean that simulates an environment that has gcloud
installed. You connect to this instance using the Cloud Console.
Open the list of compute instances by going to Navigation Menu > Compute Engine > VM instances.
On the line with the compute instance named centos-clean, click SSH.
gcloud
already installed. Inside the SSH session run:gcloud
environment. Inside the SSH session run:You are presented with a number of prompts.
For You must log in to continue. and Do you want to continue (Y/n)?, press ENTER.
Navigate to the link under the prompt Go to the following link in your browser: in a new tab.
Use the credentials for this lab to authenticate the gcloud
environment. Because you are using a Compute Engine instance you would normally use a service account. Ignore the suggestion and continue because you are simulating your own workstation environment. You look at using a service account later in this lab.
The link will open a Sign in with Google web page. Click on the account provided for this lab (if you are unsure, please look to the top left on this page).
Click Allow.
You are accepting that the Cloud SDK will have the same access as your Google account.
When you see the prompt Enter the following verification code in gcloud CLI on the machine you want to log into, click on the copy button, go back to the SSH session, and paste the code into the prompt Enter authorization code:.
If asked, Pick cloud project to use:. locate your current project (on your web console at the top you see the Project ID) in the list, then type the number for your project to select it from the list of options.
The initialization will complete and you see the zone and region are set for you. You change both in the next task.
Certain Compute Engine resources live in regions and zones. A region is a specific geographical location where you can run your resources. Each region has one or more zones.
Run the following gcloud commands in Cloud Shell to set the default region and zone for your lab:
Once you have the gcloud
command-line tool installed and basically configured, make some changes by creating a compute instance.
But what size? And where? What image will be used?
There are a number of defaults the service uses. Some can be controlled in the gcloud
configuration. For example, the location of the instance is controlled by the zone setting.
You see a compute
section, a core
section, and an active configuration
. You can change each of these, but for this lab you'll only change the zone. Look at the zone your VM was created in.
Select one of the other zones in the same region as you. For example, if your zone is us-west2-a
, you could select us-west2-b
.
Change your current zone for another zone in the same region. Inside the SSH session run the following, replacing ZONE
with the zone you selected:
You see the stated zone reflects the change you made.
You can change other settings using the gcloud config set
command. Those changes are permanent; they are written to your home directory.
The default configuration is stored in ~/.config/gcloud/configurations/config_default.
If you want to use a zone other than the default zone when creating an instance, you can use --zone switch. For example, gcloud compute instances create lab-1 --zone us-central1-f
You can see the configuration is just stored as text and can be backed up or copied.
You have now successfully configured gcloud
.
You have now set up one account. In situations when you need to work on different teams or access different accounts, you can also manage that with gcloud config
.
In your next task you learn how to create a second configuration and switch between both of them.
In this lab you have a second Google account you can log on with. This account has read-only (viewer) access to the first project. You now create a new configuration for that user.
gcloud
configuration for the second user account. Inside the SSH session run:Select option 2, Create a new configuration.
configuration name: Type user2.
Log in with a new account: select option 3 - you're logging in with the other provided user name.
Press ENTER when you see the prompt Do you want to continue (Y/n)?
Navigate to the link displayed in a new tab.
Click Use another account
Copy the second user account from this page (left side), and paste into the email or phone prompt.
Copy the same password that you started the lab with, and paste into the enter your password prompt.
Click I understand.
Click Allow.
You are accepting that the Cloud SDK will have the same access as your Google account.
When you see the prompt Enter the following verification code in gcloud CLI on the machine you want to log into, click on the copy button then go back to the SSH session and paste the code into the prompt Enter authorization code:.
On Pick cloud project to use: locate your current project (on your web console at the top you see the project ID) and then type in the number that corresponds to the project.
The initialization will complete and you see the zone and region are set for you.
This new account has viewer only access to the project, so you can test that you are indeed using this account by trying to view and then create some resources.
The second user account has viewer access so you see centos-clean
and lab-1
instances listed.
Because the second user account has only viewer access, they are not allowed to create an instance, so this command will fail. It will take a little time to fail.
You are now back to using your original user account credentials. Later you need to switch between these two accounts as you learn about roles and permissions.
You have been provided two user accounts for this project. The first user has complete control of both projects and can be thought of as the admin account. The second user has viewer only access to the two projects. Call the second user a devops user and that user identity represents a typical devops level user.
Next you use gcloud
to configure access to one project for the devops user by creating a custom role for the project that permits creation of buckets and instances.
The list of roles is returned. The addition of grep "name:"
to the command reduces the amount of data returned to just the names of the roles.
Inspect one of these roles to see the permissions assigned to the role. To view the permissions use gcloud iam roles describe
. Try looking at the simple role roles/compute.instanceAdmin.
compute.instanceAdmin
predefined role. Inside the SSH session run:You can see roles/compute.instanceAdmin has many permissions, but these are the minimum needed for later:
To review the full list of roles and the permissions assigned, refer to the IAM permissions reference Guide.
Now that you know that roles contain permissions, how do you assign a role (and therefore all the associated permissions), to a user account?
There are two ways to attach a role:
Next you attach the basic role of "viewer" to the second user onto the second project.
gcloud
configuration back to the second user (user2). Inside the SSH session run:Now you're back to user2
.
bashrc
file, so be careful.You get a warning: WARNING: You do not appear to have access to project [your 2nd project id] or it does not exist.
N
and press ENTER.This means that you don't have access to the PROJECTID2 project, which you fix shortly.
You need to switch back to the first user, which has the permission to grant access to the second user.
Set the value of USERID2
to the second user name and bind the role of viewer to the second user onto the second project.
First, Install jq
:
Once you have run the command you see (you might need to scroll up), the text that looks something like:
You should not see an error message this time.
You see 0 instances in this project.
This command will fail because user2 only has viewer access to the project.
You are now back to using your original user account credentials.
Next, create the new role with the set of permissions needed for the devops team.
devops
that has the permissions to create an instance. Inside the SSH session run:This command creates a custom role in the project called devops
with the permissions to create and manage instances.
The full name of the role is listed, note the role is in the project so the path is in the pattern of projects/PROJECT/roles/ROLENAME
.
You now have the role created and need to bind the user and the role to the project. You use gcloud projects add-iam-policy-binding
to perform the binding. To make this command easier to execute, set a couple of environment variables first; the project id and the user account.
iam.serviceAccountUser
to the second user onto the second project. Inside the SSH session run:You need permissions to create an instance with a service account attached. The role iam.serviceAccountUser
has those permissions, so use this pre-defined role.
devops
to the second user onto the second project. You can find the second user account on the left of this page. Make sure you set USERID to the second user account.Inside the SSH session run:
Once you have run the command you see (you might need to scroll up), the text that looks something like the example below:
Now you're back to user2.
Now the instance creation works for user2.
After these last changes your environment will look like this.
You have seen how to authenticate and use gcloud
to access Google Cloud services with roles. Now you'll look at a typical approach.
You have an application that will use the Application Programming Interfaces (APIs) to read and write to Cloud Storage buckets. You don't want to have to authenticate every time you launch a new server, that would be both painful and not in the spirit of using the cloud! So, you use service accounts.
A service account is a special Google account that belongs to your application or a virtual machine (VM) instead of to an individual end user. Your application uses the service account to call the Google API of a service so that the users aren't directly involved.
Learn more about service accounts from the Service accounts Guide.
Now you create a service account, use that service account with a compute instance, then test that the service account allows the access you need.
user2
doesn't have the rights to set up and configure service accounts. Inside the SSH session run:PROJECTID2
in your configuration. Inside the SSH session run:Make sure you are targeting the right project.
SA
. Inside the SSH session run:This command sets the SA local variable to the email address of the service account. Pretty useful right?
iam.serviceAccountUser
. Inside the SSH session run:This role allows the service account to assign a service account to a compute instance.
compute.instanceAdmin
. Inside the SSH session run:This role allows the service account to manage compute instances.
Access scopes are the legacy method of specifying permissions for your instance. Access scopes are not a security mechanism. Instead, they define the default OAuth scopes used in requests from the gcloud
tool or the client libraries. They have no effect when making requests not authenticated through OAuth, such as gRPC or the SignBlob APIs.
You must set up access scopes when you configure an instance to run as a service account.
A best practice is to set the full cloud-platform access scope on the instance, then securely limit the service account's API access with IAM roles.
Access scopes apply on a per-instance basis. You set access scopes when creating an instance and the access scopes persist only for the life of the instance.
Access scopes have no effect if you have not enabled the related API on the project that the service account belongs to. For example, granting an access scope for Cloud Storage on a virtual machine instance allows the instance to call the Cloud Storage API only if you have enabled the Cloud Storage API on the project.
gcloud compute ssh
. Inside the SSH session run:Press ENTER when asked if you want to continue.
Press ENTER twice to skip making a password.
gcloud
configuration. Inside the SSH session run:You see the configuration has the service account
You can press ENTER to accept the default zone for this VM.
Because the service account has permissions, you can see the instances listed.
Using the Cloud SDK tool (gcloud
), you've successfully installed and configured the gcloud client, managed multiple IAM configurations, assigned appropriate IAM permissions, and worked with a service account. These tasks demonstrate the similarities between Google Cloud IAM and Azure IAM when using command-line tools for access control. Both interfaces allow you to provision accounts/roles, create service accounts/roles, and switch users.
While there are similarities between Azure IAM and Google Cloud IAM with respect to role-based access control (RBAC), there are differences as well. One key difference you explored in the lab is the process for service account creation, because you do not register accounts and create service principal names in Google Cloud. Projects in Google Cloud work like tenants in Azure. You used the gcloud command line tool to provision accounts across multiple projects similar to how you would use the Azure CLI to provision access across multiple tenants.
...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 July 04, 2024
Lab Last Tested July 04, 2024
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.
Diese Inhalte sind derzeit nicht verfügbar
Bei Verfügbarkeit des Labs benachrichtigen wir Sie per E-Mail
Sehr gut!
Bei Verfügbarkeit kontaktieren wir Sie per E-Mail
One lab at a time
Confirm to end all existing labs and start this one