DEV Community

drewmullen
drewmullen

Posted on • Updated on

VSCode + Terraform Provider Development: Setup Debugging

Edit May 2022: This guide now reflects allowing resolution of the service via relative file paths. Previously you had to specify the service via environment variable PKG_NAME=<specific aws service>, now instead you set "env": {"PKG_NAME": "${relativeFileDirname}"} in launch.json and the service is automagically resolved. Thanks to @gdavison for solving this!

To setup debugging while writing to the AWS Terraform Provider, create a .vscode dir in the root of your clone terraform-provider-aws local repo then create a launch.json & private.env inside that .vscode directory.

Setup

$ git clone git@github.com:hashicorp/terraform-provider-aws.git
$ mkdir terraform-provider-aws/.vscode
Enter fullscreen mode Exit fullscreen mode

Create launch.json

$ cat << 'EOF' > terraform-provider-aws/.vscode/launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch a test function",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {"PKG_NAME": "${relativeFileDirname}"}, 
            "args": [
                "-test.v",
                "-test.run",
                "^${selectedText}$"
            ],
            "showLog": true,
            "envFile": "${workspaceFolder}/.vscode/private.env"
        }
    ]
}
EOF
Enter fullscreen mode Exit fullscreen mode

Create private.env

$ cat << 'EOF' > terraform-provider-aws/.vscode/private.env
TF_ACC=1
TF_LOG=INFO
GOFLAGS='-mod=readonly'
EOF
Enter fullscreen mode Exit fullscreen mode

Usage

Reload VSCode and source a profile for terraform's acc tests to use. You can do this by either opening vscode from a terminal with a profile set or by using the AWS Toolkit.

Set break-points in your <resource_name>.go file, highlight the test you want to run and click the green "play" button in VSCode debug panel (top left). Note: if you click any other play buttons, it may not work. It must be the debug pane play button that says "launch a function".

Image description

This one!

Image description

If you found this helpful, please leave me a like :D It helps with many algorithms including my self esteem calculation

Top comments (3)

Collapse
 
scottschreckengaust profile image
Scott Schreckengaust • Edited

Nice! Here are extension recommendations:

$ cat << 'EOF' > terraform-provider-aws/.vscode/extensions.json
{
    "recommendations": [
        "amazonwebservices.aws-toolkit-vscode",
        "hashicorp.terraform",
        "github.vscode-pull-request-github",
        "golang.go"
    ]
}
EOF
Enter fullscreen mode Exit fullscreen mode
Collapse
 
danquack profile image
Daniel Quackenbush

I recently used this to develop on the provider and it made it so much easier! Thank you.

Collapse
 
drewmullen profile image
drewmullen

so glad it was helpful!