DEV Community

Eliise
Eliise

Posted on • Edited on

1 2

Debug Terraform (Azure Devops) Provider with VSCode

Introduction

In an effort to save someone the pain of having to go through what I did when trying to get debugging to work on the Terraform Azure Devops Provider acceptance tests, here's the solution.

Prior to starting I was already able to run, debug the unit tests and run the acceptance tests. This article is only about enabling debugging for terraform acceptance tests.

Update: Checkout the Issues section for how to enable codelens debugging.

Environment

The environment is also set up in the Azure Devops devcontainer. The code below can also be found in the repository.

Set up

Add the launch.json and .env below. Edit the .env file as needed for your terraform provider secrets.

NB: The buildFlags attribute was only needed for Azure Devops provider, for example, the Databricks provider work without it.

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch a test",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}",
            "args": [
                "-test.v",
                "-test.run",
                "^${selectedText}$"
            ],
            "env": {
                "TF_ACC": "1",
            },            
            "buildFlags": "-v -tags=all",
            "showLog": true,
            "envFile": "${workspaceFolder}/.env"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

.env

AZDO_PERSONAL_ACCESS_TOKEN=<your_token>
AZDO_ORG_SERVICE_URL=<your_azdo_org>
Enter fullscreen mode Exit fullscreen mode

Make sure Launch a test is selected in the VSCode debug window
Alt Text

Debug a test

Highlight the name of the test you wish to run test in the test file and press F5 or select Start debugging.

Debugging in action

Alt Text

Issues

The only issue with this solution is that you have to highlight the test name and press F5 instead of being able to just select the debug test option above each test in VSCode. You're able to run non-integration tests via that option and run all tests via the corresponding run test option.

Alt Text

I'd love it if anyone could share a solution where they've got it to work with that.

Fixed: Enable codelens debugging

The missing piece to enable codelens debugging is to add the below flags to go.testFlags in .vscode/settings.json:

settings.json

{
    "go.testFlags": [
        "-v",
        "-tags=all",
        "-args",
        "-test.v"
    ],
}
Enter fullscreen mode Exit fullscreen mode

Here's the same code in our project.

Huge thanks to Thomas Meckel for figuring this out!

Credit

Inspired by https://blog.gripdev.xyz/2019/09/12/easily-debugging-terraform-provider-for-azure-in-vscode/

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay