In the post Setting Up Visual Studio Code for Terraform, we created a simple Terraform configuration and tested it locally.
This configuration didn’t deploy anything to the cloud, yet it successfully printed “Hello, Terraform!” to confirm our setup.
In this post, we’ll break down that code to understand how the Terraform Null Provider works and why it’s often used for testing or automation tasks.
🧩 The Full Code
Here’s the configuration we used earlier:
terraform {
required_providers {
null = {
source = "hashicorp/null"
}
}
}
provider "null" {}
resource "null_resource" "example" {
provisioner "local-exec" {
command = "echo Hello, Terraform!"
}
}
🧱 The terraform Block
terraform {
required_providers {
null = {
source = "hashicorp/null"
}
}
}
The terraform block defines the basic configuration Terraform needs to run your project.
Here, we’re specifying the required provider — in this case, the Null provider.
required_providers: Lists the providers your configuration depends on.null: The provider name.source = "hashicorp/null": Instructs Terraform to download this provider from the official HashiCorp Registry.
When you run terraform init, Terraform automatically downloads and installs the Null provider plugin.
⚙️ The provider Block
provider "null" {}
A provider is a plugin that tells Terraform how to communicate with a specific platform or API.
For example, you might use the Azure, AWS, or Google Cloud providers for real infrastructure.
The Null provider, however, doesn’t interact with any real cloud resources — it’s a dummy provider.
It’s mainly used for testing, experimentation, or running local commands without deploying anything.
Since it requires no configuration, we can leave this block empty ({}).
🧱 The resource Block
resource "null_resource" "example" {
provisioner "local-exec" {
command = "echo Hello, Terraform!"
}
}
A resource in Terraform represents something that Terraform creates or manages.
Here, we’re defining a null_resource — a special type of resource that does not create any actual infrastructure.
Let’s break it down:
null_resource→ The type of resource (from the Null provider).example→ The name you assign to this instance (arbitrary, for internal reference).provisioner "local-exec"→ Runs a local command when the resource is created or destroyed.command = "echo Hello, Terraform!"→ The command executed on your machine.
When you run terraform apply, Terraform “creates” this resource, then immediately runs the local command, printing “Hello, Terraform!” in your terminal.
🧪 What Happens Behind the Scenes
When you execute this configuration:
terraform initdownloads the Null provider plugin.terraform planshows what Terraform will do (create one null_resource).terraform apply“creates” the resource and executes the local command.- Terraform tracks this fake resource in its state file (
terraform.tfstate) — just like real resources.
💡 Why the Null Provider Is Useful
The Null provider is surprisingly powerful for such a simple idea.
Here are a few practical uses:
- ✅ Testing Terraform setups before connecting to cloud environments.
- ⚙️ Running local scripts or shell commands as part of a deployment workflow.
- 🔁 Creating dependencies or triggers for other Terraform resources.
- 🧩 Integrating Terraform with CI/CD pipelines to automate pre- or post-deployment steps.
✅ Summary
In this post, we learned that the Terraform Null Provider:
✔️ Lets you test Terraform without deploying real infrastructure.
✔️ Provides the null_resource for running local commands.
✔️ Helps you experiment safely before moving to cloud providers.
This foundational setup — Provider → Resource → Provisioner — is the same pattern used for all Terraform configurations, whether local or cloud-based.