792 words
4 min read

null_resource vs terraform_data in Terraform: Triggers and Which to Pick

By Docker Captain · CNCF Ambassador · IBM Champion
Cover image for the post 'Leveraging null_resource in Terraform for Complex Operations'

Let’s not kid ourselves. null_resource is the duct tape of Terraform. It doesn’t provision a VM. It doesn’t configure a VPC. And it sure as hell doesn’t play nice with cloud-native best practices. But used wisely, it’s the unsung hero of CI/CD glue code and one-off automation.

Here’s the plan. We get tactical with null_resource, walk through use cases I’ve actually shipped, then contrast it with the newer, cleaner terraform_data resource. By the end you’ll know which tool to reach for without feeling dirty about it.

Terraform Resources 101 (Quick Recap)#

Terraform’s core mechanic is the resource block. It tells the provider, “Hey, make this thing exist.”

resource "azurerm_windows_function_app" "app" {
name = "example-function-app"
location = "East US"
}

That’s your bread-and-butter declaration. Define the desired state, then let Terraform do the heavy lifting.

But sometimes you don’t want to create anything in the cloud. You just want Terraform to do something. Run a script, call a webhook, poke Jenkins with a stick. That’s where null_resource comes in.

What the Hell is null_resource?#

null_resource is exactly what it sounds like. A Terraform resource that manages nothing. No infrastructure, no API objects. Just logic.

But here’s the trick. It still behaves like a real resource. It supports lifecycle actions (create, destroy, etc.), it can depend on other resources, and most importantly, it supports provisioners and triggers.

resource "null_resource" "example" {
triggers = {
always_run = timestamp()
}
provisioner "local-exec" {
command = "echo 'Triggering follow-up actions'"
}
}

This block runs every time, because timestamp() changes on every plan. Handy when you need to kick off external processes after infra changes.

Triggers: The Secret Sauce#

Triggers are the magic behind null_resource. They control when it gets re-executed. Not based on resource state, but on changes to arbitrary data you hand it.

Example:

triggers = {
hash = filemd5("config.json")
}

Now your null_resource only re-runs when config.json changes. That’s gold in CI/CD setups where you’re tracking file changes, API responses, or even environment variables.

Real-World Scenarios (Where null_resource Actually Earns Its Keep)#

1. Post-Provision Webhook Pings#

Say you’ve just spun up infrastructure and need to notify an external system. Triggering a GitHub Actions workflow, maybe, or pinging a Slack webhook.

resource "null_resource" "notify" {
triggers = {
infra_version = var.release_tag
}
provisioner "local-exec" {
command = "curl -X POST https://hooks.slack.com/services/XXX -d 'Terraform apply complete: ${var.release_tag}'"
}
}

Why not just use curl in your pipeline? Because this runs inside Terraform’s dependency graph. It won’t fire unless upstream infra actually changed.

2. Conditional Execution Based on Dynamic Data#

Say you’re pulling in an Azure storage account and want to trigger an action when its key changes.

data "azurerm_storage_account" "example" {
name = "examplestorageaccount"
resource_group_name = "my-rg"
}
resource "null_resource" "trigger_on_key_change" {
triggers = {
key = data.azurerm_storage_account.example.primary_access_key
}
provisioner "local-exec" {
command = "echo 'Access key has changed, executing...'"
}
}

Good for invalidating cache, refreshing secrets, or kicking off a rotation script. And yes, it’s hacky. It also works.

3. Smoke Testing After Apply#

Spin up resources, run a curl test to confirm the service responds, fail fast when it doesn’t.

resource "null_resource" "smoke_test" {
depends_on = [azurerm_function_app.example]
provisioner "local-exec" {
command = "curl -sf http://example-app.azurewebsites.net/health || exit 1"
}
}

You’d be surprised how often infra “successfully applies” while the app is dead on arrival. This catches those cases before CI marks the job green.

The Cleaner Alternative: terraform_data (1.4+)#

Starting in Terraform 1.4, HashiCorp shipped a new built-in: terraform_data. It does what null_resource does, with less baggage, and without leaning on a provider plugin.

resource "terraform_data" "run_command" {
triggers_replace = {
hash = filemd5("config.json")
}
provisioner "local-exec" {
command = "echo 'Still works!'"
}
}

