Provisioners and why to avoid them
local-exec, remote-exec and file run scripts during apply. Why Terraform cannot model them, how failure taints a resource, and what to use instead.
Provisioners and why to avoid them
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A provisioner runs a script or copies a file when a resource is created or destroyed. The `file`, `local-exec` and `remote-exec` provisioners are built in.
- Terraform cannot model what a script did. Nothing in state records which packages were installed or which file was edited, so plan cannot detect or repair a mistake in them.
- If a creation-time provisioner fails, Terraform marks the resource tainted, and the next apply destroys and recreates it. The default is `on_failure = fail`, and `continue` lets the apply carry on.
- A destroy-time provisioner only runs while its block is in the configuration. Delete the resource block and the provisioner goes with it. `create_before_destroy` also stops it running.
- Prefer machine images, `user_data` and cloud-init, configuration management or a provider feature. HashiCorp's docs say to exhaust the alternatives first.
The script that got copy-pasted
A new instance has to run something once it exists: install a package, drop a config file, register with a monitoring server. Terraform has a block for exactly that. Someone discovers it, adds a remote-exec with six lines of shell, and it works the first time. A year later nobody can say what the server looks like, because the answer is in a script run once.
What provisioners are
A provisioner is a block inside a resource that runs when the resource is created or destroyed. Terraform has three built in. file copies a file or directory to the new machine. remote-exec runs commands on it, over SSH or WinRM, set up with a connection block. local-exec runs a command on the machine where Terraform itself is running. This is written from the provisioner docs, and I did not run it, as there is no Terraform on this machine:
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.small"
connection {
type = "ssh"
user = "ubuntu"
host = self.public_ip
private_key = file(var.key_path)
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install -y nginx",
]
}
}
self is how a provisioner refers to its parent resource, since it cannot use the resource’s own name.
Why the docs say to avoid them
The Terraform docs open the topic with a warning. They say Terraform is designed for immutable infrastructure operations, and strongly recommend purpose-built solutions for post-apply work. They tell you to exhaust the alternatives first, giving two reasons. Terraform cannot predictably model what a provisioner does, and most provisioners need network access to your servers and credentials to configure software on them.
The first reason is the deep one. State holds the attributes a provider reports for a resource. A script’s effects are not among them. I made that concrete with a small script that plays a creation-time provisioner, running a real shell script in a scratch directory where the second step fails:
provisioner exit code: 1 -> resource tainted: True
server state on disk: ['app.conf'] (half configured: app.conf exists, ready does not)
a plan has no way to see this: nothing in state describes app.conf
The “tainted” label is the rule from the docs, which I did not exercise in Terraform. Step one left a file, step two failed, and nothing undoes step one. A plan cannot detect that, and cannot detect the same kind of change made by hand later. The configuration stops being a description of the machine. It becomes a recipe that ran at some point.
Failure, taint and destroy
The docs describe three behaviours worth knowing.
- A failing creation-time provisioner taints the resource. Terraform does this because a failed provisioner can leave a semi-configured resource, and since it cannot model what happened, recreating is the only safe option. The next apply destroys and recreates it. By default the failure fails the apply (
on_failure = fail). Settingon_failure = continueignores the error and carries on. - Destroy-time provisioners are fragile. They run before the resource is destroyed, only with
when = destroy, and if they fail Terraform reruns them on the next apply, so they must be safe to run repeatedly. They only run if the block is still in the configuration. Delete the resource block and its provisioner goes with it. A resource that is already tainted skips them, and so does one withcreate_before_destroy. - Provisioners and the lifecycle interact. The create_before_destroy lesson mentions the same destroy-time limit.
What to use instead
The docs point to purpose-built options. Pick by what you are trying to do.
- Instance first-boot setup. Pass a script through
user_data, usually as cloud-init, so the machine configures itself when it starts. Terraform only sets the data. It does not log in. - Software baked ahead of time. Build a machine image with Packer, then have Terraform launch it. The devops lesson on immutable infrastructure makes the case. Replacing a server with a new image is cleaner than patching one in place.
- Ongoing configuration. Use a configuration management tool such as Ansible, covered in the devops lesson, and keep Terraform to provisioning the machines.
- Something the provider does not expose. Look for a provider resource or data source first. The docs suggest opening a feature request with the provider if no mechanism exists.
When a local-exec really is the right tool, such as kicking off a CLI that has no provider, tie it to something that triggers it with terraform_data and triggers_replace, so it runs when its inputs change, as the replace lesson shows. Keep the command safe to run twice.
Secrets in provisioners
A connection block usually needs an SSH key or password. The docs say Terraform 1.10 or later is required to use ephemeral values in provisioner and connection blocks, which keeps such a value out of state and plan files. The ephemeral values lesson at the end of this group covers how.
About the video
The embed is “Terraform Provisioners (with demo) - local vs remote vs file”, episode 19 of Tech Tutorials with Piyush’s Terraform series, published December 2025, 24 minutes. Its description says it covers the three built-in provisioners and the “last resort” view that HashiCorp takes, and has no chapters. Piyush’s series mixes Azure and AWS examples. I did not find a better match: most provisioner videos are older or very small, and few say plainly to avoid them. The Journey of DevOps video in deeper is a 2023 talk on “when NOT to use them”. English was judged from the title, description and the English auto-caption track.
Where this fits
Provisioners run code you wrote outside Terraform’s model. The next lesson is about the reverse: checks that Terraform runs on its own model, to catch bad assumptions before an apply: preconditions, postconditions and check blocks.
Check yourself
A `remote-exec` provisioner fails on line 3 of five. What state is the server left in, and what does Terraform do?
Half configured: lines 1 and 2 ran and nothing undoes them. Terraform marks the resource tainted, so the next apply destroys and recreates it rather than trying to continue.
Why does plan show nothing about a provisioner's effects?
State stores the attributes a provider reports for the resource. A provisioner's changes are outside that model, so Terraform has nothing to compare and cannot tell if the script ran or what it left behind.
What are two alternatives to a `remote-exec` that installs and starts nginx?
Bake it into a machine image, or pass a cloud-init script through `user_data` so the instance configures itself on first boot. A configuration-management tool such as Ansible also works.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.