Terraform Infrastructure as CodeTerraformTerraform Provisioners

Article & Tutorial

Terraform Provisioners Explained: local-exec, remote-exec, and When to Avoid Them

Learn Terraform provisioners, local-exec, remote-exec, file provisioner, common use cases, risks, mistakes, and safer alternatives.

Automation execution with caution barriers and safer declarative alternatives
Automation execution with caution barriers and safer declarative alternatives
Contents

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.

example
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.

Read related Terraform guide

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.

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.