Getting Started with Terraform and Google Cloud Platform

Kengo Wada
Mar 3, 2019 · 4 min read

What is terraform?

Terraform is an automation tool used to ease the process of setting up infrastructure on which we shall later deploy some app. It is infrastructure as code and this simply means that all you have to do is write a simple file, give it the correct credentials to your preferred IaaS provider(in my case GCP) and it will do the rest. Terraform also works well with a variety of tools and cloud service providers.

Getting Started.

The first thing we shall do is download terraform for your OS. You can follow the installation instructions for your OS. You can confirm your installation by opening your terminal or command prompt and typing terraform.

This should be the output after running the terraform command.

The next thing we shall do is get the necessary credentials from GCP. Go to the GCP console menu and select APIs & Services then Credentials. Now click the Create Credentials button and choose Service Account Key. On the next screen choose Compute Engine default service account and JSON then click Create. This will download a .json file to your computer, move this file to the folder where we shall write our terraform code. This file contains sensitive information so do not store it in a public repository or make it public.

Our First Terraform File

Inside the folder, you have created for the project, create a file called main.tf and open it with the text editor of your choice. Inside this file add the following code:

provider "google" {
credentials = "${file("authfile.json")}"
project = "<project Id>"
region = "us-east-1"
}

authfile.json is the file we downloaded from GCP.
The project Id can be obtained from the GCP console
Feel free to choose any other region

After this open your terminal or command prompt, go to the appropriate directory and run the terraform init command

The terraform init command performs several different initialization steps in order to prepare a working directory for use. For more details check the documentation.

You should see something like this after terraform init

Creating a Network with Terraform

Now lets create a network.tf file that will contain the code that creates a network. Inside the file add the following code

resource "google_compute_network" "terraform-network" {
name = "terraform-network"
auto_create_subnetworks = "true"
}

Now run the terraform plan command

The terraform plan command is used to create an execution plan. For more details check the documentation.

Now let’s run the terraform apply command followed by yes when prompted to.

By default, terraform apply scans the current directory for the configuration and applies the changes appropriately. For more details check the documentation.

This is the output after successfully creating the network.

Now if you go to the GCP menu and select VPC network then VPC networks you should see the network that was created.

You can see the terraform-network terraform just created.

We have now created a simple network with Terraform on GCP. You can do a lot more with terraform such as setting up compute instances, firewall rules and much more. Look at this repo to get started with setting up compute engine instances and firewall rules.

We can now take down all the resources we just created using a single command terraform destroy.

Infrastructure managed by Terraform will be destroyed. This will ask for confirmation before destroying. For more details check the documentation.

After successfully destroying all the resources managed by Terraform.
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade