Contents:
As Terraform is written in Go you may use Delve to debug it.
GoLand includes debugging features, and the Go extension for VS Code makes it easy to use Delve when debugging Go codebases in VS Code.
Debugging an automated test is often the most straightforward workflow for debugging a section of the codebase. For example, the Go extension for VS Code](https://code.visualstudio.com/docs/languages/go#_debugging) adds run test | debug test
options above all tests in a *_test.go
file. These allow debugging without any prior configuration.
As described above, debugging tests in VS Code is easily achieved through the Go extension.
If you need more control over how tests are run while debugging, e.g. environment variable values, look at the example debugger launch configuration 'Run selected test'. You can adapt this example to create your own launch configuration file.
When using this launch configuration you must highlight a test's name before starting the debugger:
In this workflow you:
- Build Terraform using compiler flags.
- Start a debug server with a command containing the terraform command you want to debug.
- This command is run in the working directory that contains your Terraform configuration.
- Connect to the debug server to monitor progress through breakpoints.
One way to do it is to compile a binary with the appropriate compiler flags:
go install -gcflags="all=-N -l"
This enables you to then execute the compiled binary via Delve, pass any arguments and spin up a debug server which you can then connect to:
# Update the path to the terraform binary if your install directory is influenced by $GOPATH or $GOBIN
dlv exec $HOME/go/bin/terraform --headless --listen :2345 --log -- apply
You may connect to the headless debug server via Delve CLI
dlv connect :2345
The repository provides an example 'Connect to dlv server' launch configuration, making it possible to use VS Code's native debugging integration via the Go extension for VS Code:
In this workflow you:
- Update the debugger's launch configuration to point at the directory containing your Terraform configuration.
- Start the debugger through VS Code and monitor progress through breakpoints.
Look at the example debugger launch configuration 'Run Terraform in debug mode'. You can adapt this example to create your own launch configuration file.
To use this launch configuration:
- Prepare a local Terraform project.
- Get the absolute path to that directory.
- Update the launch configuration to use that path, either in the
-chdir
argument or as acwd
attribute in the launch configuration. - Make sure the
args
array's element reflect the command you'd like to debug. - Provide any required environment variables through the
env
orenvFile
attributes.
Navigate to the Run and Debug view in the left-side Activity Bar. After selecting the Run Terraform in debug mode
configuration in the Run and Debug view from the left-side, press the green arrow.
This is equivalent to running a Terraform CLI command in the local Terraform project's directory. For example, if you run and debug a plan command that saves a plan file, that plan file will be created.
This workflow is useful if you need to set up a complicated prior state to replicate a bug or if you want to debug code behaviour given a specific configuration.