Debugging terraform using Jetbrains GoLand

Gandharva Shankara Murthy
5 min readJan 3, 2022

--

One of the most important features in Terraform since Terraform Plugin SDK versions 2.0.0 and above are the debugging capabilities. I will explain in this post how one can enable and debug terraform providers using Intellij Jetbrains GoLand.

The first step at enabling debugging is to start the terraform plugin in debug mode.

Start your provider in debug mode

In the above code, We have added logic to start our terraform provider using plugin.Debug()mode instead of the plugin.Serve()mode.

We are using the command line flag to take an additional argument debugto run our provider in debug mode. This helps us in switching between debug and servemode as and when required.

So let us try running the provider,

Running your provider in plugin.Serve() mode will result to exit status 1

Wait, why are we seeing exit status 1? Isn't our provider supposed to run in debug mode? Ouch! I missed providing the command-line flag, let me re-run the provider with a command-line flag — debug=truethis time.

My provider is running in debug mode now!!!

Great! our provider is running in debug mode now. But how do we link this to my Intelliji GoLand so we can start debugging it using our GoLand IDE. Its extremly simple, all we need to do now is to follow a three step procedure,

  1. Build our provider repo and place it on the right path
  2. Run built binary using delve debugger
  3. Create a new Run configuration in IntelliJ GoLand

Yes, it is as simple as that and it is a one-time configuration.

Build our provider

Terraform expects provider binary to be available at a specific path. Which again differs for different OS flavors.

  • Linux-based system ~/.terraform.d/plugins/${host_name}/${namespace}/${type}/${version}/${target}
  • Windows-based system %APPDATA%\terraform.d\plugins\${host_name}/${namespace}/${type}/${version}/${target}

In our example, we are using a Windows-based system hence we are expecting our provider to be available at

%APPDATA%\terraform.d\plugins\terraform.example.com\vmaas\hpegl\1.1.1

We now navigate to Run > Edit Configurations > click on + > select Go Build and create a new run configuration in GoLand to build our code and place it in the right system path,

In the above configuration, Filesrepresent main.goof our provider and Output directoryrepresent the system path where terraform expects our go-built binary to present. Also, observe that we have unchecked the box Run after build

Run terraform binary using delve debugger

Now we need to configure remote debugging using delve, the steps are similar to the previous run configuration that we created but we will be choosing a different option in the menu under the + button this time.

Delve debugger configuration using Go Remote

If we carefully read the content under the configuration, the delve debugger expects us to do a two-step prerequisite before running it. The first step is to create a Go binary of our provider. The second step is to run our binary using delve.

The command for it is as same as in the image above but only the demo.exewill be replaced with terraform_vmaas_build.exe

Now to simplify the above process, let us create a new build configuration of type shell

run terraform provider with delve and command-line flag — debug=true

Here we have created a new run configuration of type Shell Scriptand named it dlv-terraform. Also, we are passing the command line --debug=trueflag to the Script textfield. Do not worry much, this is the same command which we got during the creation of delve run configuration.

Now to pipeline our flow as, building binary and running binary using delve. To do that we will modify our dlv-terraform config file to run go-build config first. We can achieve this by using the option Before Launch in build configuration

Add go build configuration to Before launch configuration of Shell Script configuration

Make sure to check the box Execute in the terminalor else the run might fail. Now run the dlv-terraform configuration.

Run the Shell script configuration

This will build your latest code and run the binary in a new terminal

Delve debugger waiting for the remote debugger to connect

Now we need to run our Go Remote configuration to start debugging. Make sure to add breakpoints in the code wherever necessary.

Run Go Remote configuration to start listening to our remote binary

Once the Go Remote is started, the terminal running the provider binary will display a mandatory variable to set in our command line to use the debugger.

Attaching to debugger environment variables

Open a new command line terminal to start using terraform in debugging mode,

Attach the debugger variable and start running terraform commands

If you have added breakpoints in your IDE, your debugger should pause execution and take you to your breakpoints as mine doing here,

That's all!! Happy debugging

References

--

--

Gandharva Shankara Murthy

Senior Cloud Engineer | Distributed systems fanatic | Clean Code Practitioner