Terraform Provisioners Explained
Learn what Terraform provisioners are, how local-exec and remote-exec work, when they are useful, and why they should be used carefully.
What is a Terraform provisioner?
A provisioner runs scripts or commands during resource creation or destruction. Provisioners can be useful, but they should not become the main way to configure infrastructure.
local-exec example
resource "null_resource" "example" { provisioner "local-exec" { command = "echo Resource created" } }
remote-exec example
provisioner "remote-exec" { inline = [ "sudo apt update", "sudo apt install -y nginx" ] }
When provisioners are used
- Bootstrap a VM after creation.
- Run a one-time local command.
- Call external tooling carefully.
- Temporary migration workflows.
Common mistakes
- Using provisioners for all server configuration.
- Making Terraform depend on fragile SSH connections.
- Storing secrets in inline commands.
- Not handling retries or partial failures.
Best practices
- Use provisioners only when there is no better option.
- Keep commands idempotent where possible.
- Do not print secrets.
- Prefer immutable images or cloud-init for VM setup.
Related guides
- Terraform Learning Path
- Terraform Commands Explained
Official references
- Terraform provisioners documentation
Prefer cloud-init or image builds over provisioners
This example shows a harmless native Terraform data resource instead of running a provisioner. Provisioners have limited lifecycle guarantees and can create hard-to-debug side effects; for guest software prefer cloud-init, prebuilt images or configuration management tools.
resource "terraform_data" "demo" {
input = "configuration-ready"
}Related Melochip article: https://melochip.com/posts/terraform-best-practices-devops-teams. Examples are illustrative and have not been independently executed.
Practical guidance and verification
Why provisioners are fragile
Provisioners invoke commands or copy files outside Terraform's resource model. They can fail halfway through, require elevated remote access and create configuration drift. Prefer cloud-init, native provider settings, immutable images, or configuration management for repeatable provisioning. A provider resource becoming available does not prove that provisioning scripts finished correctly.
Safe use when unavoidable
Keep each command narrow, idempotent and documented. Distinguish local-exec from remote-exec and protect credentials from command arguments, build logs and shell history. Set appropriate timeouts and error handling, and test running the command more than once. Record whether the resource will be replaced or the command rerun on a later change.
Verify after execution
Independently verify the resulting service status, network reachability and expected files. Do not rely on a zero exit code alone. Define cleanup and rollback procedures for partial failures and test them before production deployment. Consult https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes monitoring and logs with kubectl logs, events, metrics-server, top commands, Prometheus, Graf...
6 min read · Updated Nov 16, 2026
Learn Kubernetes Ingress with controllers, IngressClass, host and path routing, TLS, DNS, annotations, redirec...
8 min read · Updated Nov 16, 2026
Learn Kubernetes ConfigMaps and Secrets with YAML, environment variables, file mounts, update behavior, immuta...
8 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.