Skip to content

Terraform Expressions, Lifecycle, Validation and Secrets

Step 9 of 18

Terraform Expressions, Lifecycle, Validation and Secrets

Introduction

This Terraform expressions and validation lesson completes the Terraform configuration area of the Terraform Associate (004) exam and covers objectives 4e to 4h. Together with the previous lesson, it covers eight official objectives. HashiCorp does not publish a question weighting for this area. The exam lasts one hour, tests Terraform 1.12, and mixes true/false, multiple-choice and multiple-answer questions.

Configuration is more than a list of resources. Real projects compute names, loop over collections, control the order of operations, refuse bad input, and keep secrets out of logs. These are the tools for that work: expressions and functions, dependency control, lifecycle rules, custom conditions, and protection of sensitive data.

After this lesson you will be able to write expressions with conditionals, for expressions and common functions, control dependencies with depends_on, shape replacement behaviour with lifecycle, add validation, preconditions, postconditions and check blocks, and handle secrets with sensitive, ephemeral values, write-only arguments and HashiCorp Vault.

Learning objectives

  • Write expressions: interpolation, conditionals, for expressions, splat and dynamic blocks.
  • Use common built-in functions on strings, collections, numbers and files.
  • Distinguish implicit from explicit dependencies and use depends_on only when needed.
  • Use lifecycle arguments: create_before_destroy, prevent_destroy, ignore_changes, replace_triggered_by.
  • Use validation, precondition, postcondition and check blocks and know when each runs.
  • Protect sensitive data with sensitive, ephemeral values and write-only arguments, and explain how Vault centralises secret delivery and what still determines whether values persist.

These are focused examples, not a single module to concatenate: complete provider requirements, variable declarations and referenced resources before using a fragment. Examples using # ... intentionally omit unrelated required settings.

4e. Expressions and functions

An expression refers to or computes a value. Literals, references, operators, function calls and conditionals are all expressions.

Operators, interpolation and conditionals

HCL
locals {
  name        = "${var.project}-${var.environment}"       # string interpolation
  is_prod     = var.environment == "prod"                   # comparison
  replicas    = local.is_prod ? 3 : 1                       # conditional: condition ? true_val : false_val
  disk_total  = var.disk_gb * local.replicas                # arithmetic
}

The conditional operator has the form condition ? value_if_true : value_if_false; both results must be convertible to a common type.

for expressions

A for expression transforms one collection into another. Square brackets construct a tuple and curly braces construct an object; Terraform can convert these to compatible list or map types when required.

HCL
locals {
  upper_names = [for n in var.names : upper(n)]
  name_to_len = { for n in var.names : n => length(n) }
  prod_only   = [for s in var.servers : s.name if s.env == "prod"]
}

The optional if filters items. Do not confuse a for expression (builds a value) with the for_each meta-argument (creates resource instances).

Splat expressions

For count-based instances, aws_instance.web[*].id collects their IDs, like [for i in aws_instance.web : i.id]. A for_each resource is a map, so use a for expression rather than a splat. The [*] operator also adapts a nullable single value to a tuple of zero elements for null or one element for a non-null value.

Dynamic blocks

Some resources have repeated nested blocks (for example ingress rules in a security group). A dynamic block generates them from a collection.

HCL
resource "aws_security_group" "web" {
  name = "web"

  dynamic "ingress" {
    for_each = var.allowed_ports
    content {
      from_port   = ingress.value
      to_port     = ingress.value
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    }
  }
}

Use dynamic blocks sparingly: writing the blocks explicitly is easier to read when the number is small and fixed.

Built-in functions

Terraform supplies built-in functions called as name(args). HCL configurations cannot declare their own function bodies, but providers can expose additional functions called as provider::local_name::function_name(args). Those worth knowing:

Category Functions Example
String upper, lower, format, join, split, replace, trimspace, substr join("-", ["a", "b"]) returns a-b
Collection length, concat, merge, keys, values, lookup, element, contains, distinct, flatten, toset, tolist, tomap, zipmap merge(local.common_tags, var.tags)
Numeric min, max, ceil, floor, abs max(1, var.nodes)
Filesystem file, templatefile, fileexists, filebase64 file("${path.module}/policy.json")
Encoding jsonencode, jsondecode, base64encode, yamlencode jsonencode({ a = 1 })
Network cidrsubnet, cidrhost cidrsubnet("10.0.0.0/16", 8, 2) returns 10.0.2.0/24
Type conversion tostring, tonumber, tobool tonumber("5")
Error handling try, can try(var.cfg.port, 8080)
Date and time timestamp, formatdate, timeadd formatdate("YYYY-MM-DD", timestamp())

