[2025] Create a Virtual Machine - GSP001

[2025] Create a Virtual Machine - GSP001

Overview

Compute Engine allows you to create virtual machines (VMs) that run different operating systems, including multiple flavors of Linux (Debian, Ubuntu, Suse, Red Hat, CoreOS) and Windows Server, on Google infrastructure. You can run thousands of virtual CPUs on a system that is designed to be fast and to offer strong consistency of performance.

In this hands-on lab, you create VM instances of various machine types using the Google Cloud console and the gcloud command line in Cloud Shell. You also learn how to connect an NGINX web server to your VM.

Although you can easily copy and paste commands from the lab to the appropriate place, it is recommended that you type the commands yourself to reinforce your understanding of the core concepts.

Objectives

In this lab, you learn how to perform the following tasks:

  • Create a VM with the Cloud console.

  • Create a VM with the gcloud command line.

  • Deploy a web server and connect it to a VM.

Prerequisites

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

Setup and requirements

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 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:

  • Access to a standard internet browser (Chrome browser recommended).

Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.

  • Time to complete the lab—remember, once you start, you cannot pause a lab.

Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

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 dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:

    • The Open Google Cloud console button

    • Time remaining

    • The temporary credentials that you must use for this lab

    • Other information, if needed, to step through this lab

  2. 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.

    Note: If you see the Choose an account dialog, click Use Another Account.

  3. If necessary, copy the Username below and paste it into the Sign in dialog.

     student-04-1ae68ae49041@qwiklabs.net
    

    You can also find the Username in the Lab Details pane.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

     ELjf9SbiFmop
    

    You can also find the Password in the Lab Details pane.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.

    Note: Using your own Google Cloud account for this lab may incur extra charges.

  7. 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 Google Cloud console opens in this tab.

Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.

Navigation menu icon and Search field

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.

  1. Click Activate Cloud Shell at the top of the Google Cloud console.

  2. Click through the following windows:

    • Continue through the Cloud Shell information window.

    • Authorize Cloud Shell to use your credentials to make Google Cloud API calls.

When you are connected, you are already authenticated, and the project is set to your Project_ID, qwiklabs-gcp-04-458fb2da5cb2. The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to qwiklabs-gcp-04-458fb2da5cb2

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

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: *
ACCOUNT: student-04-1ae68ae49041@qwiklabs.net

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core]
project = qwiklabs-gcp-04-458fb2da5cb2

Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Understanding Regions and Zones

Certain 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.

RegionsZones
Western USus-west1-a, us-west1-b
Central USus-central1-a, us-central1-b, us-central1-d, us-central1-f
Eastern USus-east1-b, us-east1-c, us-east1-d
Western Europeeurope-west1-b, europe-west1-c, europe-west1-d
Eastern Asiaasia-east1-a, asia-east1-b, asia-east1-c

Resources that live in a zone are referred to as zonal resources. Virtual machine Instances and persistent disks live in a zone. 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.

Learn more about regions and zones and see a complete list in the Compute Engine page, Regions and zones documentation).

Set the region and zone

  1. Run the following command to set the project region for this lab:
gcloud config set compute/region us-central1
  1. To create a variable for region, run the following command:
export REGION=us-central1
  1. Run the following command to create a variable for the zone:
export ZONE=us-central1-c

Learn more from the Regions and zones documentation.

Note: When you run gcloud on your own machine, the config settings are persisted across sessions. But in Cloud Shell, you need to set this for every new session or reconnection.

Task 1. Create a new instance from the Cloud console

In this section, you create new predefined machine types with Compute Engine from the Cloud console.

  1. In the Cloud console, on the Navigation menu (☰), click Compute Engine > VM Instances.

    This may take a minute to initialize for the first time.

  2. To create a new instance, click Create Instance.

  3. In the Machine configuration:

    Enter the values for the following fields:

    | Field | Value | Additional Information | | --- | --- | --- | | Name | gcelab | Name for the VM instance | | Region | us-central1 | For more information about regions, see the Compute Engine guide, Regions and Zones. | | Zone | us-central1-c | Note: Remember the zone that you selected to use later. For more information about zones, see the Compute Engine guide, Regions and Zones. | | Series | E2 | | | Machine Type | e2-medium | This is an e2-medium, 2-CPU, 4GB RAM instance. Several machine types are available, ranging from micro instance types to 32-core/208GB RAM instance types. For more information, see the Compute Engine guide, About machine families. |

Note: A new project has a default resource quota , which may limit the number of CPU cores. You can request more when you work on projects outside this lab.

  1. Click OS and storage.

    Click Change to begin configuring your boot disk and select the values for:

    • Operating system: Debian

    • Version: Debian GNU/Linux 11 (bullseye)

    • Boot disk type: Balanced persistent disk

    • Size: 10 GB

Several images are available, including Debian, Ubuntu, CoreOS, and premium images such as Red Hat Enterprise Linux and Windows Server. For more information, see the Operating System documentation.

  1. Click Networking.

    • Firewall: Allow HTTP traffic

Select this option in order to access a web server that you install later.

