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:
dlv CLI toolIn this workflow you:
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:
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:
-chdir argument or as a cwd attribute in the launch configuration.args array‘s element reflect the command you’d like to debug.env or envFile 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.