Encoding, time and filesystem boundaries
Separate values from their representation
Encoding, filesystem reads, time and sensitivity functions process configuration data rather than provision infrastructure. A JSON object decoded by jsondecode becomes an object value; an array becomes a tuple. jsonencode produces text. A round trip may not retain Terraform-specific type distinctions such as list versus set. Compare decoded values rather than whitespace or key order.
jsondecode("{\"replicas\":2,\"active\":true}")jsonencode({ replicas = 2, active = true })sha256("catalog-v1")The first expression yields an object with a number and a boolean. The second produces a JSON string containing the same values. sha256 produces a 64-character lowercase hex digest of the UTF-8 string; it is not encryption or evidence of source authenticity. Use reviewed cryptography for security, not a bare digest of a password.
Read only pre-existing fixture files
The supplied lab contains two synthetic JSON files and a template. fileset returns relative regular-file names as a set; sort them if you need an ordered display. file interprets bytes as UTF-8, while filebase64 encodes raw bytes. templatefile renders an existing template with an explicit object of variables. These functions are not resource dependency nodes and cannot reliably read a file created later in the same apply.
locals { names = sort(tolist(fileset(path.module, "inputs/*.json"))) records = { for n in local.names : n => jsondecode(file("${path.module}/${n}")) } encoded = filebase64("${path.module}/inputs/catalog.json") message = templatefile("${path.module}/label.tftpl", { service = "catalog" })}output "records" { value = local.records}output "message" { value = local.message}Predicted records are keyed by inputs/catalog.json and inputs/search.json; message is "service=catalog\n" (one trailing newline). Inspect the supplied inputs before use. The path.module prefix selects the module directory, but is not a security sandbox: do not accept arbitrary paths, traversal or symlinks to external data. Base64 is reversible, not protection. These expressions may place read data into state if used in persisted arguments or outputs.
Make time-dependent decisions explicit
Use fixed release timestamps as inputs for reproducible calculations. timeadd takes a timestamp and duration string. timestamp is evaluated at apply rather than known during a normal plan, while plantimestamp represents the time of a plan and is suitable for plan-time checks. Neither is a stable resource identifier across separate runs.
timeadd("2030-01-01T10:00:00Z", "90m")# Variable clock values; not deterministic expected output:timestamp()plantimestamp()The fixed calculation produces 2030-01-01T11:30:00Z. The two clock functions have no fixed expected result. Avoid using either as a replacement trigger or routine resource name. For a validity policy, use a clearly defined reference time and compare the actual expiry separately.
Treat sensitive markings and ephemeral values differently
sensitive marks a value for redaction in ordinary displays; it does not encrypt state. issensitive tests that marking, not whether the content contains secrets. nonsensitive removes the marking and must only be used where a value has been reviewed as genuinely public. Do not use it to inspect hidden values.
issensitive(sensitive("synthetic-label"))issensitive("public-label")Expected booleans are true and false. Ephemeral values are restricted to permitted contexts and omitted from plan and state. ephemeralasnull replaces ephemeral values with null, including ephemeral parts of a collection; it is not a way to persist them.
variable "temporary_label" { type = string ephemeral = true default = "synthetic"}output "persistent_label" { value = ephemeralasnull(var.temporary_label)}The predicted expression value is null; Terraform may omit a null root output from displayed outputs. This example uses a synthetic label only. Check minimum versions and permitted contexts before real use.
Know the function namespace and execution boundary
Terraform configuration does not support defining arbitrary user functions. A provider can expose functions under its local name; the built-in terraform provider has its own function namespace.
# Static syntax example; built-in terraform provider function.provider::terraform::encode_tfvars({ replicas = 2 })The result is a Terraform variable-definition string describing replicas=2, not a resource or data source. Do not assume a provider function is available without verifying its version and initialization requirements. A data source reads provider-managed information and may call an API; a provider action performs a provider-defined operation. These are not interchangeable with pure expression transforms. Provider code is executable and requires provenance review.
Practice with reproducible data
Use the supplied file inventory, fixed timestamp and public labels to calculate the expected results without Terraform. Compare structured data, not serialized object order. Keep any real credentials, plans, state and arbitrary filesystem paths out of the lab. Exact-release inventory and runtime validation remain outstanding.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Encoding, time and filesystem boundaries — knowledge check
Review advanced function behavior and data boundaries.
Which reads raw file bytes as Base64?
When must a file for file() exist?
Which is a fixed reproducible time input?
What does sensitive() provide?
What does ephemeralasnull do to an ephemeral value?