Using Terraform to Launch a Compute Engine Instance in GCP

1.25 hours
  • 4 Learning Objectives

About this Hands-on Lab

Learning how to create major resources with Terraform is important in understanding how Terraform works with GCP. In this hands-on lab, we will walk through what creating a Compute Engine instance looks like and how to configure it.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Service Account
  1. From Google Cloud console’s main navigation, choose IAM & Admin > Service Accounts.
  2. Click Create service account.
  3. Give your service account a name.
  4. Click Create.
  5. In the roles dropdown, select Project > Owner.
  6. Click Continue and then Done.
Log in to the Host Instance and Ensure Terraform Is Installed
  1. From Google Cloud navigation, choose Compute Engine > VM instances.

  2. Click SSH next to terraform-instance.

  3. Use root privileges:

    sudo -i
  4. Change into the root directory:

    cd /
  5. Input the path to communicate with Terraform into the /etc/profile file:

    echo "PATH='$PATH:/downloads/'" >> /etc/profile
  6. Run the following in order to be able to call Terraform:

    source /etc/profile
  7. Call Terraform:

    terraform
Create a Service Account Key within the Instance
  1. Allow the SDK to communicate with GCP:

    gcloud auth login
  2. Enter Y at the prompt.

  3. Click on the link in the output.

  4. Select the Cloud Student account.

  5. Click Allow.

  6. Copy the code provided.

  7. Paste the code into the terminal.

  8. Create the service account key:

    gcloud iam service-accounts keys create /downloads/compute-instance.json --iam-account <SERVICE_ACCOUNT_EMAIL>
Create and Deploy the Configuration File
  1. Create a main.tf file:

    vim main.tf
  2. Paste the following configuration, replacing <PROJECT_NAME> with your project name (found in the top navigation bar of the Google Cloud console):

    provider "google" {
      version = "3.5.0"
      credentials = file("/downloads/compute-instance.json")
      project = "<PROJECT_NAME>"
      region  = "us-central1"
      zone    = "us-central1-c"
    }
    
    resource "google_compute_network" "vpc_network" {
      name = "terraform-network"
    }
    resource "google_compute_instance" "vm_instance" {
      name         = "terraform-instance2"
      machine_type = "f1-micro"
      zone         = "us-central1-c"
      boot_disk {
        initialize_params {
          image = "centos-cloud/centos-7"
        }
      }
    
      network_interface {
        network = google_compute_network.vpc_network.name
        access_config {
        }
      }
    }
  3. Save and exit the file by pressing Escape followed by :wq.

  4. Finish up by running terraform init, terraform validate, terraform plan, and terraform apply.

Additional Resources

Your team wants to test a web-facing application on an instance and has tasked you with setting up a Compute Engine instance.

To complete this assignment, you must:

  1. Create a service account to use with the host instance.
  2. Log in to the host via SSH and create a key to be used with the service account.
  3. Ensure that Terraform is installed.
  4. Create a main.tf file to configure the instance.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?