
Before you begin
- 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
Create mynetwork and its resources
/ 100
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.
In this lab, you create a Terraform configuration with a module to automate the deployment of Google Cloud infrastructure. Specifically, you deploy one auto mode network with a firewall rule and two VM instances, as shown in this diagram:
In this lab, you learn how to perform the following tasks:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
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.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details panel.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details panel.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
In this task, you configure your Cloud Shell environment to use Terraform.
Terraform is now integrated into Cloud Shell. Verify which version is installed.
The output should look like this:
Note: Don't worry if you get a warning that the version of Terraform is out of date, because the lab instructions will work with Terraform v1.5.7 and later. The available downloads for the latest version of Terraform are on the Terraform website. Terraform is distributed as a binary package for all supported platforms and architectures, and Cloud Shell uses Linux 64-bit.
Note: If you see the message "Unable to load code editor because third-party cookies are disabled", click Open in New Window. The code editor will open in a new tab. Return to the original tab, click Open Terminal and then switch back to the code editor tab. You will periodically need to switch back to the Cloud Shell terminal in this lab.
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.
right-click
on tfinfra folder and then click New File.provider.tf
:To save provider.tf, click File > Save.
To initialize Terraform, run the following command:
You are now ready to work with Terraform in Cloud Shell.
In this task, you create the auto mode network mynetwork along with its firewall rule and two VM instances (mynet_vm_1 and mynet_vm_2).
Create a new configuration, and define mynetwork.
right-click
on tfinfra folder and then click New File.mynetwork.tf
: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.
mynetwork.tf
, replace [RESOURCE_TYPE]
with "google_compute_network"
(with the quotes).Note: The google_compute_network resource is a VPC network. Available resources are in the Google Cloud provider documentation. Learn more about this specific resource in the Terraform documentation.
mynetwork.tf
, replace [RESOURCE_NAME]
with "mynetwork"
(with the quotes).mynetwork.tf
:By definition, an auto mode network automatically creates a subnetwork in each region. Therefore, you are setting auto_create_subnetworks to true.
mynetwork.tf
, click File > Save.Define a firewall rule to allow HTTP, SSH, RDP, and ICMP traffic on mynetwork.
mynetwork.tf
:mynetwork.tf
, replace [RESOURCE_TYPE]
with "google_compute_firewall"
(with the quotes).Note: The google_compute_firewall resource is a firewall rule. Learn more about this specific resource in the Terraform documentation.
mynetwork.tf
, replace [RESOURCE_NAME]
with "mynetwork-allow-http-ssh-rdp-icmp"
(with the quotes).mynetwork.tf
:Note: Because this firewall rule depends on its network, you are using the google_compute_network.mynetwork.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.
mynetwork.tf
:The list of allow rules specifies which protocols and ports are permitted.
mynetwork.tf
file look like this:Define the VM instances by creating a VM instance module. A module is a reusable configuration inside a folder. You will use this module for both VM instances of this lab.
right-click
on instance folder and then click New File.You should have the following folder structure in Cloud Shell:
main.tf
, replace [RESOURCE_TYPE]
with "google_compute_instance"
(with the quotes).Note: The google_compute_instance resource is a Compute Engine instance. Learn more about this specific resource in the Terraform documentation.
main.tf
, replace [RESOURCE_NAME]
with "${var.instance_name}"
(with the quotes).Because you will be using this module for both VM instances, you are defining the instance name as an input variable. This allows you to control the name of the variable from mynetwork.tf. Learn more about input variables in the Terraform: Define Input Variables Guide.
main.tf
:These properties define the zone and machine type of the instance as input variables.
main.tf
:This property defines the boot disk to use the Debian 11 OS image. Because both VM instances will use the same image, you can hard-code this property in the module.
main.tf
:This property defines the network interface by providing the network name as an input variable and the access configuration. Leaving the access configuration empty results in an ephemeral external IP address (required in this lab). To create instances with only an internal IP address, remove the access_config section. For more information, see the Terraform documentation.
main.tf
looks like this, including brackets {}
right-click
on instance folder and then click New File.variables.tf
.By giving instance_type a default value, you make the variable optional. The instance_name, instance_zone, and instance_network are required, and you will define them in mynetwork.tf
.
mynetwork.tf
:These resources are leveraging the module in the instance folder and provide the name, zone, and network as inputs. Because these instances depend on a VPC network, you are using the google_compute_network.mynetwork.self_link reference to instruct Terraform to resolve these resources in a dependent order. In this case, the network 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 mynetwork.tf
, click File > Save.
Verify that mynetwork.tf
looks like this, including brackets {}
It's time to apply the mynetwork configuration.
The output should look like this:
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, download and then look at these finished configurations:
The output should look like this:
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.
The output should look like this:
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-vm-2 | VM instance in |
mynet-vm-2 | VM instance in |
The output should look like this:
Click Check my progress to verify the objective.
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.
In the Google Cloud console, verify that the resources were created.
In this lab, you created a Terraform configuration with a module to automate the deployment of Google Cloud infrastructure. As your configuration changes, Terraform can 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 configuration and module that you created as a starting point for future deployments.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 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.
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