Explore the benefits of Infrastructure as Code (IaC) and dive into OpenTofu, an open source IaC tool forked from Terraform. Learn how OpenTofu works
Table of Contents
Introduction to OpenTofu
OpenTofu is an open-source infrastructure-as-code (IaC) tool that allows developers and system administrators to define, provision, and manage cloud and on-premises infrastructure using declarative configuration files. It originated as a community-driven fork of Terraform after licensing changes to Terraform’s core, ensuring that the IaC ecosystem remains open and collaborative.
With OpenTofu, you can describe your infrastructure in simple configuration files written in the HashiCorp Configuration Language (HCL) or JSON. These files specify the desired state of your infrastructure—such as servers, networks, databases, and other resources—and OpenTofu automatically figures out how to create, update, or delete resources to match that state.
Key Features:
- Open Source Governance: Fully community-led under the Linux Foundation, ensuring transparency and long-term stability.
- Terraform Compatibility: Maintains compatibility with existing Terraform configurations and providers, making migration straightforward.
- Declarative Configuration: Users define what they want, and OpenTofu determines how to achieve it.
- State Management: Keeps track of infrastructure state to ensure consistent deployments and safe updates.
- Extensibility: Supports a wide range of providers for cloud platforms like AWS, Azure, and Google Cloud, as well as on-premises systems.
Use Cases:
- Automating cloud infrastructure provisioning
- Managing multi-cloud environments
- Version-controlling infrastructure configurations
- Enabling reproducible and consistent deployments
- OpenTofu empowers teams to manage infrastructure efficiently, collaboratively, and transparently—without vendor lock-in.
For more information, visit opentofu.
Installation:
Please find the link for software - https://opentofu.org/docs/intro/install/
- OpenTofu is available on the winget repository and you can install it by running:
winget install --exact --id=OpenTofu.Tofu
run the command tofu
Verify installation:
tofu -version
Tofu Commands
1.tofu init
- Initializes a working directory containing OpenTofu configuration files.
- Downloads necessary provider plugins and modules.
- Prepares the backend for storing state.
- Example: tofu init
2.tofu plan
- Creates an execution plan showing what actions OpenTofu will take to reach the desired state.
- Compares the current infrastructure with the configuration files.
- Does not make any changes — it’s a preview step.
- Example: tofu plan
3.tofu apply
- Applies the changes required to reach the desired state of the configuration.
- Executes the plan generated by tofu plan.
- Prompts for confirmation before making changes (use -auto-approve to skip).
- Example: tofu apply -auto-approve
4.tofu destroy
- Destroys all resources defined in the configuration.
- Useful for cleaning up infrastructure.
- Prompts for confirmation unless -auto-approve is used.
- Example: tofu destroy -auto-approve
5. tofu validate
- Validates the configuration files for syntax and internal consistency.
- Does not check against actual infrastructure.
- Example: tofu validate
6.tofu fmt
- Formats configuration files to a canonical style.
- Helps maintain consistent formatting across teams.
- Example: tofu fmt
7.tofu show
- Displays the current state or a saved plan file in a human-readable format.
- Example: tofu show
8.tofu output
- Reads and displays output values from the state file.
- Useful for retrieving values like IP addresses or resource IDs.
- Example: tofu output
9.tofu state
- Manages the state file directly.
- Subcommands include list, show, mv, rm, and pull.
- Used for advanced state management.
- Example: tofu state list
10.tofu import
- Imports existing infrastructure into OpenTofu’s state.
- Does not modify the resource, only records it in the state file.
- Example: tofu import aws_instance.example i-1234567890abcdef
11.tofu graph
- Generates a visual representation of the dependency graph.
- Can be used with tools like Graphviz to visualize relationships.
- Example: tofu graph | dot -Tpng > graph.png
12.tofu workspace
- Manages multiple workspaces (environments) within the same configuration.
- Useful for separating dev, staging, and production environments.
- Example: tofu workspace new staging
13.tofu providers
- Displays information about the providers required by the configuration.
- Example: tofu providers
14.tofu version
- Displays the current version of OpenTofu.
- Example: tofu version
Demo
1.Create a file provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.39.0"
}
}
}
provider "aws" {
# Configuration options
access_key = "Replace_AWS_Access_Key"
secret_key = "Replace_AWS_Secret_Key"
region = "Replace_AWS_Region"
}
2.Create a file ec2.tf
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "example" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
*Run the commands *
1.tofu init : For Initializing the AWS Provider
- .terraform folder will be create after running the command tofu init
2.tofu plan : Creates an execution plan showing what actions OpenTofu will take to reach the desired state.
3.tofu apply : Applies the changes required to reach the desired state of the configuration.
4.tofu destroy: Destroy the resources created of any with reference to the configuration files after the user confirmation
Free Course
https://training.linuxfoundation.org/express-learning/getting-started-with-opentofu-lfel1009/
--
💬 If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it 😀 and follow me in dev.to , linkedin, github









Top comments (0)