Generate Self-Service Forms From Terraform Variable Blocks
How to derive a self-service form from a Terraform or OpenTofu module's variable blocks, so types, defaults and validations never drift from the module.
CloudEstate team · · 5 min readKey takeaways
- A hand-written form is a second description of the module, and the two will eventually disagree.
- Type constraints, defaults, descriptions, sensitive flags and simple validations map directly onto form controls.
- A validation using contains with a literal list is an allow-list, so it can be rendered as a select instead of free text that fails at plan time.
- Inference has limits: complex validations stay in Terraform and are enforced at validate and plan.
- Writing variables well for a form generator also makes the module better for people using it from the command line.
A Terraform or OpenTofu module already declares almost everything a self-service form needs. Each variable block carries a name, a type, a description, a default and often a validation rule. If the form is generated from those blocks instead of written by hand, there is one source of truth, and the form cannot drift from the module it deploys.
Why do hand-written forms drift from their modules?
A hand-written form is a second description of the module's inputs, kept in a different file, often in a different repository, usually by a different person. Every change to a variable has to be made twice. The second change is the one that gets forgotten.
The failures are specific and they all surface late:
- The module adds an allowed instance size and the form does not offer it.
- The module removes a value and the form still offers it, so the request fails at plan, minutes after the engineer submitted it.
- The module adds a required variable and the form has no field for it, so every request fails.
- The form displays one default and the module applies another.
None of these is caught by reviewing the module, because the module is correct. The defect lives in the gap between the two descriptions.
What does a variable block already tell you?
This is a reasonable mapping from what a module declares to what a form should show:
| Module declaration | Form control | Reason |
|---|---|---|
type = string | Text field | Free text is the only safe assumption |
type = number | Number field | Rejects non-numeric input before submission |
type = bool | Switch | Two states, no typing |
type = list(string) | Multi-value input | Each entry is one string |
type = map(string) | Key and value editor | Tags and labels are the usual case |
validation with contains([...], var.x) | Select | The list is the complete set of accepted values |
sensitive = true | Masked field | The author has said the value is a secret |
default present | Optional, pre-filled | Terraform would use the default anyway |
No default | Required | Terraform would refuse to plan without it |
description | Help text | Written by the person who knows what the input means |
Name such as vpc_id | Picker of existing resources | The value must be the ID of something that already exists |
The last row is the only one that is inference from a name instead of a declaration, and it is the one that saves the most support questions. Nobody should have to open another console tab to copy a VPC ID into a text box.
What does this look like on a real variable?
variable "instance_size" {
type = string
description = "Instance size for the API servers."
default = "t3.small"
validation {
condition = contains(["t3.small", "t3.medium", "t3.large"], var.instance_size)
error_message = "Choose t3.small, t3.medium or t3.large."
}
}
variable "db_password" {
type = string
description = "Master password for the database."
sensitive = true
}
variable "vpc_id" {
type = string
description = "VPC to deploy into."
}
From those three blocks, with no form code:
instance_sizebecomes a select with three options, labeled "Instance Size", witht3.smallpre-selected. It is optional because it has a default. The description and the validation's error message appear as help text.db_passwordbecomes a masked field. It is required because it has no default.vpc_idbecomes a picker listing the VPCs found in the account, and it is required.
When someone later adds "t3.xlarge" to the list in the validation, the select gains a fourth option the next time the form is derived. There is nothing else to update.
Where does inference stop?
A generator should be honest about what it cannot read.
Only simple validations become controls. An allow-list written with contains and a literal list, or a chain of equality checks against the same variable, can be read reliably. A regular expression, a length check or a condition that compares two variables cannot be turned into a control. Those rules stay in the module and are enforced by Terraform during validate and plan, which is where they were always enforced.
Name-based inference is a heuristic. Treating vpc_id as a reference to a VPC is right nearly every time. Treating any variable with "key" in its name as a secret is not. A name heuristic is only trustworthy for strings: private_nodes is a boolean and private_subnet_ids is a list, and neither holds a secret. An explicit sensitive = true should always win over a guess.
Layout is generic. Fields can be grouped by what their names suggest, such as network, sizing and credentials, but the result is a competent form, not a designed one.
How should you write variables so they make good forms?
- Give every variable a
description. It becomes the help text, and command-line users read it too. - Declare a
type. An untyped variable gives a generator nothing to choose a control with. - Write allow-lists as
contains([...], var.name)with literal values, not as a regular expression. - Mark secrets with
sensitive = trueinstead of relying on the variable's name. - Use conventional names for references to existing infrastructure:
vpc_id,subnet_ids,security_group_ids. - Set defaults deliberately. A default makes the field optional, so only give one where the default is a value you would be happy to see in production.
How CloudEstate handles this
In CloudEstate, a blueprint is a Terraform or OpenTofu module plus the documentation around it. An author pastes a module or points at a Git repository, and the form appears beside the editor as they type, re-derived from the module's variable blocks. A validation with contains([...]) becomes a select, sensitive = true becomes a masked field, and a variable called vpc_id becomes a picker filled from the inventory that cloud discovery has recorded. Defaults and descriptions come straight from the module. Secret inputs are encrypted separately from plain ones, so plain values can be displayed while secrets stay on the server.
The provider block is written by the platform, not the blueprint, because region and account belong to the environment. That is why they are not form fields, and why one blueprint can deploy to different regions without being edited.
The service catalog ships with 30 blueprints across AWS and Azure, each checked with terraform validate. A submitted form then runs the governed path: render, init, validate, plan, policy, cost, graph, approval, apply. For where this fits in the larger picture, see what an internal developer platform is, or ask for a demo to see a form derived from one of your own modules.
Frequently asked questions
Yes. Variable blocks carry a name, type, description, default, sensitivity flag and validation rules, which is enough to choose a control, a label, help text and whether the field is required. The generated form stays in step with the module because it is re-derived from it.
When the condition is an allow-list such as contains with a literal list of strings and the variable, the listed values are the only ones the module will accept. A generator can read that list and offer it as the options of a select.
They remain in the module and are enforced by Terraform or OpenTofu during validate and plan. The form shows a plain field, and the error message from the validation block can be shown as help text.
Yes. Variable blocks use the same syntax in both, so the same derivation applies.
- terraform
- opentofu
- self-service infrastructure
- service catalog