Declare typed input variables
Give a configuration a reusable input
An input variable is a named value supplied to a Terraform module. Instead of editing a resource whenever a setting changes, you declare the accepted input and reference it with var.<name>. For example, a small service might need a replica count of two in one environment and four in another. This complete local configuration calculates an output; it does not create servers. All examples use synthetic data, and results below are predictions, not recorded executions.
variable "replicas" { description = "Number of simulated service replicas." type = number default = 2 nullable = false
validation { condition = var.replicas >= 1 && var.replicas <= 8 && floor(var.replicas) == var.replicas error_message = "Choose a whole number from 1 through 8." }}
output "requested_replicas" { value = var.replicas}With no override, the output requested_replicas is expected to be 2. A value of 4 changes that output to 4. The declaration defines the interface; var.replicas reads the selected value.
Define the contract
The block label replicas is the input name. description explains what callers should supply, type constrains the accepted type, and default makes the input optional for callers. A variable without a default needs a supplied value. Terraform can convert compatible inputs to the declared type, so a number constraint is not the same as a prohibition on all numeric strings. A type constraint also cannot express every business rule: a replica count must be a whole number within a permitted range.
Reject invalid numbers before changes
The validation condition in the example checks three things: the minimum, maximum and whether rounding down leaves the number unchanged. && joins Boolean tests; all must hold. floor rounds toward negative infinity. The message explains how to fix an invalid input. A value of 0 fails the minimum, 9 fails the maximum, and 2.5 fails the whole-number test. A nonconvertible string fails the type contract. A plan with a known invalid input should report a diagnostic rather than produce a usable change plan. Exact wording is not verified. Validation of values that remain unknown can be deferred; it is not a substitute for provider constraints or service health checks.
Distinguish required inputs, null and optional attributes
The following is a separate declaration, not part of the first lab:
variable "service_name" { description = "Synthetic service identifier." type = string nullable = false}service_name must be supplied because it has no default. With nullable = false, a null input cannot remain null; for a variable with a non-null default, explicitly passing null uses that default, while a required variable without a default rejects null. By contrast, the default nullable behavior allows explicit null to override a default. This flag concerns the whole variable, not every nested attribute.
variable "settings" { type = object({ name = string logging = optional(bool, true) }) default = { name = "catalog" }}Here the whole input has a default object, and the optional logging attribute has its own default. Supplying an object with only name still produces logging = true. Supplying logging = false preserves false. Optional object attributes are not the same as making the entire variable optional. If the variable block had no default, the object itself would still need to be supplied.
Protect input values
Marking an input sensitive hides it in ordinary Terraform display contexts, but does not encrypt it or keep it out of saved plans and state. A sensitive output must be treated accordingly. Never put real credentials into lesson fixtures, command arguments, or committed variable files. Terraform 1.10 introduced ephemeral variables for values that must not be persisted in plan/state files. This is a version-scoped declaration illustration, not a complete working secret integration:
variable "temporary_label" { type = string sensitive = true ephemeral = true}Ephemeral data is restricted to contexts that can consume it without persistence; it cannot be passed to an ordinary persistent resource argument or a normal root output. Sensitive and ephemeral solve different problems and do not guarantee that logs or external systems never expose the data. The provisional CLI pin inherited by this track has not been executed.
Practice with a local contract
Read lab-fixtures/input-variables/typed-contract/ and its parent README. In an approved isolated environment with the verified pinned CLI, initialize and run a plan using only the supplied synthetic values. Predict results for 1, 8, 0, 2.5 and 9 before trying them. For the worksheet, design a new bounded worker-count variable with a default of 3 and range 2–6. Do not copy the replica rule unchanged. Next: variable files and precedence. Related reference: Declare typed input variables.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Declare typed input variables — knowledge check
Check input contracts and assignment decisions.
What reads the replicas input?
Which input passes the lesson replica rule?
What happens if replicas is explicitly null in the first example?
What does sensitive = true do by itself?
What does an optional attribute with default true do when logging is explicitly false?