arrow_back

Getting Started with Cloud Shell and gcloud

Join Sign in

Getting Started with Cloud Shell and gcloud

45 minutes 1 Credit

GSP002

Google Cloud Skills Boost logo

Overview

Cloud Shell provides you with command-line access to computing resources hosted on Google Cloud. Cloud Shell is a Debian-based virtual machine with a persistent 5-GB home directory, which makes it easy for you to manage your Google Cloud projects and resources. The gcloud command-line tool and other utilities you need are pre-installed in Cloud Shell, which allows you to get up and running quickly.

In this hands-on lab, you learn how to connect to computing resources hosted on Google Cloud via Cloud Shell with the gcloud tool.

You are encouraged to type the commands themselves, which reinforces the core concepts. Many labs will include a code block that contains the required commands. You can easily copy and paste the commands from the code block into the appropriate places during the lab.

What you'll do

  • Practice using gcloud commands.
  • Connect to compute services hosted on Google Cloud.

Prerequisites

  • Familiarity with standard Linux text editors such as vim, emacs, or nano.

Setup

Before you click the Start Lab button

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 will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

What you need

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
  • Time to complete the lab.

Note: If you already have your own personal Google Cloud account or project, do not use it for this lab.

Note: If you are using a Chrome OS device, open an Incognito window to run this lab.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.

    Open Google Console

  2. Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Sign in

    Tip: Open the tabs in separate windows, side-by-side.

  3. In the Sign in page, paste the username that you copied from the left panel. Then copy and paste the password.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Training credentials. If you have your own Google Cloud account, do not use it for this lab (avoids incurring charges).

  4. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Activate Cloud Shell

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.

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

Click Continue.

cloudshell_continue.png

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

You can list the active account name with this command:

gcloud auth list