You keep your triggers. triggers_replace takes any value, not only a map of strings, and Terraform replaces the resource, which re-runs its provisioners, whenever that value changes. And it’s first-party and cleaner for one-off tasks. Reach for it when you’re scripting or injecting data during apply without pretending to be a cloud resource.

null_resource vs terraform_data: When to Use What#

Use CaseUse null_resourceUse terraform_data
Triggering on external data changes✅ (triggers)✅ (triggers_replace)
One-time scripting✅✅
CI/CD glue between real resources✅✅
Need clean, future-proof setup❌ (plugin)✅ (built-in)
Want something to break later✅❌

TL;DR: on Terraform 1.4 or later, migrate to terraform_data, triggers included: triggers becomes triggers_replace. It’s the cleaner, future-proof option. Keep null_resource for older Terraform and for code you can’t touch yet.

Final Thoughts#

If Terraform were a programming language, null_resource would be its eval(). Powerful, dangerous, misused 99% of the time.

But in the hands of someone who knows what they’re doing? It bridges the gap between infrastructure and orchestration. Think CI/CD pipelines, edge cases, the situations where Terraform alone just can’t model reality.

Don’t go overboard, though. If you find yourself writing a bash script inside a null_resource that runs a Python script that generates more HCL, maybe reconsider your life choices.

Pro Tip: Pair null_resource with depends_on and triggers for controlled chaos. Or use terraform_data if you want to sleep at night.


Vladimir Mikhalev

Docker CaptainCNCF AmbassadorIBM Champion

The Verdict — production-tested analysis on YouTube.

The Verdict

Inconvenient truths about shipping in the AI era

Container security, platform engineering, and the agentic shift — tested in production, argued without the hype. The verdict reaches your inbox the moment there's one worth sending.

Related Posts

Same category
  1. 1
    Scheduled Workflows Fail Silently: A GitHub Actions Heartbeat for 122 Repositories
    DevOps & Cloud · GitHub Actions switches off a scheduled workflow after 60 quiet days, with no failed run and no red badge. What watches every schedule, and the watcher.
  2. 2
    Completed Is Not Restored: Kyverno Fail-Open, a Velero Restore and an HTTP 200 That Meant 500
    DevOps & Cloud · Scale Kyverno to zero and its webhooks vanish. A Velero restore said Completed over an empty disk. A 200 carried a 500. Three statuses that reported success.
  3. 3
    Docker Hub Immutable Tags: The Image That Shipped Before It Was Signed
    DevOps & Cloud · Docker Hub immutable tags refused a per-commit tag after the version tags were already out. The image shipped, the run died, and nothing ever signed it.
  4. 4
    Docker Hardened Images Migration: A Healthy Database That Kept Nothing
    DevOps & Cloud · Moving a Compose stack to Docker Hardened Images changes user IDs, paths and tools. On the old mount path, PostgreSQL stayed healthy and kept nothing.

Random Posts

Random
  1. 1
    Completed Is Not Restored: Kyverno Fail-Open, a Velero Restore and an HTTP 200 That Meant 500
    DevOps & Cloud · Scale Kyverno to zero and its webhooks vanish. A Velero restore said Completed over an empty disk. A 200 carried a 500. Three statuses that reported success.
  2. 2
    Restore Windows Firewall Defaults: Back Up the Rules First, Then Reset
    SysAdmin & IT Pro · Export the current policy with netsh advfirewall export, then restore defaults from the console, netsh advfirewall reset, or HNetCfg.FwPolicy2 in PowerShell.
  3. 3
    Configure Exchange Server 2010
    SysAdmin & IT Pro · Complete guide to configuring Exchange Server 2010. Learn mailbox setup, certificates, DNS, email policies, and secure mail delivery—step by step.
  4. 4
    Install OTRS on Ubuntu Server
    SysAdmin & IT Pro · Guide to installing OTRS Community Edition on Ubuntu Server — configure PostgreSQL, Apache, and SSL with Let's Encrypt, and launch OTRS securely.
null_resource vs terraform_data in Terraform: Triggers and Which to Pick
https://heyvaldemar.com/leveraging-null-resource-terraform-complex-operations/
Author
Vladimir Mikhalev
Published
2024-05-04
Updated
2026-11-05
License
CC BY-NC-SA 4.0