Create your first Compute Engine(VM) in GCP using Terraform

Pulkit Agrawal
Hacker Soon
Published in
3 min readApr 29, 2019

Infrastructure provisioning using Terraform.

This is a very basic tutorial to start with infrastructure provisioning using terraform in Google Cloud Platform.

Terraform enables you to safely and predictably create, change, and improve infrastructure.

Before starting with this tutorial please make sure that Terraform and google-SDK are installed in your system.

Run these commands in your terminal:

> terraform -v> gcloud -v

Make sure both commands are working in your system. If not please install terraform and gcloud.

In this tutorial, I am going to explain to you how to create a very basic google compute engine VM with a minimal configuration in GCP using terraform.

First, let’s take a look at the folder structure of useful terraform files.

folder structure: basic terraform file

main.tf, provider.tf, variable.tf, terraform.tfvars and start.sh files contain code related to terraform. Others are the auto-generated file after terraform initialization.

provider.tf: In this file, I mentioned about the provider we gonna use. A provider can be GCP, AWS, or Azure etc..

We are considering GCP in this tutorial. we have to pass credentials to apply the terraform and also specified the project and region where we will create compute engine.

main.tf: In this file, I mentioned about the resource we are going to create in GCP. In this tutorial, we are considering resource as a compute engine. Also, we have to specify each and every specification of a resource like machine type, network, disk, instance name etc…

variables.tf: In this file, I defined all the variable which we are using in the provider.tf and main.tf files. here we can describe variable, define the type of the variable and also we can give the default value of variables.

terraform.tfvars: In this file, We can define the value of all variables created in variables.tf file. This file is useful when we have to create many resources with different specifications.

start.sh: In this file, write shell commands which you want to run inside compute engine after creation. This file is not mandatory.

I am defining some command for installing mongo inside newly created compute engine.

After the creation of all these files has to run a set of terraform commands for resource creation.

  1. Terraform init: This command initializes the terraform inside a folder and creates .terraform directory.
> terraform init

2. Terraform plan: This command is used to create an execution plan.

> terraform plan

3. Terraform apply: This command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan.

> terraform apply

After the successful execution of these commands, we will see terraform.tfstate and terraform.tfstate.backup files in your folder. These files save the state of resources which will help update or destroy infrastructure in future. Don’t delete these files and keep it safe.

4. Terraform destroy: This command is used to destroy the Terraform-managed infrastructure.

> terraform destroy

This is the very basic example to setup terraform for GCP compute engine.

Use this Github link for the codebase.

--

--