Safer configuration alternatives
Choose the tool by ownership
A reliable deployment separates infrastructure lifecycle, application artifacts and ongoing configuration. For example, a small catalog service requires compute capacity, an application release and runtime settings. Terraform can own the compute and its declared metadata; a build pipeline can own the application image; a configuration manager can own runtime settings. Assign one owner for each desired property and define verification and rollback responsibilities.
Prefer an image for repeatable software
Bake the application and its dependencies into a versioned machine image using an image builder such as Packer. Terraform then selects an approved image identifier. This flow is illustrative, not a cloud deployment.
# Architecture pseudocode, not a provider schema.Build pipeline: test application -> bake image -> approve image IDTerraform: select approved image ID -> plan compute change -> applyOperations: verify service health -> monitor -> roll back if neededThe same reviewed artifact can be tested before promotion. Replacement may still cause interruption, and image updates need patching, rollout and rollback plans. Terraform does not guarantee application readiness by selecting an image.
Use boot-time configuration for small instance-specific settings
On a supported cloud-init image, initialization data can supply settings during instance startup. This is a static cloud-init fragment, not a Terraform resource and not an instruction to run it on a host.
#cloud-configwrite_files: - path: /etc/catalog-lab.conf permissions: '0644' content: | service=catalog replicas=2A real provider may accept this document through its own initialization argument; check encoding, size limits, update and replacement behavior. Boot data may appear in state or instance metadata, so use only non-secret settings. Boot-time success and later configuration drift require separate checks. For ongoing updates, choose a reviewed configuration manager or deployment controller.
Generate data without a shell
When the task is simply formatting a settings object, use Terraform expressions directly.
locals { settings = { service = "catalog", replicas = 2 }}output "settings_json" { value = jsonencode(local.settings)}The expected decoded JSON object contains service catalog and replicas 2. Compare the parsed structure rather than relying on object-key ordering. The output is stored in state; avoid secrets. A native provider resource may handle file creation when a file truly must exist.
# Static illustration; requires a reviewed hashicorp/local provider.resource "local_file" "settings" { filename = "${path.root}/catalog-lab.json" content = jsonencode({ service = "catalog", replicas = 2 }) file_permission = "0600"}This example is not executable in the supplied environment. It would create or overwrite a lab-owned file on the runner; ensure the directory is disposable and the provider approved. A missing file on another runner can cause recreation. Restrictive permissions are not a guarantee of state confidentiality.
External data is still executable code
The external data source belongs to a separate provider. It starts a program to produce data rather than managing the program as an infrastructure resource.
# Static illustration; no helper is supplied or executed.data "external" "catalog" { program = ["./reviewed-read-only-helper"] query = { release = "v2" }}The helper would accept JSON on stdin and emit a JSON object with string values on stdout, e.g. the request and response below.
{"release":"v2"}{"image_id":"fixture-image-v2"}These are two separate illustrative messages. Do not make the helper mutate infrastructure. It can be invoked during reads or refresh; it must not depend on hidden machine state. Program arguments are a list, not a shell command string, but the executable itself remains a trust boundary. Prefer a native provider data source or pure expression where possible. No provider or helper is bundled.
Build an evidence-based decision
A versioned application image fits stable dependencies; boot configuration fits small instance-specific initialization; a native resource fits managed API objects; a configuration manager fits continuing host changes; an expression fits pure transformations. When none fits, a tightly scoped, reviewed hook may be justified. Document execution identity, inputs, repeat behavior, failure recovery, verification and an exit plan. Never treat on_failure = continue as a general reliability solution. In the next chapter, build on pure expressions with advanced functions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Safer configuration alternatives — knowledge check
Review execution boundaries and configuration alternatives.
Which tool is a good fit for a tested versioned machine image?
Which choice formats an object as JSON without a command?
What can happen to local_file on a different runner?
How should a helper used as an external data source behave?
Which mechanism fits ongoing operating system configuration?