Quick tip: Review the prerequisites before you run the lab
Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses
Automating the Deployment of Networks with Terraform
Get access to over 700 hands-on labs, skill badges, and courses
GSP460
Overview
In this lab, you create a Terraform configuration with a module to automate the deployment of a custom network with resources. Specifically, you deploy 3 networks with firewall rules and VM instances.
Objectives
Create a configuration for a custom-mode network
Create a configuration for a firewall rule
Create a module for VM instances
Create a configuration for an auto-mode network
Create and deploy a configuration
Verify the deployment of a configuration
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
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
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.
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}}
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}}
You can also find the Password in the Lab Details pane.
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.
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.
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.
Click Activate Cloud Shell at the top of the Google Cloud console.
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, . The output contains a line that declares the Project_ID for this session:
Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
(Optional) You can list the active account name with this command:
gcloud auth list
Click Authorize.
Output:
ACTIVE: *
ACCOUNT: {{{user_0.username | "ACCOUNT"}}}
To set the active account, run:
$ gcloud config set account `ACCOUNT`
(Optional) You can list the project ID with this command:
gcloud config list project
Output:
[core]
project = {{{project_0.project_id | "PROJECT_ID"}}}
Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Set up Terraform and Cloud Shell
Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open-source tool that codifies APIs into declarative configuration files that can be shared among team members, treated as code, edited, reviewed, and versioned.
Initialize Terraform
Terraform uses a plugin-based architecture to support the numerous infrastructure and service providers available. Each "Provider" is its own encapsulated binary distributed separately from Terraform itself. Initialize Terraform by setting Google as the provider.
Create a directory for your Terraform configuration by running the following command:
mkdir tfnet
In Cloud Shell, click Open Editor to open Cloud Shell Editor. Click Open in a new window if required.
Expand the tfnet folder in the left pane of the code editor.
To create a new file in the tfnet folder, click File > New File.
Name the new file provider.tf, and then open it.
Copy the code into provider.tf:
provider "google" {}
Initialize Terraform by running the following commands:
cd tfnet
terraform init
The output should look like this:
* provider.google: version = "~> 3.63"
Terraform has been successfully initialized!
You are now ready to work with Terraform in Cloud Shell.
Task 2. Create managementnet and its resources
Create the custom-mode network managementnet along with its firewall rule and VM instance (managementnet-us-vm).
Configure managementnet
Create a new configuration and define managementnet.
To create a new file, click File > New File.
Name the new file managementnet.tf, and then open it.
Copy the following base code into managementnet.tf:
# Create the managementnet network
resource [RESOURCE_TYPE] "managementnet" {
name = [RESOURCE_NAME]
#RESOURCE properties go here
}
This base template is a great starting point for any Google Cloud resource. The name field allows you to name the resource, and the type field allows you to specify the Google Cloud resource that you want to create. You can also define properties, but these are optional for some resources.
In managementnet.tf, replace [RESOURCE_TYPE] with "google_compute_network".
In managementnet.tf, replace [RESOURCE_NAME] with "managementnet".
Add the following property to managementnet.tf:
auto_create_subnetworks = "false"
Unlike an auto-mode network, a custom mode network does not automatically create a subnetwork in each region. Therefore, you are setting auto_create_subnetworks to false.
# Create managementsubnet-us subnetwork
resource "google_compute_subnetwork" "managementsubnet-us" {
name = "managementsubnet-us"
region = "{{{ project_0.default_region | Region 1 }}}"
network = google_compute_network.managementnet.self_link
ip_cidr_range = "10.130.0.0/20"
}
Note: The google_compute_subnetwork resource is a subnet. You are specifying the name, region, VPC network and IP CIDR range for managementsubnet-us. For more information on this specific resource, refer to the Terraform documentation.
To save managementnet.tf, click File > Save.
Configure the firewall rule
Define a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on managementnet.
Add the following base code to managementnet.tf:
# Add a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on managementnet
resource [RESOURCE_TYPE] "managementnet-allow-http-ssh-rdp-icmp" {
name = [RESOURCE_NAME]
source_ranges = [
"0.0.0.0/0"
]
#RESOURCE properties go here
}
In managementnet.tf, replace [RESOURCE_TYPE] with "google_compute_firewall":
Note: The google_compute_firewall resource is a firewall rule. For more information on this specific resource, refer to the Terraform documentation.
In managementnet.tf, replace [RESOURCE_NAME] with "managementnet-allow-http-ssh-rdp-icmp".
Add the following property to managementnet.tf:
network = google_compute_network.managementnet.self_link
Note: Because this firewall rule depends on its network, you are using the google_compute_network.managementnet.self_link reference to instruct Terraform to resolve these resources in a dependent order. In this case, the network is created before the firewall rule.
The list of allow rules specify which protocols and ports are permitted.
Verify that your additions to managementnet.tf look like this:
# Create a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on managementnet
resource "google_compute_firewall" "managementnet_allow_http_ssh_rdp_icmp" {
name = "managementnet-allow-http-ssh-rdp-icmp"
source_ranges = [
"0.0.0.0/0"
]
network = google_compute_network.managementnet.self_link
allow {
protocol = "tcp"
ports = ["22", "80", "3389"]
}
allow {
protocol = "icmp"
}
}
To save managementnet.tf, click File > Save.
Configure the VM instance
Define the VM instance by creating a VM instance module. A module is a reusable configuration inside a folder. You will use this module for all VM instances of this lab.
To create a new folder inside tfnet, select the tfnet folder, and then click File > New Folder.
Name the new folder instance.
To create a new file inside instance, select the instance folder, and then click File > New File.
Name the new file main.tf, and then open it.
You should have the following folder structure in Cloud Shell:
Copy the following base code into main.tf:
resource [RESOURCE_TYPE] "vm_instance" {
name = [RESOURCE_NAME]
#RESOURCE properties go here
}
In main.tf, replace [RESOURCE_TYPE] with "google_compute_instance".
Note: The google_compute_instance resource is a Compute Engine instance. For more information on this specific resource, refer to the Terraform documentation.
In main.tf, replace [RESOURCE_NAME] with var.instance_name.
Because you will be using this module for all VM instances, you are defining the instance name as an input variable. This allows you to control the name of the variable from managementnet.tf. For more information about input variables, refer to the Define Input Variables documentation.
Add the following properties to main.tf:
zone = var.instance_zone
machine_type = var.instance_type
These properties define the zone and machine type of the instance as input variables.
This property defines the boot disk to use the Debian 11 OS image. Because all four VM instances will use the same image, you can hard-code this property in the module.
Add the following properties to main.tf:
network_interface {
subnetwork = var.instance_subnetwork
access_config {
# Allocate a one-to-one NAT IP to the instance
}
}
This property defines the network interface by providing the subnetwork name as an input variable and the access configuration. Leaving the access configuration empty results in an ephemeral external IP address. For more information, see the Terraform documentation.
Define the 4 input variables at the top of main.tf and verify that main.tf looks like this, including brackets {}:
variable "instance_name" {}
variable "instance_zone" {}
variable "instance_type" {
default = "e2-standard-2"
}
variable "instance_subnetwork" {}
resource "google_compute_instance" "vm_instance" {
name = var.instance_name
zone = var.instance_zone
machine_type = var.instance_type
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
subnetwork = var.instance_subnetwork
access_config {
# Allocate a one-to-one NAT IP to the instance
}
}
}
By giving instance_type a default value, the variable is optional. The instance_name, instance_zone, and instance_subnetwork are required, and you will define them in managementnet.tf.
To save main.tf, click File > Save.
Add the following VM instance to managementnet.tf:
This resource is leveraging the module in the instance folder and provides the name, zone, and network as inputs. Because this instance depends on a VPC network, you are using the google_compute_subnetwork.managementsubnet-us.self_link reference to instruct Terraform to resolve these resources in a dependent order. In this case, the subnet is created before the instance.
Note: The benefit of writing a Terraform module is that it can be reused across many configurations. Instead of writing your own module, you can also leverage existing modules from the Terraform Module registry.
To save managementnet.tf, click File > Save.
Create managementnet and its resources
It's time to apply the managementnet configuration.
Rewrite the Terraform configurations files to a canonical format and style by running the following command:
terraform fmt
Note: If you get an error, revisit the previous steps to ensure that your configuration matches the lab instructions. If you cannot troubleshoot the issue of your configuration, look at these finished configurations:
Initialize Terraform by running the following command:
terraform init
The output should look like this:
Initializing the backend...
...
* provider.google: version = "~> 3.63"
Terraform has been successfully initialized!
Note: If you get an error, revisit the previous steps to ensure that you have the correct folder/file structure. If you cannot troubleshoot the issue of your configuration, refer to the finished configurations linked above. When you have corrected the issue, re-run the previous command.
Create an execution plan by running the following command:
terraform plan
The output should look like this:
...
Plan: 4 to add, 0 to change, 0 to destroy.
...
Terraform determined that the following 4 resources need to be added:
Name
Description
managementnet
VPC network
managementsubnet-us
Subnet of managementnet in
managementnet_allow_http_ssh_rdp_icmp
Firewall rule to allow HTTP, SSH, RDP and ICMP
managementnet-us-vm
VM instance in
Apply the desired changes by running the following command:
Notice that when the VPC network is created, the firewall rule and subnet are created. After the subnet is created, the VM instance is created. That's because the firewall rule and subnet relied on the network, and the VM instance relied on the subnet through self_link references.
Note: If you get an error during the execution, revisit the previous steps to ensure that you have the correct folder/file structure. If you cannot troubleshoot the issue of your configuration, refer to the finished configurations linked above. When you have corrected the issue, re-run the previous command.
Verify managementnet and its resources
In the Cloud Console, verify that the resources were created.
In the Cloud Console, select Navigation menu > VPC network > VPC networks.
View the managementnet VPC network with its subnetwork.
In the left pane, click Firewall.
View the managementnet_allow_http_ssh_rdp_icmp firewall rule for the VPC network that was created.
Select Navigation menu > Compute Engine > VM instances.
Note the managementnet-us-vm instance.
Return to Cloud Shell.
Click Check my progress to verify the objective.
Create managementnet and its resources
Task 3. Create privatenet and its resources
Create the custom-mode network privatenet along with its firewall rule and VM instance (privatenet-us-vm).
Configure privatenet
Create a new configuration and define privatenet.
To create a new file in the tfnet folder, click File > New File.
Name the new file privatenet.tf, and then open it.
You should have the following folder structure in Cloud Shell:
Add the VPC network by copying the following code into privatenet.tf:
Add the privatesubnet-us subnet resource to privatenet.tf:
# Create privatesubnet-us subnetwork
resource "google_compute_subnetwork" "privatesubnet-us" {
name = "privatesubnet-us"
region = "{{{project_0.default_region| REGION 1}}}"
network = google_compute_network.privatenet.self_link
ip_cidr_range = "172.16.0.0/24"
}
Add the privatesubnet-second-subnet subnet resource to privatenet.tf:
# Create privatesubnet-second-subnet subnetwork
resource "google_compute_subnetwork" "privatesubnet-second-subnet" {
name = "privatesubnet-second-subnet"
region = "{{{project_0.default_region_2 | REGION 2}}}"
network = google_compute_network.privatenet.self_link
ip_cidr_range = "172.20.0.0/24"
}
To save privatenet.tf, click File > Save.
Configure the firewall rule
Define a firewall rule to allow HTTP, SSH, and RDP traffic on privatenet.
Add the firewall resource to privatenet.tf:
# Create a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on privatenet
resource "google_compute_firewall" "privatenet-allow-http-ssh-rdp-icmp" {
name = "privatenet-allow-http-ssh-rdp-icmp"
source_ranges = [
"0.0.0.0/0"
]
network = google_compute_network.privatenet.self_link
allow {
protocol = "tcp"
ports = ["22", "80", "3389"]
}
allow {
protocol = "icmp"
}
}
Note: Alternatively, you could create a module for the firewall rule because the only difference to the previous firewall rule is the VPC network that it applies to.
To save privatenet.tf, click File > Save.
Configure the VM instance
Use the instance module to configure privatenet-us-vm.
Rewrite the Terraform configurations files to a canonical format and style by running the following command:
terraform fmt
Note: If you get an error, revisit the previous steps to ensure that your configuration matches the lab instructions. If you cannot troubleshoot the issue of your configuration, take a look at these finished configurations:
Initialize Terraform by running the following command:
terraform init
The output should look like this:
Initializing the backend...
...
* provider.google: version = "~> 3.63"
Terraform has been successfully initialized!
Note: If you get an error, revisit the previous steps to ensure that you have the correct folder/file structure. If you cannot troubleshoot the issue of your configuration, refer to the finished configurations linked above. When you have corrected the issue, re-run the previous command.
Create an execution plan by running the following command:
terraform plan
The output should look like this:
...
Plan: 5 to add, 0 to change, 0 to destroy.
...
Terraform determined that the following 5 resources need to be added:
Name
Description
privatenet
VPC network
privatesubnet-us
Subnet of privatenet in
privatesubnet-second-subnet
Subnet of privatenet in
privatenet-allow-http-ssh-rdp-icmp
Firewall rule to allow HTTP, SSH, RDP and ICMP
privatenet-us-vm
VM instance in
Apply the desired changes by running the following command:
terraform apply
Confirm the planned actions by typing:
yes
The output should look like this:
...
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
Note: If you get an error during the execution, revisit the previous steps to ensure that you have the correct folder/file structure. If you cannot troubleshoot the issue of your configuration, refer to the finished configurations linked above. When you have corrected the issue, re-run the previous command.
Verify privatenet and its resources
In the Cloud Console, verify that the resources were created.
In the Cloud Console, select Navigation menu > VPC network > VPC networks.
View the privatenet VPC network with its subnetworks.
In the left pane, click VPC network > Firewall.
View the privatenet_allow_http_ssh_rdp_icmp firewall rule for the VPC network that was created.
Select Navigation menu > Compute Engine > VM instances.
Note the internal IP addresses for privatenet-us-vm.
For managementnet-us-vm, click SSH to launch a terminal and connect.
To test connectivity to privatenet-us-vm's internal IP address, run the following command in the SSH terminal (replacing privatenet-us-vm's internal IP address with the value noted earlier):
ping -c 3 <Enter privatenet-us-vm's internal IP here>
Note: This should not work because both VM instances are on separate VPC networks!
Return to Cloud Shell.
Click Check my progress to verify the objective.
Create privatenet and its resources
Task 4. Create mynetwork and its resources
Create the auto-mode network mynetwork along with its firewall rule and two VM instances (mynet_us_vm and mynet_second_vm).
Configure mynetwork
Create a new configuration and define mynetwork.
To create a new file in the tfnet folder, click File > New File.
Name the new file mynetwork.tf, and then open it.
You should have the following folder structure in Cloud Shell:
Copy the following code into mynetwork.tf:
# Create the mynetwork network
resource "google_compute_network" "mynetwork" {
name = "mynetwork"
#RESOURCE properties go here
}
Add the following property to mynetwork.tf:
auto_create_subnetworks = "true"
By definition, an auto-mode network automatically creates a subnetwork in each region. Therefore, you are setting auto_create_subnetworks to true.
Verify that mynetwork.tf looks like this:
# Create the mynetwork network
resource "google_compute_network" "mynetwork" {
name = "mynetwork"
auto_create_subnetworks = "true"
}
To save mynetwork.tf, click File > Save.
Configure the firewall rule
Define a firewall rule to allow HTTP, SSH, and RDP traffic on mynetwork.
Add the firewall resource to mynetwork.tf:
# Create a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on mynetwork
resource "google_compute_firewall" "mynetwork-allow-http-ssh-rdp-icmp" {
name = "mynetwork-allow-http-ssh-rdp-icmp"
source_ranges = [
"0.0.0.0/0"
]
network = google_compute_network.mynetwork.self_link
allow {
protocol = "tcp"
ports = ["22", "80", "3389"]
}
allow {
protocol = "icmp"
}
}
To save mynetwork.tf, click File > Save.
Configure the VM instance
Use the instance module to configure mynetwork-us-vm and mynetwork-second-vm.
Rewrite the Terraform configurations files to a canonical format and style by running the following command:
terraform fmt
Note: If you get an error, revisit the previous steps to ensure that your configuration matches the lab instructions. If you cannot troubleshoot the issue of your configuration, take a look at these finished configurations:
Initialize Terraform by running the following command:
terraform init
The output should look like this:
Initializing the backend...
...
* provider.google: version = "~> 3.63"
Terraform has been successfully initialized!
Note: If you get an error, revisit the previous steps to ensure that you have the correct folder/file structure. If you cannot troubleshoot the issue of your configuration, refer to the finished configurations linked above. When you have corrected the issue, re-run the previous command.
Create an execution plan by running the following command:
terraform plan
The output should look like this:
...
Plan: 4 to add, 0 to change, 0 to destroy.
...
Terraform determined that the following 4 resources need to be added:
Name
Description
mynetwork
VPC network
mynetwork-allow-http-ssh-rdp-icmp
Firewall rule to allow HTTP, SSH, RDP and ICMP
mynet-us-vm
VM instance in
mynet-second-vm
VM instance in
Apply the desired changes by running the following command:
terraform apply
Confirm the planned actions by typing:
yes
The output should look like this:
...
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
Note: If you get an error during the execution, revisit the previous steps to ensure that you have the correct folder/file structure. If you cannot troubleshoot the issue of your configuration, refer to the finished configurations linked above. When you have corrected the issue, re-run the previous command.
Verify mynetwork and its resources
In the Cloud Console, verify that the resources were created.
In the Cloud Console, select Navigation menu > VPC network > VPC networks.
View the mynetwork VPC network with its subnetworks.
In the left pane, click Firewall.
View the mynetwork-allow-http-ssh-rdp-icmp firewall rule for the VPC network that was created.
Select Navigation menu > Compute Engine > VM instances.
View the mynet-us-vm and mynet-second-vm instances.
Note the internal IP addresses for mynet-second-vm.
For mynet-us-vm, click SSH to launch a terminal and connect.
To test connectivity to mynet-second-vm's internal IP address, run the following command in the SSH terminal (replacing mynet-second-vm's internal IP address with the value noted earlier):
ping -c 3 <Enter mynet-second-vm's internal IP here>
Note: This should work because both VM instances are on the same network, and ICMP traffic is allowed!
Click Check my progress to verify the objective.
Create mynetwork and its resources
Congratulations!
This concludes this self-paced lab, Automate the Deployment of Networks Using Terraform.
In this lab, you created Terraform configurations and modules to automate the deployment of a custom network. As the configuration changes, Terraform can determine what changed and create incremental execution plans, which allows you to build your overall configuration step-by-step.
The instance module allowed you to re-use the same resource configuration for multiple resources while providing properties as input variables. You can leverage the configurations and modules that you created as a starting point for future deployments.
Google Cloud training and 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 January 19, 2024
Lab Last Tested January 19, 2024
Copyright 2025 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.
Labs create a Google Cloud project and resources for a fixed time
Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
On the top left of your screen, click Start lab to begin
Use private browsing
Copy the provided Username and Password for the lab
Click Open console in private mode
Sign in to the Console
Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
Accept the terms, and skip the recovery resource page
Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one
Use private browsing to run the lab
Use an Incognito or private browser window to run this lab. This
prevents any conflicts between your personal account and the Student
account, which may cause extra charges incurred to your personal account.
In this lab, you create a Terraform configuration with a module to automate the deployment of a custom network with resources.