This simple tutorial will show you how to provision a Google Cloud Compute Engine VM easily using Terraform 3 different ways.

Prerequisites:

So with this tutorial you can follow along using Windows, Mac or Linux. I personally used Ubuntu 23.04 running the Cinnamon Desktop environment. Lets start using the first method.

Whenever I am doing any type of IaC project using Terraform CLI on the local machine I always create a Directory for the project and name that directory the same name as the project created in GCP. Make sure you have authenticated to GCP from the command line as well as created and properly stored your Service Account Key. For simplicity and for the sake of this tutorial, you can store the key in the same Directory on your local machine ( I recommend deleting the key after this tutorial from your local machine and from GCP). So from that Directory on the local machine using your IDE of choice or text editor create a file called main.tf and populate it with the contents shown below:


Visual Studio Code

A few things to note that you can change to customize the VM deployment to your liking:

In the resource block you can change the label, name, machine_type and zone to whatever options you like as long as they are valid Terraform and GCP options. The type of VM we will be deploying is an f1-micro with Debian 11 Linux installed. Also note in the provider block with the credentials=file make sure within the “” is the file name of the key including the .json extension.

From the command-line of your machine run the Terraform init command


I like to also as a best practice run the Terraform fmt command to format the code into what Terraform likes as far as code structure. If your code format does not need any adjusting you will simply get another prompt. If it does Terraform will output all of .tf files within the Directory that it properly formatted. Next run the Terraform validate and correct any errors in your code if applicable. Luckly the code above was already in the correct format and didn’t contain any syntax errors. See screenshot below


Continuing with the standard Terraform workflow run the Terraform plan command and take a look at what Terraform plans to create in GCP


Next run Terraform apply and yes to deploy the VM in GCP


Note at this point in the Directory you will now also have a terraform.tfstate file created by Terraform. This file is what Terraform references to keep track of the resources it has created, destroyed, etc in GCP.

From this point you should be able to see the VM created in GCP under the appropriate project by navigating to Compute Engine->VM Instances.


Clicking on the VM and observing the info under the Details tab you can see all of the options “attributes” that were in your main.tf file we created correctly by Terraform.

Now navigating back to your command-line run the Terraform destroy command, yes to delete the resources generated in GCP.

Now lets see how we can provision the same resource using Terraform Cloud with CLI-driven work flow. For this example we can actually work out of the same directory or if you choose, create another directory and copy the main.tf file. Later on there are a few things we will have to add to the main.tf file after we set up everything in Terraform Cloud.

So logging into Terraform the first thing you will want to do is create an Organization, a Project and then a Workspace (for tutorial purposes you can name the workspace the same name as the directory you are working out of on your local machine).


From the Project we will then create a Workspace and choose CLI-driven workflow.


Enter the Workspace name and choose Create workspace.


The next thing we will have to do is authenticate to Terraform Cloud via the command line. So from the Directory that you are working out of from your local machine run the Terraform login command and follow the prompts. The next step is to now modify the main.tf file to include the following in the Terraform block:


Notice in the Terraform block we have added the organization and also the workspace name.

Now we are ready to setup the Environment variables (variable set) in Terraform Cloud so we can authenticate to GCP. This part I found a bit tricky since GCP using a json file as far as the credentials and essentially you have to correctly paste the right contents of the json file into the Environment variables sections of Terraform Cloud. To setup the variable follow the tutoria on this link https://developer.hashicorp.com/terraform/tutorials/cloud-get-started/loud-create-variable-set?in=terraform/cloud-get-started

The link covers setting up the Environment variable for AWS but when you get to the section for adding the variable do this instead for GCP: https://support.hashicorp.com/hc/en-us/articles/4406586874387-How-to-set-up-Google-Cloud-GCP-credentials-in-Terraform-Cloud

Once you have created the variable and added the appropriate block to your main.tf file you are ready to deploy the infrastructure. First we must Following the workflow run the Terraform init, plan and apply. Notice the following screen shots in the Workspace on Terraform Cloud as you run the plan and then the apply commands


You can now log into GCP console and see the resource deployed.


Don’t forget to run the Terraform destroy to clean up any deployments and not incur any monthly cost. Next we will cover deploying and managing infrastructure in Terraform Cloud using CI/CD processes with GitLab.

So for the next deployment type you will need to have a GitLab account. Once you have your GitLab account established and if not already done so setup a Group and then a Project. So from the last deployment you shouldn’t have to worry about setting up the .json file with the GCP credentials since this should be already established. Now we will connect GitLab with Terraform Cloud using Version Control (VC) workflow. So while creaing a Workspace choose VC workflow.


From there choose GitLab as the VC provider.


Once you select GitLab, from the drop-down select GitLab.com and note the information on the screen from Terraform Cloud. Also note the documentation link that goes into further details on how to connect GitLab as a VC provider to Terraform Cloud.


Once you have everything connected and repos created and established we can now follow the CI/CD process between Terraform Cloud and GitLab by using the appropriate git commands to move the Terraform files to the appropriate repo created in GitLab. For this deployment I decided to add a few things in which when we deploy the VM it will also be configured to install Nginx. Listed below are screenshots of the files that were pushed to the repo that contains the updated code.


Now from Terraform Cloud we can essentially deploy resources with the following actions seen below in the screenshot.


The following screenshots of Terraform Cloud are essentially the init, plan and apply commands run via the command line.


Then hit Confirm & Apply.

Based on the files and the output.tf file the deployment should produce the IP address of the VM. Putting that address in your browser you should see that the nginx server is up and running.


Now we are ready to destroy the resources deployed. From the Workspace navigate to Settings.


Then Destruction and Deletion.

Then select Queue and Destroy Plan.

Confirm the Plan and wait for Terraform Cloud to run through the destroy process. Once all resources have been destroyed you should also be able to navigate to the GCP console and see that there are no VMs running.

Thats it! we have now deployed resources using 3 different methods involving Terraform. Any questions, comments, feedback please reach out and happy DevOping! To see the medium format of this tutorial with larger screenshots click here: https://medium.com/@mikedg79/how-to-provision-a-gcp-vm-using-terraform-3differrent-ways-ce3e49f6c32d