Skip to content

Allow warning messages within variable validationΒ #32594

Open
@derekwinters

Description

Terraform Version

Terraform v1.3.7

Use Cases

I want to be able to raise a warning in a Terraform module, similar to how a provider raises a warning for deprecated arguments.

We focus heavily on maintaining backwards compatibility and giving time to transition how modules are used, so we end up deprecating module variables in favor of new variables from time to time. We document this in variable descriptions and the logic that uses them handles the logic for transitioning, but being able to clearly highlight the deprecations in the plan would be very useful as well.

Attempted Solutions

The way we accomplish this today is simply in descriptions, readme/documentation, and in the implementation of the variables.

As a highly simplified example, we may want to change the name of a variable as we update legacy modules to use standard variable name structures and input types across all our modules.

variable "name" {
  description = "The name of the bucket to create. DEPRECATED: Use bucket_name instead. If bucket_name is provided, this string is ignored."
  type        = string
  default     = ""
}

variable "bucket_name" {
  description = "The unique name of the bucket to create. This will be combined with the environment name to create the final bucket name."
  type        = string
  default     = ""
}

variable "environment" {
  description = "The environment name for this resource."
  type        = string
}

locals {
  # Compare two variables to ensure one was provided. Exit with error if both are empty strings.
  provided_name = coalesce(var.bucket_name, var.name)
  bucket_name   = var.bucket_name != "" ? join("-",[var.environment, local.provided_name]) : local.provided_name
}

Proposal

It would be useful to highlight the deprecated variables of a module within the Terraform plan. For this to work, it would need to be possible to choose warning or error for the validation.

variable "name" {
  description = "The name of the bucket to create. DEPRECATED: Use bucket_name instead. If bucket_name is provided, this string is ignored."
  type        = string
  default     = ""

  validation {
    condition     = var.name != ""
    error_message = "This variable has been deprecated, use bucket_name instead."
    error_level   = "warning"
  }
}

This would then trigger a warning message at the end of the plan, similar to provider warnings or variable validation errors.

Warning: Module Variable Condition

  on ../modules/example/variables.tf line 18:
   18: variable "name" {

This variable has been deprecated, use bucket_name instead.

This was checked by the validation rule at variables.tf:23.

References

No response

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions