Debugging - jfrog/terraform-provider-artifactory GitHub Wiki
Debugging a terraform provider is not straightforward. Terraform forks your provider as a separate process and then connects to it via RPC. Normally, when debugging, you would start the process to debug directly. However, with the terraform + go architecture, this isn't possible. So, you need to run terraform as you normally would and attach to the provider process by getting it's pid. This would be really tricky considering how fast the process can come up and be down. So, you need to actually halt the provider and have it wait for your debugger to attach.
See Terraform Debugging for additional information.
Having said all that, here are the steps:
- Install delve
- Keep in mind that terraform will parallel process if it can, and it will start new instances of the TF provider process when running apply between the plan and confirmation. Add a snippet of go code to the close to where you need to break where in you install a busy sleep loop:
debug := true
for debug {
time.Sleep(time.Second) // set breakpoint here
}
Then set a breakpoint inside the loop. Once you have attached to the process you can set the debug
value to false
, thus breaking the sleep loop and allow you to continue.
2. Compile the provider with debug symbology (go build -gcflags "all=-N -l"
)
3. Install the provider (change as needed for your version)
# this will bump your version by 1 so it doesn't download from TF. Make sure you update any test scripts accordingly
$ make install
- Run your provider:
terraform init && terraform plan
- it will start in this busy sleep loop. - In a separate shell, find the
PID
of the provider that got forkedpgrep terraform-provider-artifactory
- Then, attach the debugger to that pid:
dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient attach $pid
A 1-liner for this whole process is:dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient attach $(pgrep terraform-provider-artifactory)
- In intellij, setup a remote go debugging session (the default port is
2345
, but make sure it's set.) And click thedebug
button - Your editor should immediately break at the breakpoint from step 2. At this point, in the watch window, edit the
debug
value and set it to false, and allow the debugger to continue. Be ready for your debugging as this will release the provider and continue executing normally.
You will need to repeat steps 4-8 everytime you want to debug