Terraform Hands-on: ChatGPT to the RESCUE!!

Janice Sanders
4 min readApr 19, 2023

--

We all know the power of ChatGPT and how it can help us in our day-to-day tasks.

Recently, I started exploring Terraform, a popular infrastructure-as-code (IaC) tool, which has become increasingly popular due to its ability to automate various infrastructure tasks, such as provisioning cloud resources, for instance (no pun intended😊), which is one of the main use cases of Terraform.

As I’m learning the basics of Terraform and its features, I decided to use it to create and manage some virtual machine (vm) instances in my Google Cloud Platform (GCP).

My main.tf file included a code snippet that defined a Google Cloud Platform (GCP) Compute Engine instance resource. In particular, my plan was to create a vm instance named “cloudshell” in the “us-central1-a” zone, using the “e2-small” machine type and the “debian-cloud/debian-11” image for its boot disk; and I also had it set to configure the instance to use the default network.

Below, you can see a snippet of the code I was using which I snagged from the Terraform for Google Cloud Essential Guide by Bernd Nordhausen.

Image from Terraform for Google Cloud Essential Guide by Bernd Nordhausen

Here is a snapshot of my main.tf file when I was initially running it.

As I was going through the process of creating my vm, I kept encountering an issue. This ‘issue’ ended up being very minor, but at the time, my eyes were overlooking it. (The fact that I was working on this at 2 a.m. in the morning might have a little something to do with that.)

Each time I tried to apply my Terraform configuration, I kept getting the following error:

“Project: required field is not set” as shown below.

As far as I could tell, the syntax appeared fine.

I decided I would turn to ChatGPT for possible suggestions, so I shared my main.tf code along with a very vague prompt that said:

“Does this look right?”

For the most part, ChatGPT confirmed that there didn’t seem to be an issue with the syntax:

After a little more probing, ChatGPT quickly pointed out that the project field was missing from my google_compute_instance resource block.

I added the missing field with my GCP project ID and saved the file. (You can view my step-by-step instructions to resolve the issue at the very end of this posting.)

Once I made that simple fix, my code ran with no issues, and my vm instance was successfully created.

I was able to verify the successful build via my GCP console.

With the assistance of ChatGPT, I was able to perform some troubleshooting and fix my Terraform issue.

I am able to continue my hands-on with Terraform and, eventually, I’ll perform some more robust build-projects.

— — — — — — — — — — — — — — — — — —

Step-by-step on how I resolved the “Project: required field is not set” error in order to successfully create my vm instance in GCP using Terraform.

  1. Opened my main.tf file in visual studio code.
  2. Located the google_compute_instance resource block that is causing the error.
  3. Looked for the project field in the google_compute_instance resource block. It was not present, so I added it to the block.
  4. In the project field, I added the project-id of my GCP project where I wanted to create the vm instance. I found my project ID in my GCP Console.
  5. Saved mymain.tf file.
  6. Confirmed that my command prompt was opened in the directory containing my Terraform configuration files.
  7. Ran the terraform init command to initialize the Terraform working directory.
  8. (I skipped this step in my home lab, but best practice is to…) Run the terraform plan command to view the planned changes to my infrastructure.
  9. Ran the terraform apply command to apply the changes and create the vm instance. (This would be after the planned changes are verified and look good.)
  10. Verified that the vm instance was created in my GCP Console once the apply completes successfully.

ChatGPT to the rescue!! Hopefully, this provides some guidance when you come across similar issues when doing hands-on work with Terraform.

Connect with Janice on LinkedIn!
Connect with Teach2Geek on LinkedIn!

--

--

Janice Sanders
Janice Sanders

Written by Janice Sanders

Cloud Engineer | Cloud Security Engineer | DevOps | Multi-Cloud | Coursera Course Developer | Former Writing Teacher

No responses yet