Note: This automatically creates a firewall rule to allow HTTP traffic on port 80.

  1. Once all sections are configured, scroll down and click Create to launch your new virtual machine instance.

    It should take about a minute for the VM, gcelab, to be created. After gcelab is created, the VM Instances page lists it in the VM instances list.

  2. To use SSH to connect to the VM, click SSH to the right of the instance name, gcelab.

    This launches an SSH client directly from your browser.

Note: Learn more about how to use SSH to connect to an instance from the Compute Engine guide, Connect to Linux VMs using Google tools .

Task 2. Install an NGINX web server

Now you install an NGINX web server, one of the most popular web servers in the world, to connect your VM to something.

  1. Run the following command to update the OS:

     sudo apt-get update
    

    Expected output:

     Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B]
     Get:5 file:/etc/apt/mirrors/debian-security.list Mirrorlist [39 B]
     Get:7 https://packages.cloud.google.com/apt google-compute-engine-bookworm-stable InRelease [1321 B]
     Get:2 https://deb.debian.org/debian bookworm InRelease [151 kB]                         
     Get:3 https://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
     Get:4 https://deb.debian.org/debian bookworm-backports InRelease [59.0 kB]
     Hit:8 https://packages.cloud.google.com/apt cloud-sdk-bookworm InRelease
     Hit:6 https://deb.debian.org/debian-security bookworm-security InRelease
     Fetched 267 kB in 1s (274 kB/s)
     Reading package lists... Done
    
  2. Run the following command to install NGINX:

     sudo apt-get install -y nginx
    

    Expected output:

      Reading package lists... Done
      Building dependency tree
      Reading state information... Done
      The following NEW packages will be installed:
      ...
    
  3. Run the following command to confirm that NGINX is running:

     ps auwx | grep nginx
    

    Expected output:

      root      2330  0.0  0.0 159532  1628 ?        Ss   14:06   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
      www-data  2331  0.0  0.0 159864  3204 ?        S    14:06   0:00 nginx: worker process
      www-data  2332  0.0  0.0 159864  3204 ?        S    14:06   0:00 nginx: worker process
      root      2342  0.0  0.0  12780   988 pts/0    S+   14:07   0:00 grep nginx
    
  4. To see the web page, return to the Cloud console and click the External IP link in the row for your machine, or add the External IP value to http://EXTERNAL_IP/ in a new browser window or tab.

    A default web page should open that says: Welcome to nginx!

    To check your progress in this lab, click Check my progress below. A checkmark means you're successful.

    Create a Compute Engine instance and add an NGINX Server to your instance with necessary firewall rules.

    Check my progress

Task 3. Create a new instance with gcloud

Instead of using the Cloud console to create a VM instance, use the command line tool gcloud, which is pre-installed in Google Cloud Shell. Cloud Shell is an interactive shell environment for Google Cloud loaded with all the development tools you need (gcloud, git, and others) and offers a persistent 5-GB home directory.

Note: If you want to try this on your own machine, read the gcloud command line tool guide.

  1. In Cloud Shell, run the following gcloud command to create a new VM instance from the command line:

     gcloud compute instances create gcelab2 --machine-type e2-medium --zone=$ZONE
    

    Expected output:

          Created [...gcelab2].
          NAME: gcelab2
          ZONE: us-central1-c
          MACHINE_TYPE: e2-medium
          PREEMPTIBLE:
          INTERNAL_IP: 10.128.0.3
          EXTERNAL_IP: 34.136.51.150
          STATUS: RUNNING
    

    To check your progress in this lab, click Check my progress below. A checkmark means you're successful.

    Create a new instance with gcloud.

    Check my progress

    The new instance has these default values:

    • The Debian 11 (bullseye) image.

    • The e2-medium machine type.

    • A root persistent disk with the same name as the instance; the disk is automatically attached to the instance.

When working in your own project, you can specify a custom machine type.

  1. To see all the defaults, run the following command:

     gcloud compute instances create --help
    

    Note: You can set the default region and zones that gcloud uses if you are always working within one region/zone and you don't want to append the --zone flag every time.

    To do this, run these commands:

    gcloud config set compute/zone ...

    gcloud config set compute/region ...

  2. To exit help, press CTRL+C.

  3. In the Cloud console, on the Navigation menu (), click Compute Engine > VM instances. Or if you still had the VM instances page open, just click Refresh. Your two new instances should be listed.

  4. You can also use SSH to connect to your instance via gcloud. Make sure to add your zone, or omit the --zone flag if you've set the option globally:

     gcloud compute ssh gcelab2 --zone=us-central1-c
    
  5. Enter Y to continue.

        Do you want to continue? (Y/n)
    
  6. Press Enter through the passphrase section to leave the passphrase empty.

        Generating public/private rsa key pair.
        Enter passphrase (empty for no passphrase)
    
  7. After connecting, disconnect from SSH by exiting from the remote shell with the command that follows:

     exit
    

Task 4. Test your knowledge

Test your knowledge about Google Cloud by taking the quiz. (Please select multiple correct options if necessary.)

Through which of the following ways can you create a VM instance in Compute Engine?

  • The Cloud console

  • The gcloud command line tool


Solution of Lab

curl -LO raw.githubusercontent.com/QUICK-GCP-LAB/2-Minutes-Labs-Solutions/main/Creating%20a%20Virtual%20Machine/gsp001.sh
sudo chmod +x gsp001.sh
./gsp001.sh