Debug a Terraform Provider
Note: We recommend using the Terraform Plugin Framework for new provider development because it offers significant advantages compared to the SDKv2. Refer to the Plugin Framework tutorials to learn how to create providers using the framework.
In this tutorial, you will add custom error messages to a Terraform provider that interacts with the API of a fictional coffee-shop application called Hashicups and view detailed Terraform provider logs.
Prerequisites
To follow this tutorial, you need:
- a Golang 1.15+ installed and configured.
- the Terraform 0.14+ CLI installed locally.
- Docker and Docker Compose to run an instance of HashiCups locally.
Navigate to your terraform-provider-hashicups
directory. Next, fetch new changes.
Then, checkout the implement-complex-read
branch. This step is optional but recommended to ensure that your code matches the code from the previous tutorials.
Your directory should have the following structure.
If you're stuck at any point during this tutorial, refer to the debug-tf-providers
branch to see the changes implemented in this tutorial.
Update error messages
In your hashicups/provider.go
file, the providerConfigure
function returns an interface{}
and a diag.Diagnostics
type. diag.Diagnostics
can return multiple errors and warnings to Terraform, giving users more robust error and warning messages.
The diag.FromError()
function casts standard Go errors to diag.Diagnostics
type notifying the user whenever the provider errors.
Replace the return nil, diag.FromError(err)
line in your providerConfigure
function with the following code snippet. There should be two replacements — one where the user's HashiCups credentials provided and the other where its not. Notice how this appends a diag.Diagnostic
type to the existing diags
variable; this allows your provider to return multiple error message. This will produce an error message containing Summary
and Detail
contents.
Your providerConfigure
function should look like the following.
Format your code.
Notice how the Diagnostic
severity is set to diag.Error
. There are two levels of severity — diag.Error
and diag.Warning
. You will create and view a warning message in the next section.
Test error message
Now that you've added a custom error message, verify that the provider returns your error message.
First, confirm that you are in the terraform-provider-hashicups
root directory.
Next, build the binary and move it into your user Terraform plugins directory. This allows you to sideload and test the custom provider. Select the tab for your operating system for specific instructions.
Tip
The Perform CRUD operations with Providers tutorial explains why and how to sideload custom providers. Refer to it to learn more about where to install custom providers and how to reference them in your configuration.
Navigate to the terraform-provider-hashicups/examples
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
Update your HashiCups provider configuration so it uses the wrong credentials.
Next, initialize your workspace to refresh your HashiCups provider, then apply. This should return the error message you defined above.
Because your provider errors, your Terraform state doesn't update when you ran the terraform apply
.
Add warning message
Add the following snippet after diags
is declared in providerConfigure
function.
Your providerConfigure
function should look like the following.
Format your code.
Notice that you have set the Diagnostic
's severity to diag.Warning
. When you use your provider, both warning and messages appear because diags
contains both. A warning message will only appear when the provider also errors.
The Diagnostic
's detail contains additional information about the warning message, including which function it was triggered in. This could provide handy while debugging.
Test warning message
Now that you've added a warning message and updated your Terraform log level, verify that the provider returns both your warning and error message.
First, confirm that you are in the terraform-provider-hashicups
root directory.
Next, build the binary and move it into your user Terraform plugins directory. This allows you to sideload and test the custom provider. Select the tab for your operating system for specific instructions.
Tip
The Perform CRUD operations with Providers tutorial explains why and how to sideload custom providers. Refer to it to learn more about where to install custom providers and how to reference them in your configuration.
Navigate to the terraform-provider-hashicups/examples
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
Next, initialize your workspace to refresh your HashiCups provider, then apply. This should return both a warning and error message.
Because your provider errors, your Terraform state doesn't update when you ran the terraform apply
.
Fix provider
Comment out or remove the warning message, then recompile your Terraform provider.
Fix your Terraform configuration to use the correct credentials.
Finally, re-initializing your workspace and applying the configuration.
There should be no errors or warning messages.
Tip
You can also retrieve detailed Terraform and provider logs by setting the environment variable TF_LOG
. Please include a detailed logs with any bug reports so the author can identify and address the bug. To learn more about log levels and how to interpret a crash log, refer to the Debugging Terraform Documentation.
Next steps
In this tutorial, you added error and warning messages to your HashiCups provider and implemented a nested read function.
If you were stuck during this tutorial, checkout the debug-tf-providers
branch to see the changes implemented in this tutorial.
- The Terraform Provider Scaffold is a quick-start repository for creating a Terraform provider. Use this GitHub template when you're ready to create a custom provider.
- To learn more about the Terraform Plugin SDK, refer to the Terraform Plugin SDK Documentation.