You can try functions interactively with terraform console, which opens a prompt that evaluates expressions against your configuration and state.

Example. An operations team builds tags in one place: tags = merge(local.common_tags, { Name = "${local.name}-web" }). A later requirement adds a CostCenter tag in common_tags and every resource picks it up in a single edit.

Worked example: building subnets from a CIDR

Scenario. A VPC uses 10.20.0.0/16. The team needs one /24 subnet for each of three availability zones, named after the zone, without computing CIDRs by hand.

Step by step.

  1. Each /24 inside a /16 needs 8 extra bits, so the newbits argument of cidrsubnet is 8.
  2. The netnum is the subnet number: 0, 1, 2 for the three zones.
  3. Assign each zone a stable subnet number, then use a for expression over that map.
HCL
locals {
  zone_numbers = {
    "eu-west-1a" = 0
    "eu-west-1b" = 1
    "eu-west-1c" = 2
  }
  subnets = { for zone, number in local.zone_numbers : zone => cidrsubnet("10.20.0.0/16", 8, number) }
}

local.subnets is { "eu-west-1a" = "10.20.0.0/24", "eu-west-1b" = "10.20.1.0/24", "eu-west-1c" = "10.20.2.0/24" }, which can feed for_each = local.subnets on an aws_subnet resource.

Why other designs are weaker. Hard-coded CIDR strings drift from the VPC range and are easy to mistype. Using count with an index couples the subnet to its position in the list, so inserting a zone in the middle would shift the CIDRs and cause replacements. Keying by zone name alone is not enough if the CIDR still comes from its list position. The explicit zone-to-number mapping above keeps both resource addresses and CIDR assignments stable when a new zone is added with a new number.

For resource addressing and collections, revisit Terraform resources, variables and outputs.

4f. Implicit and explicit dependencies, and lifecycle

Implicit and explicit dependencies

Terraform infers most ordering from references (the previous lesson). When a dependency exists but is not visible through a reference, you declare it with depends_on:

HCL
resource "aws_iam_role_policy" "app" {
  role   = aws_iam_role.app.id
  policy = data.aws_iam_policy_document.app.json
}

resource "aws_instance" "app" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  iam_instance_profile = aws_iam_instance_profile.app.name

  depends_on = [aws_iam_role_policy.app]   # the app needs the policy to exist at boot, but does not reference it
}

depends_on takes a static list of references to resource or module objects; arbitrary computed expressions cannot choose dependencies dynamically. Use it sparingly: it makes Terraform treat the dependent as having possibly unknown values, which can produce larger plans. Prefer a real reference whenever one exists. It is also available on module blocks and, with a few limits, on data sources and outputs.

The lifecycle block

The lifecycle meta-argument changes how Terraform treats a resource:

Argument Effect
create_before_destroy Creates a replacement before deleting the old object; requires compatible names and quotas and does not itself guarantee application availability
prevent_destroy Plans that would destroy the resource produce an error
ignore_changes Names attributes whose changes made outside Terraform are ignored in later plans (for example tags added by an external system)
replace_triggered_by Forces replacement when a referenced resource or attribute changes
precondition / postcondition Custom checks around the resource (below)
HCL
resource "aws_launch_template" "web" {
  name_prefix   = "web-"
  image_id      = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_db_instance" "main" {
  # ...
  lifecycle {
    prevent_destroy = true
    ignore_changes  = [tags["LastScan"]]
  }
}

Note that prevent_destroy stops a destroy plan only while the block exists in the configuration. If the whole resource block is deleted, the protection is gone.

Comparison: dependency tools

Tool When to use
Reference (aws_vpc.main.id) Default; the dependency is also data
depends_on Ordering needed but no value is passed
replace_triggered_by One resource must be recreated when another changes
create_before_destroy Replacement must not leave a gap

4g. Custom conditions: validation, preconditions, postconditions and check blocks

Terraform lets you state assumptions in code so that a bad situation fails early with a clear message.

