Skip to content

Lesson 7 of 14 · 15m

Where Terraform's Job Ends

Provisioners run once at creation and fail applies on transient errors — `user_data`, pre-built images, and config management are the convergent tools past the provisioning boundary.

Terraform provisions. It calls APIs that create VMs, networks, databases — and stops when the API returns 200. What runs on the box afterward is someone else's job, and the sooner you draw that line the fewer deploys you'll spend debugging SSH timeouts inside an apply.

The temptation is provisioner blocks — remote-exec to SSH in and run a bootstrap script, local-exec to run something on the machine doing the apply, file to push a script up first. HashiCorp's own documentation calls provisioners a last resort, and the docs undersell it. Here's what actually happens:

resource "aws_instance" "app" {
  # ...
  provisioner "remote-exec" {
    inline = ["sudo apt-get update", "sudo apt-get install -y nginx"]

    connection {
      type        = "ssh"
      user        = "ubuntu"
      private_key = file("~/.ssh/deploy_key")
      host        = self.public_ip
    }
  }
}
  • It runs once, at creation. Edit the script and re-apply — nothing happens, because the resource already exists and provisioners aren't part of the diff. The box drifts from your script immediately.
  • A nonzero exit fails the apply. Apt mirror hiccup, SSH not ready yet, a transient network blip — the apply reports failure, but the instance already exists. Now Terraform owns a resource in a half-configured state and your state file knows it.
  • Credentials in the plan context. connection needs a private key or password Terraform can read — that's one more secret your plan environment has to hold, and one more place it can leak.
  • It's imperative wedged into a declarative tool. Nothing converges; nothing retries; there's no idempotency checking. You wrote a shell script that runs at a random moment in a distributed system's lifecycle.

What replaces it

The configuration boundary is real and the tools on the other side are convergent by design:

  • user_data / cloud-init. The instance gets its bootstrap script at first boot, inside the cloud's own lifecycle — retries, logging to /var/log/cloud-init-output.log, no SSH key needed by Terraform. For "install packages, write a config, start a service," this is the answer.
  • Pre-built images. Packer builds an AMI with everything baked; Terraform launches it. Boot time drops to seconds and every box is bit-identical — the provisioning-time script becomes a build-time pipeline with its own CI.
  • Configuration management. Ansible, or a pull-based agent that checks in and converges itself. Terraform's job ends at aws_instance; Ansible's begins at the SSH connection — each tool operating in its own lifecycle.
  • local-exec's narrow use. Triggering a webhook, calling an API the provider lacks, invalidating a CDN — glue that acts on outside systems, not on the resource being created.

There's a reason the wrong model is sticky: provisioners look like they close the loop. resource + remote-exec = "declare and configure in one place." But the loop they close is false — the apply reports success on a box configured by a script that will never run again and can't be re-run by re-applying. What you built is configuration drift with extra steps.

The escape hatch that exists

terraform_data (Terraform 1.4+, the modern replacement for null_resource) can host a provisioner when you genuinely need glue — triggers_replace forces it to re-run when inputs change, which is how you bolt an "on every deploy" hook onto a resource lifecycle. Use it deliberately and rarely: it's the exception that proves where the boundary is.

Field notes

  • Terraform v0.15.2 deprecated terraform taint/untaint; apply -replace= is the supported path for forcing a resource — and its provisioners — to re-run.
  • If a bootstrap fails, the instance exists and plan won't show it as broken — the provisioner's failure is invisible to the next diff. That invisibility is the real cost.
  • connection secrets are another thing that can end up in state-adjacent places — keep Terraform's credentials for provisioning only, never for configuration.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.