A new feature adds a queue and its access policy to a shared configuration that serves dev, staging and prod. The team wants these resources created only in dev until the feature is approved, without maintaining separate copies of the code. Which approach fits?
Select an answer to reveal the explanation.
Short Explanation
Think of count as a dimmer switch that only goes to zero or one. Set it from a feature flag and each environment decides whether the resource exists at all. Flip the flag in dev first, and staging and prod just plan zero instances until you're ready.
Full Explanation
Conditional creation in Terraform uses count or for_each with an expression driven by an input. count = var.enable_feature ? 1 : 0 declares one instance when the flag is true and none when it is false, so the same code serves every environment and each environment's variables decide whether the feature exists. Turning the flag on in dev creates the resources there, while staging and prod plan nothing for them; promotion is simply setting the flag in the next environment. Once a resource uses count, references to it need an index, such as [0], or a splat or one() for optional use. depends_on only orders operations and must reference resources or modules, not variables; it cannot skip anything. Leaving the resources unconditional means every environment's normal plan would create them, and using -target to avoid that misuses an exceptional recovery tool and leaves other environments perpetually showing pending creates. check blocks run assertions that produce warnings; they do not control whether resources are created. Exam caveat: for_each over an empty or one-element map achieves the same pattern and gives instances stable keys. Operational check: run plans in dev and prod and confirm only dev shows the queue and policy being created.