HashiCorp Certified: Terraform Associate (003 / 004) — HCL, State Management, Modules & Governance
High-yield Terraform Associate (003/004) flashcards covering the 57-question exam blueprint, core CLI workflow, provider lock files, remote state backends & locking, state CLI vs. moved/import/removed blocks, count vs. for_each, dynamic blocks, lifecycle meta-arguments, variable precedence, Vault/sensitive data, Module Registry, and HCP Terraform / Sentinel.
- Category
- Multi-Cloud & DevOps Architecture — Microsoft Azure (AZ-104 & AZ-305), Google Cloud (PCA & PDE), Kubernetes (CKA / CKAD) & HashiCorp Terraform Associate
- Level
- advanced
- Languages
- en → en
- Cards
- 32
- Markdown
- HashiCorp Certified: Terraform Associate (003 / 004) — HCL, State Management, Modules & Governance (.md)
Flashcards (32)
| # | Front (Term / Question) | Back (Definition / Answer) | Example | Multiple-Choice Options |
|---|---|---|---|---|
| 1 | In HCP Terraform, how do Projects, Variable Sets, and Run Triggers organize multi-workspace infrastructure at scale? | Projects group Workspaces for RBAC; Variable Sets share variables/credentials across Projects or Workspaces; Run Triggers queue downstream Workspace plans when upstream Workspaces apply. | Instead of duplicating AWS OIDC role variables across 40 microservices workspaces, an administrator attaches one `AWS-Prod-Auth` Variable Set to the `Production` Project and links `network-prod` to downstream workspaces via Run Triggers. |
|
| 2 | Compare `terraform refresh` (now deprecated), `terraform apply -refresh-only`, and `terraform plan -refresh=false`. | `terraform apply -refresh-only` previews out-of-band cloud drift and prompts before updating `terraform.tfstate` (replacing unsafe `terraform refresh`), while `-refresh=false` skips API refresh queries. | If someone manually modifies a tag in the Azure Portal, running `terraform plan -refresh-only` displays the exact state drift without modifying any cloud infrastructure. |
|
| 3 | Does setting `sensitive = true` on a Terraform `variable` or `output` encrypt or hide the secret value inside `terraform.tfstate`? | NO! Setting `sensitive = true` only redacts the value from CLI terminal output and logs; the plaintext value still persists in `terraform.tfstate`! | Note that in Terraform 1.10+ (Exam 004), `ephemeral = true` variables/resources (`ephemeral "vault_kv_secret_v2"`) and provider Write-Only arguments (`password_wo`) DO omit secrets from being stored in `terraform.tfstate` or plan files. |
|
| 4 | Compare the `count` and `for_each` meta-arguments in Terraform: what collection types does each accept, and why is `for_each` generally safer when removing items from the middle of a list? | `count` takes an integer and indexes by position (`[0]`); `for_each` takes a `map` or `set(string)` and indexes by key (`["prod"]`), preventing cascading re-creations. | If 3 subnets are created with `count = length(var.subnets)` and `var.subnets[0]` is removed, indices `[1]` and `[2]` shift down, forcing Terraform to recreate subsequent subnets; `for_each = toset(var.subnets)` deletes only the removed key. |
|
| 5 | How does the native `terraform test` framework (Terraform 1.6+, tested on Exam 004) validate modules and configurations? | `terraform test` executes `.tftest.hcl` files with `run` blocks and `assert` conditions, supporting both integration tests (`apply`) and offline unit tests (`plan` + `mock_provider`). | Using `mock_provider "aws" {}` inside `tests/unit.tftest.hcl` lets CI pipelines verify complex HCL variable transformations, module outputs, and preconditions in seconds without needing cloud credentials or provisioning real resources. |
|
| 6 | In HCP Terraform (formerly Terraform Cloud) and Sentinel / OPA Policy-as-Code, at what point in the run lifecycle are policies evaluated, and what are the three Sentinel enforcement levels? | Policies evaluate **between `plan` and `apply`**; Sentinel offers `advisory` (warn only), `soft-mandatory` (authorized override allowed), and `hard-mandatory` (cannot be overridden). | A `hard-mandatory` Sentinel policy checking that no S3 bucket has `acl == "public-read"` or that estimated monthly run cost delta is `< $500` blocks the run immediately after `plan` with zero possibility of manual override. |
|
| 7 | Which module `source` types support the `version` argument (e.g., `version = "~> 5.0"`) inside a `module` block? | The `version` argument is ONLY supported for modules fetched from a **Terraform Registry** (Public Registry or Private Registry in HCP Terraform). | Modules sourced from local paths (`./modules/vpc`), GitHub/Git URLs (`git::https://example.com/vpc.git?ref=v1.2.0`), or S3/GCS buckets do NOT accept the `version` attribute—Git sources pin versions using the `?ref=<tag/branch/sha>` query string instead! |
|
| 8 | Compare `terraform state mv` vs. declarative `moved` blocks, and `terraform state rm` vs. declarative `removed` blocks. | `moved` and `removed` blocks refactor state declaratively in HCL during `plan`/`apply`, replacing imperative `terraform state mv` and `terraform state rm` commands. | When renaming `aws_instance.app` to `aws_instance.web` inside a shared module or team GitOps pipeline, committing a `moved` block ensures every workspace updates its state automatically on the next `terraform apply` without destroying the VM. |
|
| 9 | When a `local-exec` or `remote-exec` provisioner IS used, what happens by default if the script exits with a non-zero status code, and how can you change that behavior? | By default (`on_failure = fail`), a failing creation-time provisioner fails the run and marks the resource **tainted** in state; `on_failure = continue` ignores errors. | Creation-time provisioners run during initial resource creation (`when = create`, default), whereas a provisioner with `when = destroy` runs immediately before the resource is destroyed. |
|
| 10 | How do Implicit Dependencies and Explicit Dependencies (`depends_on`) differ in Terraform's execution graph? | Terraform infers Implicit Dependencies automatically from attribute references (`vpc_id = aws_vpc.main.id`); `depends_on` is only needed for hidden side-effect dependencies without attribute references. | If an EC2 instance's bootstrap script needs an `aws_iam_role_policy_attachment` or an `aws_s3_bucket_policy` to exist before booting—even though the instance block doesn't reference that policy's ID—you add `depends_on = [aws_iam_role_policy_attachment.app]`. |
|
| 11 | How does config-driven `import` (`import { to = ... id = ... }`) in Terraform 1.5+ improve upon the legacy imperative `terraform import <address> <id>` CLI command? | Declarative `import` blocks execute inside the standard `plan`/`apply` cycle, support `for_each`, and auto-generate HCL configuration via `terraform plan -generate-config-out=generated.tf`. | Unlike legacy `terraform import` (which immediately mutates state without a plan preview and requires writing the `resource` block manually beforehand), `import` blocks preview the exact import in `terraform plan`. |
|
| 12 | In Terraform HCL expressions, what is the Splat Expression (`[*]`), how does `try()` / `can()` handle errors, and how do you test built-in functions interactively? | The Splat operator (`aws_instance.web[*].id`) extracts attributes across a list/tuple/set; `try()` and `can()` handle evaluation errors; `terraform console` opens an interactive expression REPL. | Note that the Splat operator `[*]` works on `count` resources (`aws_instance.web[*].id`) and lists, but does NOT work directly on `for_each` maps—for `for_each` maps, use `values(aws_instance.web)[*].id` or a `[for k, v in ...]` expression! |
|
| 13 | What are the six stages of the core Terraform CLI workflow, and what does each command do regarding state and cloud APIs? | `init` initializes backend/providers/modules; `fmt` formats HCL; `validate` checks syntax offline; `plan` previews diffs; `apply` executes changes; `destroy` deletes managed infrastructure. | Crucially for the exam, `terraform validate` runs entirely offline against downloaded provider schemas and does NOT access remote state or cloud APIs, whereas `terraform plan` and `terraform apply` refresh remote state by default. |
|
| 14 | What does the Dependency Lock File (`.terraform.lock.hcl`) track, and should it be committed to Version Control (Git)? | `.terraform.lock.hcl` locks the exact versions and SHA-256 hashes of **Providers** only (NOT modules!) and MUST be committed to Git. | When you change a provider version constraint in `required_providers`, running `terraform init -upgrade` updates `.terraform.lock.hcl` to the newest allowed provider release. |
|
| 15 | How does variable and resource scoping work between a Root Module and a Child Module in Terraform? | Modules are encapsulated: Child Modules only see explicitly passed input variables, and the Root Module only sees Child Module attributes exported via `output` blocks. | If child module `modules/vpc` creates `aws_vpc.main`, the root module cannot reference `module.vpc.aws_vpc.main.id` directly—`modules/vpc/outputs.tf` must declare `output "vpc_id" { value = aws_vpc.main.id }`, which the root module accesses as `module.vpc.vpc_id`. |
|
| 16 | What is the format, duration, and question type breakdown of the HashiCorp Certified: Terraform Associate (003 / 004) exam? | 57 questions in 60 minutes consisting of multiple-choice, multiple-answer, true/false, and text-match questions covering Terraform Community Edition and HCP Terraform. | Exam 004 updates the blueprint to emphasize config-driven refactoring (`import`, `moved`, and `removed` blocks), native `check` assertions, the `terraform test` framework, and ephemeral values/write-only arguments alongside core HCL and state management. |
|
| 17 | Compare CLI-managed OSS `terraform workspace` (Local/Backend Workspaces) vs. **HCP Terraform Workspaces** (formerly Terraform Cloud Workspaces). | CLI OSS workspaces are simply alternate state files sharing one working directory; HCP Terraform Workspaces are isolated environments with independent variables, state, VCS, and RBAC. | HashiCorp warns that CLI OSS workspaces alone are NOT suitable for strong security isolation between prod and dev (since they share backend credentials), whereas HCP Terraform Workspaces enforce strict per-workspace RBAC. |
|
| 18 | What are the three Execution Modes available in an HCP Terraform Workspace? | `Remote` (runs on HCP managed runners), `Local` (runs on local CLI while HCP stores/locks state), and `Agent` (runs on self-hosted private `tfc-agent` workers). | When an enterprise needs HCP Terraform to provision internal VMware vSphere or private AKS/GKE clusters behind a corporate firewall without opening inbound firewall ports, they select `Agent` execution mode with HCP Terraform Agents. |
|
| 19 | What problem do `dynamic` blocks solve in Terraform HCL, and when should you avoid overusing them? | `dynamic` blocks generate repeatable nested configuration blocks (such as `ingress` rules) inside a resource using `for_each` and a `content {}` block. | Inside an `aws_security_group`, a `dynamic "ingress" { for_each = var.ports; content { from_port = ingress.value ... } }` block generates one `ingress` block per port; however, use literal blocks when possible for readability. |
|
| 20 | Why does HashiCorp state that Terraform `provisioner` blocks (`local-exec`, `remote-exec`, `file`) should only be used as a **last resort**? | Provisioners execute imperative scripts outside Terraform's declarative graph: Terraform cannot model, preview, or roll back script changes inside `terraform plan`. | Instead of using `remote-exec` over SSH/WinRM to install packages on a new VM, HashiCorp recommends passing declarative `user_data` / `custom_data` (`cloud-init`) or pre-baking immutable machine images with **HashiCorp Packer**. |
|
| 21 | Why was `terraform taint` deprecated in Terraform 0.15.2+, and what command or meta-argument replaces it? | `terraform apply -replace="<resource_address>"` replaced `terraform taint` so replacement happens atomically inside a single planned run without mutating state beforehand. | If `terraform taint aws_instance.web` was run by mistake in legacy workflows, another teammate running `terraform apply` would unexpectedly recreate the VM; `terraform apply -replace="aws_instance.web[0]"` scopes the replacement to that exact execution plan. |
|
| 22 | Compare Input Variables (`var.<name>`), Local Values (`local.<name>`), and Output Values (`output`) in Terraform. | Input Variables (`var.x`) parameterize a module externally; Local Values (`local.x` in `locals {}`) define internal reusable expressions; Outputs (`output "x"`) export values. | Notice the singular vs. plural syntax trap tested on the exam: you declare `variable "x" {}` (singular) and reference `var.x`, whereas you declare `locals { x = ... }` (plural block!) and reference `local.x` (singular!). |
|
| 23 | How do Custom Condition Checks (`validation`, `precondition`, `postcondition`) and `check` blocks differ in Terraform? | `validation`, `precondition`, and `postcondition` are **blocking** (halt `plan`/`apply` on failure), whereas standalone `check` blocks execute non-blocking assertions that only emit warnings! | Adding a `check "health_check" { data "http" "api" { ... } assert { condition = data.http.api.status_code == 200 ... } }` block verifies that a newly deployed load balancer returns HTTP 200 after `apply` completes. |
|
| 24 | What is the exact order of precedence (from lowest to highest priority) when Terraform loads Input Variable (`variable`) values? | Lowest to highest: 1) variable `default` -> 2) `TF_VAR_<name>` -> 3) `terraform.tfvars` -> 4) `terraform.tfvars.json` -> 5) `*.auto.tfvars` -> 6) CLI `-var` / `-var-file`. | If `instance_type` is set to `t3.micro` in `TF_VAR_instance_type`, `t3.small` in `terraform.tfvars`, `t3.medium` in `prod.auto.tfvars`, and `-var="instance_type=t3.large"` on the CLI, Terraform uses `t3.large`. |
|
| 25 | Why does Terraform require a State file (`terraform.tfstate`), and why must teams use a Remote Backend with State Locking? | State maps HCL addresses to real-world cloud IDs and tracks dependencies; remote backends share encrypted state and lock it during runs. | Without state locking, two engineers running `terraform apply` concurrently could corrupt state or create duplicate resources; if a lock gets stuck after a crash, clear it cautiously with `terraform force-unlock <LOCK_ID>`. |
|
| 26 | How does integrating Terraform with HashiCorp Vault reduce secret sprawl, and what is the caveat with the standard `data "vault_generic_secret"` data source? | Vault injects short-lived dynamic credentials during runs, though standard `data` sources still write secrets into `terraform.tfstate` unless using `ephemeral` Vault resources. | Because standard Vault data sources write fetched database passwords into `terraform.tfstate`, architects restrict RBAC on the remote state bucket, enable KMS encryption, or use Terraform 1.10+ `ephemeral` blocks with write-only resource attributes. |
|
| 27 | How do you enable verbose debugging logs in Terraform CLI, and how do you save them to a file without cluttering standard output? | Set `TF_LOG` to `TRACE`, `DEBUG`, `INFO`, `WARN`, or `ERROR` (`TRACE` is most verbose) and set `TF_LOG_PATH` (e.g., `./terraform.log`) to save logs. | To debug only provider plugin API calls or only Terraform core graph evaluation, you can also set `TF_LOG_PROVIDER=TRACE` or `TF_LOG_CORE=TRACE` independently. |
|
| 28 | Explain the four `lifecycle` meta-arguments in Terraform: `create_before_destroy`, `prevent_destroy`, `ignore_changes`, and `replace_triggered_by`. | `create_before_destroy` creates replacements first; `prevent_destroy` blocks destruction plans; `ignore_changes` ignores drift on listed attributes; `replace_triggered_by` forces replacement when referenced items change. | Note an important exam nuance: `prevent_destroy = true` protects against accidental replacement or `terraform destroy` while the block remains in HCL, but removing the entire `resource` block from HCL bypasses `prevent_destroy`! |
|
| 29 | In Terraform version constraint expressions, what do the operators `=`, `!=`, `>=`, `~>` (pessimistic constraint), and `1.2.0` mean? | The pessimistic operator `~>` allows only the rightmost version component to increment: `~> 1.2` allows `>= 1.2.0, < 2.0.0`; `~> 1.2.3` allows `< 1.3.0`. | Writing `version = "~> 5.4.0"` on a provider or registry module permits patch bug-fix upgrades (`5.4.1`, `5.4.9`) while blocking minor (`5.5.0`) and major (`6.0.0`) releases. |
|
| 30 | When migrating from a local state file to a remote `backend` block (or changing backends), which `terraform init` flags are used? | Use `terraform init -migrate-state` to copy existing state to the new backend, or `terraform init -reconfigure` to ignore prior state. | After adding `backend "azurerm" { ... }` or a `cloud { ... }` block to `terraform {}`, running `terraform init -migrate-state` safely uploads local `terraform.tfstate` into the remote backend. |
|
| 31 | What is the difference between a `resource` block and a `data` block (Data Source) in Terraform? | A `resource` block creates, updates, and destroys managed infrastructure; a `data` block performs a read-only query against a provider API without managing lifecycle. | Referencing `data.aws_ami.ubuntu.id` queries AWS for the latest canonical Ubuntu AMI ID during the plan phase without taking ownership of that AMI. |
|
| 32 | How do you configure multiple configurations of the same Terraform Provider (for example, deploying resources across `us-east-1` and `us-west-2` in the same root module)? | Define a default `provider` block and additional `provider` blocks with the `alias` meta-argument, then reference `provider = <provider>.<alias>` on resources. | A second `provider "aws" { alias = "west"; region = "us-west-2" }` block is referenced on a DR bucket via `provider = aws.west`, or passed to child modules via `providers`. |
|