liveBook · Manning

OpenTofu CLI

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

OpenTofu is an open-source infrastructure-as-code tool derived from Terraform and designed to process Terraform-compatible configuration.

A manually configured AWS virtual private cloud (VPC) can take hours to recreate. Infrastructure as code replaces that work with a reusable description of the desired resources. OpenTofu provides the open-source `tofu` CLI for running Terraform-compatible configurations. section 1.0 section 1.6.6

OpenTofu was forked after Terraform releases starting with version 1.6 moved away from an open-source license. It has remained compatible with Terraform code, so switching runners usually requires little change. Compatibility is not a permanent guarantee: OpenTofu-only language features can make a return to Terraform harder, while new Terraform features may reach OpenTofu later. section 1.6 section 1.6.5

The familiar deployment workflow carries over to `tofu`:

Listing 7.8 Switching between OpenTofu and Terraform
TF_ENGINE:=terraform                                            #1
 
ifeq ($(TF_ENGINE), terraform)                                  #2
    TF_BINARY:=terraform                                        #3
else ifeq ($(TF_ENGINE), opentofu)                              #4
    TF_BINARY:=tofu                                             #5
endif
 
.PHONY: format
format:
  $(TF_BINARY) fmt                                              #6

This Makefile listing maps a selectable engine name to either `terraform` or `tofu`, then uses the chosen binary for formatting. section 7.2.4

Check formatting with OpenTofu

With that Makefile, run `make format TF_ENGINE=opentofu`. The same target can retain Terraform as its default, making it practical to test both runners without rewriting the target. section 7.2.4

Runner choice also has operational consequences. New releases can introduce breaking changes, so teams supporting multiple versions can use `tenv` to select Terraform and OpenTofu versions. For continuous delivery, check platform support and licensing: the choice of runner can affect which delivery systems are available. section 7.2.5 section 8.6.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains command compatibility, the init-plan-apply workflow, runner selection, version management, and caveats without detailing CLI internals.
Examples
Uses an AWS VPC as a reuse example and a Makefile formatting target as a concrete runner-switching example.
What it assumes
Introduces infrastructure as code through an AWS VPC example; uses Make only for the optional runner-switching pattern.
What you can do after
Apply it via `tofu` to run Terraform-compatible commands and select OpenTofu for a Makefile target.
How it connects
Connects OpenTofu to Terraform, infrastructure as code, providers, modules, Make, version management, and continuous delivery.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.