Terraform Provider Validation
The Terraform provider that displays helpful messages to users.
Terraform 0.13 added input variable validation support, a much requested feature by module developers and maintainers. However, the variable validation introduced in 0.13 only allowed validation in the context of a single variable, and not much has changed with this feature even though Terraform has now gone 1.0 and beyond.
Consider the following example:
You are introducing an experimental feature into your configuration. Since it is experimental, it is not ready to be released into production, but you want to make the feature available in your development environment for testing/vetting.
Attempting to execute the above code results in an error, but not the one we want.
There have been workarounds suggested in various GitHub issue threads, but none of these workarounds are particularly straightforward to understand (neither writing the code nor reading the error message).
This workaround uses an explicit programming error in the case that the count condition evaluates to true, and will attempt to set a string as the value for a count (which requires a numeric value).
Executing this code results in the offending code snippet being printed to the user, so technically the custom message is shown — but it is a hack based on an intentional programming error, and relies on your user to understand that the programming error is intended to serve as an error message.
Being unable to validate multiple variables together and the inability to produce custom warning/error messages are the exact problems that I solved by creating the terraform-provider-validation.
The Validation provider is a purely logical Terraform provider — it does not call any external services, and instead relies solely upon Terraform’s core library to function.
There are two resources,
validation_error. These resources accept an input condition which, if true, will display a summary message of respective severity during the Terraform execution. If the condition for
validation_error is true, the execution will fail and display the configured information to the user.
The provider also provides two companion data sources,
validation_error. These data sources have exactly the same schema as the resources, but can be used to stop execution prior to the apply phase.
warnings can be used to show users a deprecation notice on input variables being used without causing Terraform to exit. The details section can also be compressed when using the
-compact-warnings option on the Terraform CLI.
Instead of failing the execution, the run continues (and succeeds), and prints a helpful message to the user so that they can better prepare for an upcoming release.