Debugging terraform using Jetbrains GoLand
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.
In the above code, We have added logic to start our terraform provider using
plugin.Debug()mode instead of the
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
servemode as and when required.
So let us try running the provider,
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.
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,
- Build our provider repo and place it on the right path
- Run built binary using delve debugger
- 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
- Windows-based system
In our example, we are using a Windows-based system hence we are expecting our provider to be available at
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,
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.
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
Now to simplify the above process, let us create a new build configuration of type
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
Make sure to check the box
Execute in the terminalor else the run might fail. Now run the dlv-terraform configuration.
This will build your latest code and run the binary in a new terminal
Now we need to run our Go Remote configuration to start debugging. Make sure to add breakpoints in the code wherever necessary.
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.
Open a new command line terminal to start using terraform in debugging mode,
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
Plugin Development - Debugging Providers | Terraform by HashiCorp
Search Terraform documentation This guide documents a few different ways to access more information about the runtime…