Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 929 Bytes

3.2.3. Input validation.md

File metadata and controls

32 lines (25 loc) · 929 Bytes

Input validation

One interesting thing Stacks does with Jinja is inject a throw(msg) filter which performs a Python raise Exception(msg). This allows halting the execution of a Stacks render if the template is programmed to do so.

We can use this for input validation, for example.

Example: enforcing an owner variable be defined out of a list of valid values

# stacks/owners.tf
{% if 'owner' in var %}{{ throw('var.owner is not defined') }}{% endif %}
{% if var.owner not in var.owners %}{{ throw('var.owner not in var.owners') }}{% endif %}
# stacks/owners.tfvars
owners = [
  "engineering",
  "marketing",
  "operations",
  "sales",
]

From now on, on each stack/layer, you'll have to define a valid owner:

owner = "engineering"

Otherwise Stacks will fail to render.

This can be used for enforcing common resource tagging policies, for ownership tracking, cost analysis, etc.