Skip to content
Novus Examples
tf1.4 KB

Terraform Variables with Validation Blocks

Seven Terraform variables covering string, number, map and a map(object) with an optional attribute and a default, plus two validation blocks with error messages. The sensitive variable's default is explicitly a SAMPLE note, not a credential.

Preview — first 50 linestf
variable "environment" {
  description = "Deployment environment name."
  type        = string

  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "environment must be one of dev, staging, prod."
  }
}

variable "region" {
  description = "Primary region for the fictional examplecloud provider."
  type        = string
  default     = "eu-example-1"
}

variable "secondary_region" {
  type    = string
  default = "us-example-2"
}

variable "instance_count" {
  description = "Number of application instances."
  type        = number
  default     = 2

  validation {
    condition     = var.instance_count > 0 && var.instance_count <= 20
    error_message = "instance_count must be between 1 and 20."
  }
}

variable "instance_size" {
  type    = string
  default = "small"
}

variable "subnets" {
  description = "Subnet definitions keyed by name."
  type = map(object({
    cidr = string
    zone = string
    public = optional(bool, false)
  }))
  default = {
    app-a = { cidr = "10.20.1.0/24", zone = "a" }
    app-b = { cidr = "10.20.2.0/24", zone = "b" }
  }
}
63 lines total — download for the full file.

Specifications

Variables
7
Validation Blocks
2
Complex Types
1
Optional Attribute
true
Sensitive Variables
1

Testing contract

Expected to pass
Scenario
Parse Terraform variable type constraints, including map(object) with optional()
Expected result
All seven variables resolve, the subnets type reports an optional public attribute defaulting to false, and both validation conditions parse as HCL expressions

What is a .tf file?

A .tf file is a Terraform configuration written in HCL. It declares infrastructure as blocks — terraform, provider, resource, data, variable, output, module, locals — whose bodies hold arguments and nested blocks, with values drawn from expressions, interpolation, and functions. Terraform reads every .tf file in a directory as one merged configuration, so ordering across files carries no meaning.

How to use this file

Use an example .tf file to test HCL parsers, infrastructure linters and policy engines, and configuration migrators, checking block-and-label parsing, heredoc strings, expression references between resources, and that a deprecated-syntax file is flagged rather than silently accepted.

How to use this file for testing

“Terraform Variables with Validation Blocks” is a deterministic Novus Examples fixture for Config testing, Config parsing, Syntax highlighting. TOML, INI, YAML, .env, and dotfile configuration samples with nested sections and typed values — for testing config parsers, loaders, and environment tooling.

Documented properties for this file: TF · 1,448 bytes. Compare results against paired or grouped companions on this page when present (clean↔damaged, searchable↔scanned, or format twins) so scores stay reproducible across runs.

Download the file once, keep the path stable in CI or local scripts, and treat the spec table as the contract: dimensions, seeds, field lists, and roles are intentional. Corrupt or invalid samples are labelled as such — expect parsers to fail loudly rather than silently accept them.

Pipeline and infrastructure fixtures are inert configuration: steps reference fictional images and scripts, and nothing here executes. Run your linter, schema validator, migrator, or policy engine against them, and expect the deprecated-syntax and intentionally invalid variants to be rejected.

Point your config loader at the file and assert it reads the documented sections and typed values, including any deliberately-tricky nesting or comments.

Generated by generation/pipelines.py. Free for any use, no attribution required — license.