Provisioners and external execution
What happens outside Terraform?
A provisioner is a hook that delegates work to an execution environment. Imagine Terraform creates a server, but the application still needs configuration. A hook could perform that configuration, but Terraform cannot generally infer the script’s side effects, undo them, or detect all later changes. Prefer a managed resource or a dedicated deployment process. This chapter uses only static examples; no commands or remote connections are executed. The existing Terraform 1.14.8 teaching baseline is provisional.
Local execution is relative to the runner
The local-exec hook invokes a command where Terraform runs, which may be a CI worker or a remote execution agent rather than your laptop. The following independent snippet illustrates a fixed POSIX-shell command and a synthetic environment value.
# Static example only; POSIX shell assumed.resource "terraform_data" "notice" { input = "catalog-v1"
provisioner "local-exec" { command = "printf '%s\\n' \"$LAB_LABEL\"" environment = { LAB_LABEL = self.input } }}The predicted message is catalog-v1 followed by a newline. Passing a value through the environment and quoting its expansion keeps it separate from shell syntax in this example. Avoid concatenating untrusted values into commands, allowing untrusted executable paths, or passing credentials in arguments. Environment variables are not a secret store; subprocesses and logs may expose them. An interpreter and printf must be available in the approved environment.
Remote hooks cross another trust boundary
A file hook copies material to a destination on a remote resource; remote-exec runs commands there. Connection settings describe the transport and authentication. SSH and WinRM have distinct requirements. This diagram deliberately omits executable remote configuration.
# Pseudocode, not valid Terraform configuration.resource <reviewed compute resource> { connection { <approved destination and verified host identity> } provisioner "file" { <reviewed source and destination> } provisioner "remote-exec" { <reviewed bootstrap operation> }}Review destination identity, certificate or host-key verification, least privilege, transfer paths, network policy and credential lifetime. Do not weaken verification to make bootstrap work. A successful transfer does not prove that the application is healthy. Use a dedicated test of service behavior.
Understand timing and failure
Creation hooks run on creation, not on every normal update. Multiple applicable hooks run in declaration order. A failed creation hook normally taints its parent and fails the apply; ignoring the error with on_failure = continue can conceal an incomplete setup. Destroy hooks use when = destroy and run before removal; they may be retried after failure. They are skipped for tainted resources and when create_before_destroy is enabled. Removing the whole declaration removes its hooks too. Design cleanup as a separate reliable operation rather than trusting a hook to run in every circumstance.
# Synthetic event timeline, not CLI output.create object -> run setup -> setup fails -> apply failsnext reviewed plan -> replacement of tainted objectsuccessful create -> ordinary update -> no creation hookThis is a conceptual sequence; no actual plan is represented.
Use explicit replacement triggers
The built-in terraform_data resource gives a lifecycle to stored values without a cloud object. Older configurations may use the external hashicorp/null provider and null_resource with triggers. For a new local fixture, use terraform_data.
resource "terraform_data" "revision" { input = "catalog" triggers_replace = "v2"}Changing input alone normally updates the record; changing triggers_replace forces replacement. If a creation hook is attached, replacement can rerun it. Never tie triggers to a constantly changing timestamp just to force routine execution. Prefer idempotent and observable operations with explicit retry and recovery policy.
Review external code before plan
A plan is not a security sandbox: provider code and data sources can execute while values are read. Unreviewed modules can introduce executable hooks. Inspect dependencies, executable provenance, runner permissions, configuration and expected effects before initialization or planning. If a hook fails, inspect the actual system and state rather than retrying blindly. Complete the offline review worksheet before considering any authorized lab.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Provisioners and external execution — knowledge check
Review execution boundaries and configuration alternatives.
Where does local-exec run?
What normally happens when a creation hook fails?
Which situation skips a destroy hook?
How should a synthetic value reach a fixed shell command?
What does triggers_replace do when changed?