0Pricing
DevOps Bootcamp · Lesson

Null Resources and Triggers

Discover `null_resource` for executing arbitrary commands that don't map to a real cloud resource, and use triggers to control when provisioners run.

Null Resources and Triggers is a free DevOps Bootcamp lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is null_resource?

Terraform's null_resource is a special type of resource that doesn't directly create or manage any infrastructure object in your cloud provider.

Instead, it acts as a placeholder for running arbitrary commands or scripts, often used for tasks that fall outside the scope of typical resource provisioning.

Why Use null_resource?

You might wonder why you'd need a 'null' resource. It's incredibly useful for:

  • Running Local Scripts: Executing commands on the machine where Terraform is run (e.g., preparing configuration files).
  • Post-Deployment Actions: Performing setup or cleanup tasks after real resources are provisioned.
  • Interacting with External APIs: When a dedicated Terraform provider doesn't exist for a specific service.
  • Custom Logic: Orchestrating custom steps that aren't tied to a specific cloud resource.

Basic null_resource Structure

A null_resource block looks like any other Terraform resource. By itself, without any provisioners or triggers, it simply creates a 'null' object in the Terraform state and does nothing else.

It's typically paired with provisioners to execute commands, and triggers to control when those commands run.

Simple null_resource Example

Here's a minimal null_resource. If you terraform apply this, it will be created in the state, but no external action will occur.

It's just a placeholder, ready to be enhanced with actions.

resource "null_resource" "my_placeholder" {
  # This resource exists in state but performs no action.
  # It's ready to attach provisioners or triggers.
}

Provisioners with null_resource

The real power of null_resource comes when you combine it with provisioners.

Provisioners allow you to execute scripts or commands on the machine running Terraform (local-exec) or on the remote resource itself (remote-exec, though less common with null_resource).

This lets null_resource become an action executor.

Introducing Triggers

triggers are a map of arbitrary key-value pairs defined within a null_resource block.

Their purpose is to tell Terraform: 'If any value in this map changes, this null_resource (and its provisioners) needs to be re-run.'

This is crucial for making the null_resource react to changes in your infrastructure.

How Triggers Work

When a value in the triggers map changes, Terraform detects a modification to the null_resource.

During a terraform apply, Terraform will then:

  • Destroy the existing null_resource (if it exists).
  • Re-create the null_resource.
  • Re-run any associated provisioners.

This mechanism ensures that your custom actions are executed only when relevant dependencies change.

Example: Triggers & local-exec

Here, we use a null_resource to simulate updating a configuration file based on a simple variable. The local-exec provisioner runs a command, and the triggers ensure it re-runs if var.app_version changes.

variable "app_version" {
  description = "The version of the application."
  type        = string
  default     = "1.0.0"
}

resource "null_resource" "update_config_file" {
  triggers = {
    version_change = var.app_version
  }

  provisioner "local-exec" {
    command = "echo 'Config updated for version: ${self.triggers.version_change}' > app_config.txt"
  }
}

Best Practices & Alternatives

While powerful, use null_resource judiciously:

  • Prefer Providers: If a dedicated Terraform provider or resource exists for a task, use it instead.
  • Keep it Idempotent: Ensure scripts run by provisioners can be safely re-run multiple times without side effects.
  • Avoid State Management: Don't try to manage external state with null_resource; that's what remote state backends are for.
  • Consider external Data Source: For complex external logic that returns structured data, external data source might be a cleaner alternative.

Quick Check: Null Resources

You've learned about null_resource and triggers. Let's test your understanding.

Recap: Null & Triggers

In this lesson, you discovered the versatility of Terraform's null_resource, a powerful tool for executing custom commands and scripts that don't map to concrete cloud resources.

You also learned about triggers, a mechanism within null_resource that allows you to define dependencies and ensure provisioners are re-executed whenever those dependencies change. This enables dynamic and reactive custom automation within your IaC.

Frequently asked questions

Is the “Null Resources and Triggers” lesson free?

Yes — the full text of “Null Resources and Triggers” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Null Resources and Triggers”?

Discover `null_resource` for executing arbitrary commands that don't map to a real cloud resource, and use triggers to control when provisioners run. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Null Resources and Triggers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Querying Data with Data Sources
  2. Local and Remote Exec Provisioners
  3. Null Resources and Triggers
  4. Using External Data and Templates
← Back to DevOps Bootcamp