Feature Where When it is evaluated Failure effect
validation Inside a variable When its condition can be evaluated; may be deferred if values are unknown A false condition stops the operation
precondition lifecycle of a resource/data source, or directly inside an output Before the guarded action or output is accepted; deferred to apply if needed A false condition blocks that action
postcondition lifecycle of a resource or data source After planning/applying the resource or reading the data source, when values are known A false condition blocks dependent actions; it does not roll back completed changes
check block Top level, with optional scoped data source At the end of plan and apply Produces a warning only; does not stop the run
HCL
variable "cidr" {
  type = string
  validation {
    condition     = can(cidrhost(var.cidr, 0))
    error_message = "cidr must be a valid CIDR block."
  }
}

data "aws_ami" "base" {
  most_recent = true
  owners      = ["self"]
  filter {
    name   = "tag:Approved"
    values = ["true"]
  }
  lifecycle {
    postcondition {
      condition     = self.architecture == "x86_64"
      error_message = "The AMI must be x86_64."
    }
  }
}

resource "aws_instance" "app" {
  ami           = data.aws_ami.base.id
  instance_type = var.instance_type

  lifecycle {
    precondition {
      condition     = lookup(data.aws_ami.base.tags, "Approved", "false") == "true"
      error_message = "Only approved AMIs may be used."
    }
  }
}

check "health" {
  data "http" "site" {
    url = "https://${aws_lb.main.dns_name}/health"
  }
  assert {
    condition     = data.http.site.status_code == 200
    error_message = "The load balancer health endpoint is not returning 200."
  }
}

Points to remember:

  • A validation may reference only the variable itself in older versions; since Terraform 1.9 it can also reference other variables, data sources and locals.
  • Use self inside a postcondition to refer to the object just created.
  • A check block reports failed assertions as warnings. Its scoped data source is visible only inside that check. Standalone CLI operations evaluate checks during their runs; continuous health assessment requires an appropriate HCP Terraform feature or a scheduled workflow.
  • Preconditions guard inputs and assumptions; postconditions guard results and guarantees.

Example. A platform team publishes a module that requires a subnet in a private range. A precondition on the resource checks startswith(var.cidr, "10.") and gives an error message pointing to the internal addressing standard. When the CIDR is known during planning, users see the error before deployment. This is a company policy for 10.0.0.0/8, not a complete test for every RFC 1918 range or for CIDR syntax.

4h. Sensitive data and secrets

The sensitive flag

Marking a variable or output sensitive = true makes Terraform redact it as (sensitive value) in CLI output. Values derived from a sensitive value are also treated as sensitive. This is a display protection only: the value is still written in plain text to state and plan files. Therefore:

  • Store state in a backend with encryption and tight access control.
  • Treat plan files as secret.
  • Never commit .tfvars files that contain secrets.

Ephemeral values and write-only arguments

Recent Terraform versions add a stronger tool for secrets that should not be stored at all.

  • Ephemeral variables and child-module outputs (ephemeral = true) omit those values from plan and state. Root-module outputs cannot be ephemeral, and ephemeral values can flow only into supported ephemeral contexts, such as provider configuration or write-only arguments.
  • Ephemeral resources (ephemeral blocks) fetch temporary data, such as a password from a secrets manager, for the length of the run.
  • Write-only arguments are resource arguments (usually with names ending in _wo) that Terraform sends to the provider but never records in plan or state.
HCL
variable "db_password" {
  type      = string
  sensitive = true
  ephemeral = true
}

resource "aws_db_instance" "main" {
  # ...
  password_wo         = var.db_password
  password_wo_version = 1
}

This password fragment needs an AWS provider release with these fields; AWS provider 5.100.0 documents both password_wo and password_wo_version. Declare suitable provider requirements and retain the lock file. Combine sensitive with ephemeral when a value should also be redacted; omission from state alone is not a general guarantee against logs or other consumers exposing it.

A write-only argument cannot be compared with previous values, because nothing is stored. Providers therefore offer a companion version argument (here password_wo_version); incrementing it signals Terraform to send the new value. Only providers that implement write-only arguments offer them, and the exam expects you to know the concept and its purpose: secrets that never reach state.

Secrets management with Vault

A secrets manager centralises storage and delivery; variables can still carry the fetched value. What matters for persistence is whether the whole path uses ephemeral values and write-only consumers. HashiCorp Vault is the product the exam names. An ordinary Vault data source can persist its secret result in state. A supported ephemeral Vault resource avoids persisting that result, but must feed an ephemeral-compatible consumer. This example assumes a Vault provider v5 release that supports vault_kv_secret_v2 and an existing KV v2 mount named kv:

