Using Terraform to Create a New VPC and Public Subnet in GCP

1.25 hours
  • 4 Learning Objectives

About this Hands-on Lab

Creating a virtual private network and subnetworks is the foundation of using resources or any infrastructure within GCP. In this hands-on lab, we will learn how to use Terraform to create a VPC and public subnet.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Service Account

NOTE: Once you have the lab up and running, give it an extended time for all the software to properly download in order to complete the lab. You’ll know it is ready when when the ll step shows both a downloads folder and a Terraform zip file.

  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. Click on the link given, allow the cloud_user email to retrieve the key, and copy and paste the key into your terminal.

  3. Create the service account key:

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

    vim main.tf
  2. Paste the following configuration:

    provider "google" {
      version = "3.5.0"
      credentials = file("/downloads/instance.json")
      project = ""
      region  = "us-central1"
      zone    = "us-central1-c"
    }
    resource "google_compute_network" "vpc_network" {
      name = "terraform-network"
    }
    resource "google_compute_subnetwork" "public-subnetwork" {
      name          = "terraform-subnetwork"
      ip_cidr_range = "10.2.0.0/16"
      region        = "us-central1"
      network       = google_compute_network.vpc_network.name
      }
  3. Save and exit the file by pressing Escape followed by :wq.

  4. Use terraform init , terraform validate , terraform plan, and then terraform apply.

Additional Resources

NOTE: Once you have the lab up and running, give it an extended time for all the software to properly download in order to complete the lab. You'll know it is ready when when the ll step shows both a downloads folder and a Terraform zip file. It can take around 13 minutes or so.

Your company wants to provision a new network with a public subnet so they can test any new instances within that public subnet. They have tasked you with creating this VPC and subnet through Infrastructure as Code so they can test and launch resources as necessary.

You’ll need to accomplish the following steps to complete your task:

  1. Create a service account.
  2. Create a service account key to be used with the host instance.
  3. Ensure Terraform is installed.
  4. Create a main.tf file to create the configuration for the VPC and subnet.
  5. Use terraform apply to execute the plan.

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?