Cheat Sheet 4: Terraform Configuration
resource vs. data
resource= Terraform owns the object (create, update, delete).data= read-only, referenced asdata.T.N.- Data sources are read during plan, or deferred to apply (
<=) if their inputs are unknown ordepends_onpoints at pending changes. Results are stored in state. terraform_datais built in and needs no provider. It hasinput/outputandtriggers_replace, and replacesnull_resource.
References: T.N.attr · data.T.N.attr · T.N[0] (count) · T.N["k"] (for_each) · T.N[*].attr (lists and count only) · var.x · local.x · module.m.out · ephemeral.T.N.attr · path.module · terraform.workspace · self (postconditions and provisioners)
Variables: type, default (none → required), description, sensitive, nullable, ephemeral, validation
Precedence (later wins): default < TF_VAR_ < terraform.tfvars < terraform.tfvars.json < *.auto.tfvars (lexical order) < -var/-var-file (last one wins). Other .tfvars names need -var-file.
Outputs: value, description, sensitive (required if derived from a sensitive value), depends_on, precondition. terraform output alone shows <sensitive>; output NAME, -json and -raw show sensitive values in plaintext.
Types: list (ordered) · set (unique, no index) · map (same value type) · object (per-attribute types; optional(T, default)) · tuple (per-position types) · any. tuple→list, object→map, list→set (dedups, loses order).
Expressions
[for x in L : f(x) if c]·{for k, v in M : k => v}·...groups duplicate keyscond ? a : b(types must match) ·L[*].attrdynamic "blk" { for_each = …; content { … blk.key / blk.value } }locals {}→local.x. No user-defined functions. Provider functions:provider::p::f().terraform consoleevaluates against config and state, and holds a state lock.- Key functions:
lookup·merge·concat·flatten·toset·try·can·coalesce·format·join·split·cidrsubnet·jsonencode·templatefile·sensitive·nonsensitive
Meta-arguments
count(index; shifts on removal) vs.for_each(map or set of strings; keys known at plan). Never both on one block.depends_on = [res, module.m]: whole objects, static, last resort. On data sources it defers reads.provider = aws.aliaslifecycle(literal values only):create_before_destroy(+/-)prevent_destroy(errors on destroy; useless once the block is deleted)ignore_changes = [attr]orallreplace_triggered_by = [managed resource or attribute](useterraform_datafor variables)
Custom conditions
| Mechanism | Location | Failure |
|---|---|---|
validation |
variable (can reference other variables since 1.9) | Error |
precondition |
resource, data, or output lifecycle |
Error, before evaluation |
postcondition |
resource or data lifecycle, uses self |
Error, after evaluation |
check + assert (+ scoped data) |
top level | Warning only, every plan and apply |
Sensitive data
sensitive= hidden from display, still in state and plan.ephemeral(1.10+): variables, outputs,ephemeralresources. Never stored. Allowed only in ephemeral contexts, provider blocks, provisioner and connection blocks, and write-only arguments. Must be supplied again when applying a saved plan.- Write-only (1.11+):
*_woarguments plus*_wo_version. Stored as null. Requires provider support. - Classic Vault data sources store the secret in state. Ephemeral Vault resources don't.
- Use dynamic provider credentials (OIDC), never commit secrets, encrypt and restrict state, and treat plan files as sensitive.
Module 4 self-check
Module 4 self-check.
- Coverage: 4a (lesson 4.1, Q1), 4b (lesson 4.2, Labs 4A and 4B, Q2), 4c (lesson 4.3, Lab 4A, Q3–Q4), 4d (lesson 4.4, Q5), 4e (lesson 4.5, Lab 4A console drills, Q6), 4f (lesson 4.6, Lab 4B, Q7–Q8), 4g (lesson 4.7, Lab 4C, Q9), and 4h (lesson 4.8, Lab 4D, Q10).
- HCL: all lab files use documented arguments with
fmt-style alignment.strcontains(1.5+), cross-variable validation (1.9+),ephemeral(1.10+), and write-only arguments (1.11+) are all available in 1.12. - Flagged uncertainty: the Vault provider version range for ephemeral and write-only support is checked inside the lab (step 2) rather than asserted. Expected console outputs are described rather than reproduced character for character, because console formatting spans multiple lines.
- Answers: rechecked against documented behavior for precedence,
for_eachinput types,checkwarnings, sensitive output errors, and ephemeral contexts.