Home Infrastructure as Code (IaC) Getting Started with Terraform Hands-On (Using the Null Provider)

Getting Started with Terraform Hands-On (Using the Null Provider)

249
0

🪜 Introduction

Terraform is an Infrastructure as Code (IaC) tool that lets you define and manage infrastructure using declarative configuration files.
Instead of manually creating cloud resources, you describe what you want in code — Terraform handles the rest.

In this first hands-on exercise, you’ll create and run a simple Terraform configuration using the Null Provider.
This provider doesn’t deploy anything to the cloud — instead, it helps you test your setup and understand how Terraform works behind the scenes directly from the command line.


🧰 Prerequisite

Before starting this exercise, make sure you’ve completed the setup steps in the post:
👉 Installing Terraform on Linux (Ubuntu) and Windows (WSL2 with Ubuntu)


🧩 Step 1: Create Your Working Directory

Open your terminal and create a new folder for this lab:

mkdir terraform-lab
cd terraform-lab

🧩 Step 2: Create the Terraform Configuration File

Use a terminal-based editor like nano or vim to create a new file named main.tf.

Example using nano:

nano main.tf

Alternatively if you are familiar with the vi editor, you may create file as vi main.tf. Personally I use vi.

Paste the following configuration:

terraform {
  required_providers {
    null = {
      source = "hashicorp/null"
    }
  }
}

provider "null" {}

resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo Hello, Terraform!"
  }
}

Save and exit (Ctrl + O, Enter, Ctrl + X in nano).


⚙️ Step 3: Initialize Terraform

Initialize the Terraform working directory:

terraform init

Terraform downloads the Null Provider plugin and prepares your environment for execution.


🧾 Step 4: Understand the Configuration

🧱 The terraform Block

Defines the basic setup Terraform needs to run your configuration.
The required_providers section specifies that the null provider should be downloaded from the HashiCorp registry.

⚙️ The provider Block

Specifies which provider to use.
The Null provider doesn’t interact with any cloud service — it’s mainly used for testing and running local commands.

🧱 The resource Block

Represents a resource managed by Terraform.
Here, a null_resource runs a local shell command (echo Hello, Terraform!) without creating any real infrastructure.


▶️ Step 5: Run Terraform Commands

Initialize (if not done already)

terraform init

Preview the Execution Plan

terraform plan

This command shows what Terraform will do — in this case, create one fake null_resource.

Apply the Configuration

terraform apply

When prompted, type yes.

You’ll see output like:

null_resource.example: Creating...
null_resource.example: Provisioning with 'local-exec'...
null_resource.example (local-exec): Hello, Terraform!
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Terraform has successfully “created” the resource and executed the local command.


🧪 Step 6: View the Terraform State

Terraform tracks managed resources in a state file named terraform.tfstate.
Check it with:

cat terraform.tfstate

You’ll see JSON data showing your null_resource and its properties.


🧹 Step 7: Clean Up

To remove the fake resource and reset your environment:

terraform destroy

Confirm with yes when prompted.


💡 Why the Null Provider Is Useful

The Null Provider is lightweight but powerful for:
✅ Testing Terraform configurations before connecting to real cloud environments
⚙️ Running local shell scripts or automation steps
🔁 Creating triggers or dependencies for other resources
🧩 Integrating Terraform with CI/CD pipelines for pre- and post-deployment tasks


✅ Summary

You’ve completed your first Terraform hands-on exercise entirely from the command line!

You learned that the Terraform Null Provider:
✔️ Lets you test Terraform safely without creating real infrastructure
✔️ Provides the null_resource for running local commands
✔️ Follows the same structure used in all Terraform projects — Provider → Resource → Provisioner

Previous articleWhat is Node.js? A Beginner’s Guide
Next articleBeginner’s Guide to Node.js: Step-by-Step Learning Path
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here