
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 co-workers, treated as code, edited, reviewed, and versioned.
Objectives
In this lab, we will learn how to perform the following tasks:
- Get started with Terraform in Google Cloud.
- Install Terraform from installation binaries.
- Create a VM instance infrastructure using Terraform.
What is Terraform?
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing, popular service providers and custom in-house solutions.
Configuration files describe to Terraform the components needed to run a single application or your entire data center. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform can determine what changed and create incremental execution plans that can be applied.
The infrastructure Terraform can manage includes both low-level components such as compute instances, storage, and networking, and high-level components such as DNS entries and SaaS features.
Key features
Infrastructure as code
Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your data center to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.
Execution plans
Terraform has a planning step in which it generates an execution plan. The execution plan shows what Terraform will do when you execute the apply
command. This lets you avoid any surprises when Terraform manipulates infrastructure.
Resource graph
Terraform builds a graph of all your resources and parallelizes the creation and modification of any non-dependent resources. Because of this, Terraform builds infrastructure as efficiently as possible, and operators get insight into dependencies in their infrastructure.
Change automation
Complex changesets can be applied to your infrastructure with minimal human interaction. With the previously mentioned execution plan and resource graph, you know exactly what Terraform will change and in what order, which helps you avoid many possible human errors.
Verifying Terraform installation
Terraform comes pre-installed in Cloud Shell.
- Open a new Cloud Shell tab, and verify that Terraform is available:

The resulting help output should be similar to this:

Update Terraform
The example code you will use in this lab requires Terraform versions 0.13.0 and higher.
- First, ensure you are using a sufficient version of Terraform by downloading 0.13.0.
wget https://releases.hashicorp.com/terraform/0.13.0/terraform_0.13.0_linux_amd64.zip

2. Unzip the downloaded executable:

3. Move the executable into your local bin folder:

4. Run this command to ensure your Terraform version is 0.13.0:

[Ignore the Out of Date warning ]
Configuration
The set of files used to describe infrastructure in Terraform is simply known as a Terraform configuration
. In this section, you will write your first configuration to launch a single VM instance. We recommend using JSON for creating configuration files.
- In Cloud Shell, create an empty configuration file named
instance.tf
with the following command:
touch instance.tf
- Click Open Editor on the Cloud Shell toolbar. To switch between Cloud Shell and the code editor, click Open Editor or Open Terminal as required, or click Open in new window (image) to leave the Editor open in a separate tab.
- Click Open Editor on the Cloud Shell toolbar. To switch between Cloud Shell and the code editor, click Open Editor or Open Terminal as required, or click Open in new window (image) to leave the Editor open in a separate tab.
3. Click the instance.tf
file and add the following content in it, replacing <PROJECT_ID>
with your Google Cloud project ID:
resource "google_compute_instance" "terraform" {
project = "<PROJECT_ID>"
name = "terraform"
machine_type = "n1-standard-1"
zone = "us-central1-a" boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
} network_interface {
network = "default"
access_config {
}
}
}

This is a complete configuration that Terraform is ready to apply. The general structure should be intuitive and straightforward.
The “resource” block in the instance.tf
file defines a resource that exists within the infrastructure. A resource might be a physical component such as an VM instance.
The resource block has two strings before opening the block: the resource type and the resource name. For this lab, the resource type is google_compute_instance
and the name is terraform.
The prefix of the type maps to the provider: google_compute_instance
automatically tells Terraform that it is managed by the Google
provider.
Within the resource block itself is the configuration needed for the resource.
4. In Cloud Shell, verify that your new file has been added and that there are no other *.tf
files in your directory, because Terraform loads all of them.

Initialization
The first command to run for a new configuration — or after checking out an existing configuration from version control — is terraform init
. This will initialize various local settings and data that will be used by subsequent commands.
Terraform uses a plugin-based architecture to support the numerous infrastructure and service providers available. Each “provider” is its own encapsulated binary that is distributed separately from Terraform itself.
The terraform init
command will automatically download and install any provider binary for the providers to use within the configuration, which in this case is just the Google provider.
- Download and install the provider binary:
terraform init
The Google provider plugin is downloaded and installed in a subdirectory of the current working directory, along with various other bookkeeping files. You will see an “Initializing provider plugins” message. Terraform knows that you’re running from a Google project, and it is getting Google resources.
Downloading plugin for provider "google" (2.12.0)...

The output specifies which version of the plugin is being installed and suggests that you specify this version in future configuration files to ensure that terraform init
will install a compatible version.
2. Create an execution plan:
terraform plan
Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files. This command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state. For example, you might be run this command before committing a change to version control, to create confidence that it will behave as expected.

Note The optional -out
argument can be used to save the generated plan to a file for later execution with terraform apply
.
Apply changes
- In the same directory as the
instance.tf
file you created, run this command:
terraform apply
This output shows the Execution Plan, which describes the actions Terraform will take in order to change real infrastructure to match the configuration. The output format is similar to the diff format generated by tools like Git.
There is a +
next to google_compute_instance.terraform
, which means that Terraform will create this resource. Following that are the attributes that will be set. When the value displayed is <computed>
, it means that the value won’t be known until the resource is created.
If the plan was created successfully, Terraform will now pause and wait for approval before proceeding.
In a production environment, if anything in the Execution Plan seems incorrect or dangerous, it’s safe to cancel here. No changes have been made to your infrastructure.

2. For this case the plan looks acceptable, so type yes
at the confirmation prompt to proceed. Executing the plan will take a few minutes because Terraform waits for the VM instance to become available.

After this, Terraform is all done!
Test that the VM is created
- In the Google Cloud Console, on the Navigation menu, click Compute Engine > VM instances to see the created VM instance.

2. Alternately, you may execute below command in cloud shell:

That concludes our Lab!!