(Output)

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`

You can list the project ID with this command:

gcloud config list project

(Output)

[core] project = <project_ID>

(Example output)

[core] project = qwiklabs-gcp-44776a13dea667a6

After Cloud Shell is activated, you can use the command line to invoke the Cloud SDK gcloud tool or other tools available on the virtual machine instance. Later in the lab, you will use your $HOME directory, which is used in persistent disk storage to store files across projects and between Cloud Shell sessions. Your $HOME directory is private to you and cannot be accessed by other users.

Task 1: Configure your environment

In this section, you'll learn about aspects of the development environment that you can adjust.

Understanding regions and zones

Certain Google Compute Engine resources live in regions or zones. A region is a specific geographical location where you can run your resources. Each region has one or more zones. For example, the us-central1 region denotes a region in the Central United States that has zones us-central1-a, us-central1-b, us-central1-c, and us-central1-f. The following image shows zones in their respective regions:

regions_and_zones.png

Resources that live in a zone are referred to as zonal resources. Virtual machine instances and persistent disks live in a zone. If you want to attach a persistent disk to a virtual machine instance, both resources must be in the same zone. Similarly, if you want to assign a static IP address to an instance, the instance must be in the same region as the static IP address.

Learn more about regions and zones and see a complete list in Regions and Zones documentation.
  1. Set the region to us-central1
gcloud config set compute/region us-central1
  1. To view the project region setting, run the following command:
gcloud config get-value compute/region
  1. Set the zone to us-central1-b
gcloud config set compute/zone us-central1-b
  1. To view the project zone setting, run the following command:
gcloud config get-value compute/zone

Finding project information

  1. Copy your project ID to your clipboard or text editor. The project ID is listed in 2 places:

  • In the Google Cloud Console, on the Dashboard, under Project info. (Click Navigation menu (Navigation menu), and then click Home > Dashboard.)

  • On the Qwiklabs tab near your username and password.

  1. In Cloud Shell, run the following gcloud command, to view the project id for your project:
gcloud config get project
  1. In Cloud Shell, run the following gcloud command to view details about the project:

gcloud compute project-info describe --project $(gcloud config get project)

Find the zone and region metadata values in the output. You'll use the zone (google-compute-default-zone) from the output later in this lab.

When the google-compute-default-region and google-compute-default-zone keys and values are missing from the output, no default zone or region is set.

The output includes other useful information regarding your project. Take sometime to explore this in more detail.

Setting environment variables

Environment variables define your environment and help save time when you write scripts that contain APIs or executables.

  1. Create an environment variable to store your Project ID, replacing <your_project_ID> with the value for name from the gcloud compute project-info describe command you ran earlier:

export PROJECT_ID=$(gcloud config get project)
  1. Create an environment variable to store your Zone, replacing <your_zone> with the value for zone from the gcloud compute project-info describe command you ran earlier:

export ZONE=$(gcloud config get-value compute/zone)
  1. To verify that your variables were set properly, run the following commands:

echo -e "PROJECT ID: $PROJECT_ID\nZONE: $ZONE"

If the variables were set correctly, the echo commands will output your Project ID and Zone.

Creating a virtual machine with the gcloud tool

Use the gcloud tool to create a new virtual machine (VM) instance.

  1. To create your VM, run the following command:

gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone $ZONE

Output:

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-04-326fae68bc3d/zones/us-central1-b/instances/gcelab2]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS gcelab2 us-central1-b n1-standard-2 10.128.0.2 34.67.152.90 RUNNING

Command details

  • gcloud compute allows you to manage your Compute Engine resources in a format that's simpler than the Compute Engine API.

  • instances create creates a new instance.

  • gcelab2 is the name of the VM.

  • The --machine-type flag specifies the machine type as n1-standard-2.

  • The --zone flag specifies where the VM is created.

  • If you omit the --zone flag, the gcloud tool can infer your desired zone based on your default properties. Other required instance settings, such as machine type and image, are set to default values if not specified in the create command.

Test completed task

Click Check my progress to verify your performed task. If you have successfully created a virtual machine with the gcloud tool, an assessment score is displayed.

Create a virtual machine with gcloud
  • To open help for the create command, run the following command:

gcloud compute instances create --help Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

Explore gcloud commands

The gcloud tool offers simple usage guidelines that are available by adding the -h flag (for help) onto the end of any gcloud command.

  1. Run the following command:

gcloud -h

You can access more verbose help by appending the --help flag onto a command or running the gcloud help command.

  1. Run the following command:

gcloud config --help Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.
  1. Run the following command:

gcloud help config

The results of the gcloud config --help and gcloud help config commands are equivalent. Both return long, detailed help.

Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

gcloud Global Flags govern the behavior of commands on a per-invocation level. Flags override any values set in SDK properties.

  1. View the list of configurations in your environment:

gcloud config list
  1. To see all properties and their settings:

gcloud config list --all
  1. List your components:

gcloud components list

This command displays the gcloud components that are ready for you to use in this lab.

Task 2: Filtering command line output

The gcloud cli is a powerful tool for working at the command line. Sometime you may want specific information to be displayed.

  1. List the compute instance available in the project
gcloud compute instances list

Having multiple resource deployed in a project is very common. Fortunately gcloud has some clever formatting that can help identify specific resources.

Output:

NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS cloudlearningservices us-central1-a e2-standard-2 10.128.0.2 35.202.228.143 RUNNING gcelab2 us-central1-b n1-standard-2 10.128.0.2 34.68.7.186 RUNNING
  1. List the gcelab2 virtual machine
gcloud compute instances list --filter="name=('gcelab2')"

Output:

NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS gcelab2 us-central1-b n1-standard-2 10.128.0.2 34.68.7.186 RUNNING

In the above command we have asked gcloud to only show the information matching the criteria i.e. a virtual instance name matching the criteria.

  1. List the Firewall rules in the project
gcloud compute firewall-rules list

Output:

NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-icmp default INGRESS 65534 icmp False default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False default-allow-rdp default INGRESS 65534 tcp:3389 False default-allow-ssh default INGRESS 65534 tcp:22 False dev-net-allow-ssh dev-network INGRESS 1000 tcp:22 False serverless-to-vpc-connector dev-network INGRESS 1000 icmp,udp:665-666,tcp:667 False vpc-connector-egress dev-network INGRESS 1000 icmp,udp,tcp False vpc-connector-health-check dev-network INGRESS 1000 tcp:667 False vpc-connector-to-serverless dev-network EGRESS 1000 icmp,udp:665-666,tcp:667 False
  1. List the Firewall rules for the default network
gcloud compute firewall-rules list --filter="network='default'"

Output:

NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-icmp default INGRESS 65534 icmp False default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False default-allow-rdp default INGRESS 65534 tcp:3389 False default-allow-ssh default INGRESS 65534 tcp:22 False
  1. List the Firewall rules for the default network where the allow rule matches an ICMP rule
gcloud compute firewall-rules list --filter="NETWORK:'default' AND ALLOW:'icmp'"

Output:

NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-icmp default INGRESS 65534 icmp False default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False

Task 3: Connecting to your VM instance

gcloud compute makes connecting to your instances easy. The gcloud compute ssh command provides a wrapper around SSH, which takes care of authentication and the mapping of instance names to IP addresses.

  1. To connect to your VM with SSH, run the following command:

gcloud compute ssh gcelab2 --zone $ZONE

Output:

WARNING: The public SSH key file for gcloud does not exist. WARNING: The private SSH key file for gcloud does not exist. WARNING: You do not have an SSH key for gcloud. WARNING: [/usr/bin/ssh-keygen] will be executed to generate a key. This tool needs to create the directory [/home/gcpstaging306_student/.ssh] before being able to generate SSH Keys. Do you want to continue? (Y/n)
  1. To continue, type Y.

Generating public/private rsa key pair. Enter passphrase (empty for no passphrase)
  1. To leave the passphrase empty, press ENTER twice.

You have connected to the virtual machine created earlier in the lab. Did you notice how the command prompt changed?

The prompt now says something similar to sa_107021519685252337470@gcelab2.

  • The reference before the @ indicates the account being used.
  • After the @ sign indicates the host machine being accessed.
  1. Install nginx web server on to virtual machine
sudo apt install -y nginx
  1. You don't need to do anything here, so to disconnect from SSH and exit the remote shell, run the following command:
exit

You should be back at your project's command prompt.

Task 4: Updating the Firewall

When using compute resources such as virtual machines, its important to understand the associated firewall rules.

  1. List the firewall rules for the project

gcloud compute firewall-rules list

Output:

NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-icmp default INGRESS 65534 icmp False default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False default-allow-rdp default INGRESS 65534 tcp:3389 False default-allow-ssh default INGRESS 65534 tcp:22 False dev-net-allow-ssh dev-network INGRESS 1000 tcp:22 False serverless-to-vpc-connector dev-network INGRESS 1000 icmp,udp:665-666,tcp:667 False vpc-connector-egress dev-network INGRESS 1000 icmp,udp,tcp False vpc-connector-health-check dev-network INGRESS 1000 tcp:667 False vpc-connector-to-serverless dev-network EGRESS 1000 icmp,udp:665-666,tcp:667 False

From the above we can see we have two networks available. The default network is where our virtual machine gcelab2 is located.

  1. Try to access the nginx service running on the gcelab2 virtual machine

Communication with the virtual machine will fail as it does not have an appropriate firewall rule. Our nginx web server is expecting to communication on tcp:80. To get communication working we need to:

  • Add a tag to the gcelab2 virtual machine
  • Add a firewall rule for http traffic
  1. Add a tag to the virtual machine
gcloud compute instances add-tags gcelab2 --tags http-server,https-server
  1. Update the firewall rule to allow
gcloud compute firewall-rules create default-allow-http --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:80 --source-ranges=0.0.0.0/0 --target-tags=http-server
  1. List the firewall rules for the project
gcloud compute firewall-rules list --filter=ALLOW:'80'

Output:

NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-http default INGRESS 1000 tcp:80 False
  1. Verify communication is possible for http to the virtual machine

curl http://$(gcloud compute instances list --filter=name:gcelab2 --format='value(EXTERNAL_IP)')

You will see the default nginx output.

Task 5: Viewing the system logs

Viewing logs is essential to understanding the working of your project. Use gcloud to access the different logs available on Google Cloud.

  1. View the available logs on the system:

gcloud logging logs list

Output:

NAME projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/GCEGuestAgent projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/OSConfigAgent projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/autoscaler.googleapis.com%2Fstatus_change projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/cloudaudit.googleapis.com%2Factivity projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/cloudaudit.googleapis.com%2Fsystem_event projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/compute.googleapis.com%2Fautoscaler projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/compute.googleapis.com%2Finstance_group_manager_events projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/compute.googleapis.com%2Fshielded_vm_integrity projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/run.googleapis.com%2Frequests projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/run.googleapis.com%2Fstderr projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/run.googleapis.com%2Fstdout
  1. View the logs that relate to compute resources

gcloud logging logs list --filter="compute"

Output:

NAME projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/compute.googleapis.com%2Fautoscaler projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/compute.googleapis.com%2Finstance_group_manager_events projects/qwiklabs-gcp-03-6bef54ddd5e7/logs/compute.googleapis.com%2Fshielded_vm_integrity
  1. Read the logs related to the resource type of gce_instance
gcloud logging read "resource.type=gce_instance" --limit 5
  1. Read the logs for a specific virtual machine

gcloud logging read "resource.type=gce_instance AND labels.instance_name='gcelab2'" --limit 5

Test your understanding

The following multiple-choice question should reinforce your understanding of this lab's concepts.

Congratulations!

You learned how to launch Cloud Shell and run some sample gcloud commands.

Finish your Quest

completion badge sdk completion badge

This self-paced lab is part of the Qwiklabs Google Cloud Essentials and Using the Cloud SDK Command Line Quests. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in a Quest and get immediate completion credit for taking this lab. See other available Qwiklabs Quests.

Take your next lab

Continue your Quest with Provision Services with Google Cloud Marketplace, or check out these suggestions:

Next steps/learn more

Google Cloud Training & Certification

...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 March 20, 2022
Lab Last Tested March 20, 2022

Copyright 2022 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.