Inputs, expressions, and stable resource keys¶
Inputs and derived values¶
Use variables for caller choices and locals for derived expressions.
Choose types deliberately: string, number, bool, collections,
and structured object types. Validation catches mistakes early; it is not
a replacement for live provider checks.
terraform {
required_version = ">= 1.7, < 2.0"
}
variable "prefix" {
description = "Short lowercase prefix for the learning records."
type = string
default = "learn"
}
variable "environments" {
description = "Unique environment keys; stable keys prevent index-based address changes."
type = set(string)
default = ["dev", "test"]
validation {
condition = length(var.environments) > 0 && alltrue([for value in var.environments : contains(["dev", "test", "prod"], value)])
error_message = "Choose one or more of dev, test, and prod."
}
}
locals {
environments = {
for environment in var.environments : environment => {
name = "${var.prefix}-${environment}"
retention_days = environment == "prod" ? 90 : 30
}
}
}
resource "terraform_data" "environment" {
for_each = local.environments
input = each.value
}
output "planned_environments" {
description = "Derived values available during planning; no cloud access occurs."
value = local.environments
}
The environment collection is a set. A for expression converts it into a
map containing derived names and retention numbers. for_each creates
one data record for each stable key.
Why keys matter¶
terraform_data.environment["dev"] and
terraform_data.environment["prod"] are different addresses.
Removing dev proposes removing that record. Renaming a key normally means
a different address unless you explicitly describe a supported move.
By contrast, changing the display name inside an existing key keeps its
address, though a provider may still replace the remote object.
Use count for genuinely index-based instances and for_each for named
entities. Inserting a list item can shift count indexes and produce
surprising changes if indexes are being used as identities.
Expressions and functions¶
String interpolation uses "${var.prefix}-${environment}".
The conditional condition ? true_value : false_value selects retention.
contains restricts choices, alltrue checks every element, regex with
can validates shape, and cidrhost can validate CIDR syntax.
An expression can be unknown during plan when it depends on an ID the
provider will return during apply. Do not force unknown values into
for_each keys. Use stable configuration keys and computed values as map values.
Outputs and sensitive values¶
Outputs expose a module interface, not just terminal decoration.
Prefer a resource ID/name over a whole resource object.
sensitive = true redacts normal display; it does not encrypt state or saved
plans. Avoid secrets in both examples and outputs.
Practice¶
terraform -chdir=examples/02-expressions init
terraform -chdir=examples/02-expressions test
terraform -chdir=examples/02-expressions plan -var='environments=["dev","prod"]'
The quoted array uses Bash-style quoting; adapt it to your shell. Expected: two stable keys and 90-day production learning metadata. No actual log retention policy is deployed.