HCL
provider "vault" {
  address = "https://vault.example.com:8200"
}

ephemeral "vault_kv_secret_v2" "db" {
  mount = "kv"
  name  = "prod/database"
}

The secret can be read as ephemeral.vault_kv_secret_v2.db.data["password"] and passed to a compatible write-only argument. Declare hashicorp/vault as the source with a compatible v5 version constraint, then review and commit the lock file. The optional mount_id argument is useful when mount creation must defer the read to apply; this example assumes the mount already exists.

Advantages: secrets are centrally stored, rotated and audited; short-lived dynamic credentials can be generated per run; and engineers do not handle long-lived passwords. Credentials for the Vault provider itself should come from environment variables or workload identity, not from the code. In HCP Terraform, dynamic provider credentials with OIDC and Vault-backed credentials remove static cloud keys from workspaces.

Where secrets may and may not appear

Place Safe for secrets?
Configuration files in Git No
terraform.tfvars committed to Git No
Environment variables (TF_VAR_...) or a secrets manager Better
State and plan files Contain sensitive values in clear; protect with encryption and access control
Ephemeral values and write-only arguments Not stored in state or plan

Common pitfalls and exam tips

  • Pitfall: thinking sensitive = true encrypts state. It does not.
  • Pitfall: adding depends_on everywhere. It hides the real data flow and widens plans.
  • Pitfall: expecting a failing check block to stop an apply. It only warns.
  • Pitfall: using prevent_destroy as the only protection for a database. Deleting the block removes the protection.
  • Pitfall: count.index or list positions as identifiers in for expressions, causing churn when lists change.
  • Exam tip: HCL cannot declare function bodies, but Terraform supports built-in and provider-defined functions.
  • Exam tip: terraform console evaluates expressions, handy for testing cidrsubnet or merge.
  • Exam tip: custom conditions run as soon as their inputs are known. A postcondition on a new resource may run during apply; known conditions can be evaluated during planning.
  • Exam tip: use create_before_destroy to order replacement creation first; availability still depends on capacity, names and rollout design. Use ignore_changes when another system edits an attribute, prevent_destroy to block deletion.
  • Exam tip: Terraform 1.12 is the version tested; ephemeral values and write-only arguments were added in 1.10 and 1.11.

For state protection beyond display redaction, review Terraform providers and state fundamentals.

Mini case study

Situation. A fintech company deploys an API behind a load balancer. Requirements: (1) the environment must be dev, staging or prod; (2) the replacement launch template must exist before the old template is removed; application availability also requires a rolling instance rollout; (3) a compliance scanner adds a LastScan tag to instances and Terraform should not remove it; (4) the database password must never appear in state; (5) the team wants a warning, not a failure, if the health endpoint is down after an apply.

Proposed solution.

  1. A validation block on var.environment with contains([...], var.environment).
  2. lifecycle { create_before_destroy = true } on the launch template.
  3. lifecycle { ignore_changes = [tags["LastScan"]] } on the instances.
  4. An ephemeral variable for the password and a write-only password argument with a version attribute on the database resource, with the value fetched from Vault.
  5. A check block with a scoped http data source and an assert on the status code.

Commentary. Each requirement maps to one tool: validation for input, lifecycle for replacement behaviour and externally managed attributes, ephemeral and write-only features for secrets, and a check block for a non-blocking assertion about the running system. Using precondition for requirement 5 would be wrong, because it would stop the run, and sensitive = true for requirement 4 would still leave the password in state.

Key terms

  • Expression: a piece of code that yields a value.
  • Splat: [*] shorthand that collects attributes.
  • Dynamic block: generates repeated nested blocks.
  • Explicit dependency: declared with depends_on.
  • Lifecycle: meta-argument that adjusts create, update and destroy behaviour.
  • Check block: non-blocking assertion.
  • Ephemeral value: exists during a run only; not stored.
  • Write-only argument: sent to the provider, never stored.

Summary checklist

  • I can write conditionals, for expressions and splats.
  • I distinguish built-in and provider-defined functions from unsupported HCL function declarations.
  • I use depends_on only when no reference exists.
  • I can name four lifecycle arguments and their effects.
  • I can compare validation, precondition, postcondition and check.
  • I know sensitive only hides display and state still contains the value.
  • I can explain ephemeral values, write-only arguments and Vault.

Official sources