How to Run and Debug the Terraform Scripts in Qwiklabs

How to Run and Debug the Terraform Scripts in Qwiklabs

26 May 2022

Introduction

If we want to build, change, and version infrastructure safely and efficiently then we can use the Terraform tool. It can manage existing and popular service providers and custom solutions. It is the Infrastructure as a code service offered by HashiCorp. The configuration files describe the components needed to run a single application or an entire data center. We use different basic terraform commands like terraform init, plan, apply, show and destroy to run the terraform scripts.

The structure of the lab folder in Qwiklabs is the same as follows:

Lab-Folder/

    └──  Instructions/

        ├──  en.md

        ├──  img/

    └──  qwiklabs.yaml

    └──  terraform/

        ├──  runtime.yaml

        ├──  main.tf

        ├──  providers.tf  

        ├──  variables.tf

        └──  outputs.tf

 

It is recommended that all Terraform configurations contain the following set of files in its top-level directory. More files may be included as needed, but the following is seen as the bare minimum.

  • main.tf
  • providers.tf
  • variables.tf
  • outputs.tf

Run and Debug the Terraform Script

Once we write our terraform configuration files, we have to run and debug the terraform script. We learn the best way to run and debug the qwiklabs script during the labs development phase.

Run the Script of the Qwiklabs Lab

  1. We have to visit the labs edit page. The URL looks like: https://googlestaging.qwiklabs.com/labs/[lab_id]/edit
    • We can also achieve this by clicking on the pencil icon in the upper right corner of the lab page.
  2. Click on the Startup Lab Environment icon present in the upper right corner or we get this option by scrolling down to the bottom of the lab edit page.
  3. It opens with a new status page and the URL looks like: https://googlestaging.qwiklabs.com/labs/[lab_id]/status
  4. Scroll down and we will see the Qwiklabs Startup Script section.
  5. Once the Script Runner Status is IN_USE, click on the Script Runner Link, which will open a new page with the script runner UI.
  6. Click on the Executions option. We will get the list of executions done.
  7. Click on the existing execution, we will see the overview, logs and terraform files option.

Re-Run the Terraform Script

We have two ways to run the terraform script if we are getting errors while developing the script and have run into errors or want to run for any other reason.

  1. Using GitHub
    We have to make the changes locally and add, commit and push the changes. After making the changes, restart the lab and check that the script worked. This is a good option if we want to restart from a blank slate as this will provide us with a brand new project with no prior state.
  2. Re-run using the Debugger
    We also debug a script using a script debugger but this option is good for minor mistakes/typos. We can access the script debugger using the status page. After clicking on the executions option, there are two types of files, one is immutable files and the other is editable files. We can’t change the immutable files and it shows the exact files state at the time of execution run.

The editable files are initially identical to the immutable files, but may be updated. Editable files also will not show up until the script has finished running. We have to update the content of the editable files and save the changes by clicking on the Save button. The changes are not automatically saved. After all the necessary changes, we have to click on the Re-Run button to run the script. It will open a pop up that will allow us to edit any of the custom input, or other values being submitted to the execution. We will likely not need to change any of these values, but may need to change the custom input. Clicking submit will trigger a new execution that will use any edits that we have made. A new execution should pop up in the executions list shortly after clicking submit.

One important thing to note is that any changes made in the debugger UI will not persist locally or in the git repository, so we will need to copy those changes back to our local version of the script and then re-upload via git.

Request a quote