How to Test a Terraform Provider from a Local Binary

Wale

Terraform providers extend Terraform’s core functionality, allowing it to interact with external APIs or services. This guide is for anyone who wants to test or develop a custom Terraform provider using a local binary, specifically using the Terracurl provider, which allows making HTTP requests from Terraform.

By using a local binary, you can iterate quickly without needing to publish your provider to a remote registry.

Setup

Step 1: Override Provider Installation

Create a dev.tfrc file with the following contents:

provider_installation {
  dev_overrides {
    "devcastops.com/devops-rob/terracurl" = "./plugins"
  }
  direct {}
}

This configuration tells Terraform to look for the terracurl provider in the local ./plugins directory instead of the Terraform Registry. This is useful when testing a provider you’re developing locally or to avoid downloading it from a remote source.

Step 2: Declare the Provider in Terraform

Create a terraform.tf file with the following contents:

terraform {
  required_providers {
    terracurl = {
      source  = "devcastops.com/devops-rob/terracurl"  # This can be any unique string
    }
  }
}

provider "terracurl" {
  # Configuration options (if any)
}

💡 Note: The source value can be any made-up string, as long as it doesn’t point to an actual public registry source. Terraform uses this to match the local override in your CLI config. Just make sure it matches the one used in your dev.tfrc file.

Step 3: Configure the Terraform CLI

Set the TF_CLI_CONFIG_FILE environment variable to point to the dev.tfrc file:

export TF_CLI_CONFIG_FILE=$(pwd)/dev.tfrc

This ensures that your local CLI uses the overridden provider installation settings.

Step 4: Download the Provider Binary

Download the Terracurl plugin binary for your operating system and move it into the plugins directory. For Linux AMD64:

wget -q https://github.com/devops-rob/terraform-provider-terracurl/releases/download/v2.0.0-rc1/terraform-provider-terracurl_2.0.0-rc1_linux_amd64.zip && \
unzip -q terraform-provider-terracurl_2.0.0-rc1_linux_amd64.zip && \
mkdir -p plugins && \
mv terraform-provider-terracurl_v2.0.0-rc1 plugins/ && \
rm terraform-provider-terracurl_2.0.0-rc1_linux_amd64.zip

Note: This example is for Linux AMD64. For macOS, Windows, or other platforms, download the appropriate binary from the GitHub releases page.

Step 5: Test the Setup

Create a main.tf file with a simple test configuration to verify everything works. This example makes a GET request to a fake API and outputs the JSON response:

resource "terracurl_request" "get_products" {
  name   = "get-products"
  url    = "https://fakestoreapi.com/products?limit=5"
  method = "GET"

  response_codes = [
    200
  ]
}

output "get_products" {
  value = jsondecode(terracurl_request.get_products.response)
}

Breakdown of the Example:

  • terracurl_request is a resource type from the provider used to make HTTP requests.
  • name gives a logical name to the request.
  • url specifies the endpoint to call.
  • method is set to GET.
  • response_codes lists accepted HTTP status codes.
  • The output block decodes the response body into JSON and prints it to the terminal.

Run the following commands to execute the configuration:

terraform fmt
terraform apply -auto-approve

If everything is set up correctly, Terraform will output the response from the fake API as JSON.

Wrap-Up & Troubleshooting

  • ✅ You should now see JSON output from the fake store API.
  • 🛠 If Terraform cannot find the provider, double-check:
    • The TF_CLI_CONFIG_FILE environment variable
    • Your local directory structure (./plugins)
    • File permissions on the binary

This setup is great for rapid development, debugging, and validation of a custom Terraform provider. Happy coding!