Terraform is HashiCorp’s open-source Infrastructure as Code (IaC) tool that allows provisioning and managing cloud infrastructure (VMs, databases, networks, DNS records, Kubernetes clusters) using declarative configuration files. Unlike Ansible (which is primarily a configuration management tool that executes tasks imperatively), Terraform maintains a state file that tracks the real-world infrastructure it manages — allowing it to calculate the minimum set of changes needed to move from the current state to the desired state, and to destroy all provisioned resources cleanly. Terraform supports 3,000+ providers including AWS, Azure, GCP, DigitalOcean, VMware, Kubernetes, and on-premises infrastructure. This guide covers installing Terraform on RHEL 9 from HashiCorp’s official repository and verifying the installation.
Prerequisites
- RHEL 9 with sudo/root access
Step 1 — Add HashiCorp Repository and Install
# Add the official HashiCorp Linux repository
dnf install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# Install Terraform
dnf install -y terraform
terraform --version
Step 2 — Terraform Project Structure
mkdir -p /srv/terraform/myproject && cd /srv/terraform/myproject
# Standard Terraform project files:
# main.tf — resource definitions
# variables.tf — input variable declarations
# outputs.tf — output value definitions
# terraform.tfvars — variable values (not committed for secrets)
# versions.tf — required provider versions
# versions.tf — pin provider versions
terraform {
required_version = ">= 1.9"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
Step 3 — Terraform Workflow
# Initialise the working directory (downloads providers)
terraform init
# Preview what changes Terraform will make
terraform plan
# Apply the changes (prompts for confirmation)
terraform apply
# Destroy all managed infrastructure
terraform destroy
# Format all .tf files to canonical style
terraform fmt
# Validate configuration syntax
terraform validate
Step 4 — Enable Shell Completion
# Install bash completion for terraform commands
terraform -install-autocomplete
source ~/.bashrc
Step 5 — Install terraform-docs (Documentation Generator)
# Generate documentation from variable/output definitions
curl -fsSL https://github.com/terraform-docs/terraform-docs/releases/latest/download/terraform-docs-v0.18.0-linux-amd64.tar.gz | tar xz -C /usr/local/bin/
# Generate README from a Terraform module
terraform-docs markdown table . > README.md
Conclusion
Terraform on RHEL 9 provides a cloud-agnostic Infrastructure as Code platform that works with any cloud provider or on-premises infrastructure. The terraform init → plan → apply workflow is the core Terraform lifecycle: always run plan before apply to review the full set of changes Terraform will make, particularly when deleting or replacing existing infrastructure. The Terraform state file (terraform.tfstate) is the most critical file in any Terraform project — it should be stored in a remote backend (S3, Azure Blob, Terraform Cloud) rather than locally to enable team collaboration and prevent state loss.
Next steps: How to Use Terraform to Provision Infrastructure on RHEL 9, How to Install Ansible on RHEL 9, and How to Register a GitHub Actions Runner on RHEL 9.