Skip to content

Use datasources on variable validation #28060

@somatorio

Description

@somatorio

Current Terraform Version

Terraform v0.14.8

Use-cases

As we can't use datasources at variable validation, in some cases we can only validate if that values seems like something correct, not if it IS correct...
Examples:
A)

variable "region" {
  type = string
  description = "AWS region where our cluster will be built on"
  validation {
    condition = can(regex("[a-z]{2}-[a-z]+-[0-9]",var.region))
    error_message = "Region must be one of the available regions for your account (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions)."
  }
}

Would validate if the region set looks like a valid region, not if it is an available region for this account

B)
The same case applies for the example at the docs (https://www.terraform.io/docs/language/values/variables.html#custom-validation-rules), it only validates that the variable value looks like a valid AMI Id, not if that value is indeed a valid AMI Id

variable "image_id" {
  type        = string
  description = "The id of the machine image (AMI) to use for the server."

  validation {
    # regex(...) fails if it cannot find a match
    condition     = can(regex("^ami-", var.image_id))
    error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
  }
}

Attempted Solutions

Using the same examples above... It would be much better if we could do something like:
A)

data "aws_regions" "regions_available" {}

variable "region" {
  type = string
  description = "AWS region where our cluster will be built on"
  validation {
    condition = can(regex(join("|",data.aws_regions.regions_available.names),var.region))
    error_message = "Region must be one of the available regions for your account (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions)."
  }
}

Then it would validate using available regions for the account
It would be even better if we could use functions and datasources at error_message, so it could be error_message = "Region must be one of the following regions: ${join(", ",data.aws_regions.regions_available.names)}." and so giving the user valid values on error.

B)

data "aws_ami_ids" "available" {
  owners = ["self", "amazon", "aws-marketplace"]
}

variable "image_id" {
  type        = string
  description = "The id of the machine image (AMI) to use for the server."

  validation {
    # regex(...) fails if it cannot find a match
    condition     = can(regex(join("|",data.aws_ami_ids.available.ids),var.image_id))
    error_message = "The image_id value must be a valid AMI id from your account, amazon or aws marketplace"
  }
}

Proposal

My proposal is to enable the use of other values than the actual var being tested, so we can test it against real values

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions