- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud

Using Terraform to Launch a Compute Engine Instance in GCP
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.

Lab Info
Table of Contents
-
Challenge
Create a Service Account
- From Google Cloud console's main navigation, choose IAM & Admin > Service Accounts.
- Click Create service account.
- Give your service account a name.
- Click Create.
- In the roles dropdown, select Project > Owner.
- Click Continue and then Done.
-
Challenge
Log in to the Host Instance and Ensure Terraform Is Installed
-
From Google Cloud navigation, choose Compute Engine > VM instances.
-
Click SSH next to
terraform-instance
. -
Use
root
privileges:sudo -i
-
Change into the
root
directory:cd /
-
Call Terraform:
terraform
-
-
Challenge
Create a Service Account Key within the Instance
-
Allow the SDK to communicate with GCP:
gcloud init --console-only
-
Enter
Y
andY
at the prompt. -
Choose your Cloud Project.
-
Select the Cloud Student account.
-
Choose Y to configure a default Compute Region and Zone
-
Choose "us-central1-c"
-
Copy the code provided.
-
Paste the code into the terminal.
-
Create the service account key:
gcloud iam service-accounts keys create /downloads/compute-instance.json --iam-account <SERVICE_ACCOUNT_EMAIL>
-
-
Challenge
Create and Deploy the Configuration File
-
Create a
main.tf
file:vim main.tf
-
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.20.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-stream-9" } } network_interface { network = google_compute_network.vpc_network.name access_config { } } }
-
Save and exit the file by pressing Escape followed by
:wq
. -
Finish up by running
terraform init
,terraform validate
,terraform plan
, andterraform apply
.
-
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.