Overview
Terraform is written in Go, so you can use Delve for debugging. Modern IDEs like GoLand and VS Code have built-in debugging support for Go projects.Debugging Automated Tests
Debugging tests is often the most straightforward workflow for debugging a section of the codebase.VS Code Test Debugging
The Go extension for VS Code addsrun test | debug test options above all tests in *_test.go files. These work without any configuration.
Simply click “debug test” above any test function in VS Code to start debugging immediately.
Custom VS Code Test Configuration
For more control (e.g., environment variables), create a launch configuration:.vscode/launch.json
- Highlight the test function name
- Start the debugger
- The highlighted test will run with your specified environment
GoLand Test Debugging
GoLand has built-in debugging features. Right-click any test and select “Debug”.Debugging Terraform Operations
You can debug Terraform commands that use real configurations in two ways:Method 1: Debug with dlv CLI
This workflow uses the command-line Delve debugger.Step 1: Build and Start Debug Server
Compile with debug flags and start a headless debug server:Replace
apply with whatever Terraform command you want to debug (e.g., plan, init, etc.).Step 2a: Connect via CLI
Connect to the debug server using Delve CLI:break- Set breakpointscontinue- Continue executionnext- Step to next linestep- Step into functionprint- Print variables
Step 2b: Connect from VS Code
Alternatively, connect using VS Code:.vscode/launch.json
Method 2: Launch from VS Code Debugger
This workflow launches Terraform directly from VS Code.Create Launch Configuration
.vscode/launch.json
Configuration Options
args: Set the Terraform command to debug (e.g.,["plan"],["apply"])-chdir: Specify the working directory containing your Terraform configurationcwd: Alternatively, usecwdinstead of-chdirto set the working directoryenv: Set environment variables likeTF_LOG,TF_ACC, etc.envFile: Load environment variables from a file
Run the Configuration
- Navigate to the Run and Debug view (Activity Bar)
- Select “Run Terraform in debug mode”
- Press the green arrow to start debugging
This is equivalent to running a Terraform command in the project directory. Plan files and state will be created as they would in normal execution.
Setting Breakpoints
In VS Code
Click in the gutter to the left of a line number to set a breakpoint:- Red dot: Breakpoint set
- Gray dot: Breakpoint disabled
- Right-click in the gutter
- Select “Add Conditional Breakpoint”
- Enter a Go expression (e.g.,
name == "example")
In Delve CLI
Debugging Workflows
Debug a Specific Bug
- Create a minimal Terraform configuration that reproduces the bug
- Set breakpoints in the suspected code
- Launch Terraform in debug mode
- Step through execution to find the issue
Debug a Test Failure
- Run the test normally to confirm it fails
- Set breakpoints in the test or implementation
- Debug the test in VS Code or GoLand
- Examine variables and execution flow
Debug Provider Issues
- Set breakpoints in the provider plugin code
- Use the VS Code launch configuration with
TF_LOG=DEBUG - Examine the plugin protocol communication
Debugging Environment Variables
Terraform Logging
Control Terraform’s log output:Plugin Development
Debugging Tools
Delve Commands
Common Delve debugging commands:Useful Debugging Expressions
Common Debugging Scenarios
Panic or Crash
- Enable panic recovery in debugger
- Set breakpoint at suspected panic location
- Examine stack trace when panic occurs
- Check nil pointers and array bounds
Infinite Loop
- Pause execution in debugger
- Examine current location
- Check loop variables and conditions
- Set conditional breakpoint on loop iteration count
Unexpected Behavior
- Set breakpoints at decision points
- Examine variable values at each step
- Compare expected vs actual values
- Trace execution path
Performance Debugging
CPU Profiling
Memory Profiling
Trace Execution
Tips and Best Practices
Next Steps
Testing
Learn about Terraform’s testing requirements
Code Style
Follow code formatting and style guidelines
Building
Build Terraform with debugging flags