Skip to main content

Cloud and deployment · Intermediate · 16 min

How to Manage Infrastructure as Code with OpenTofu

By Padding · Updated 15 Nov 2026
OpenTofu 1.13 · Ubuntu 26.04.1 LTS

You will install OpenTofu 1.13 on Ubuntu 26.04 from a checksum-verified release, describe two resources in code, create them with a saved plan, change and repair them, and then destroy everything, all without a cloud account.

On this page

Infrastructure as code means you describe what should exist in text files, and a tool makes the real world match. You review the change before it happens, you keep the files in Git, and you can rebuild the same setup tomorrow. OpenTofu is an open source tool for this, run by the Linux Foundation. It reads the same configuration language as Terraform, called HCL, and it has its own command: tofu.

Cloud resources are the usual use, but the workflow is the same for any provider. In this tutorial you use two providers that need no account and bill nothing. One generates a random name, and the other writes a file to disk. They are enough to learn the whole loop: initialise, plan, apply, change, detect drift and destroy. When you later point the same loop at AWS or Google Cloud, only the provider and the resources change.

Step 1 · Install OpenTofu

This tutorial installs the release archive from the OpenTofu GitHub page and checks it against the published SHA-256 list before it runs anything. OpenTofu also publishes an apt repository and an install script, which are better if you want updates through apt. An archive you unpack yourself gets no automatic security updates, so you will need to repeat this step when a new version is out.

Install curl, then download the archive and the checksum list. The dpkg --print-architecture call picks amd64 or arm64 to match your machine:

Bash
sudo apt update
sudo apt install -y curl ca-certificates
VERSION=1.13.9
ARCH=$(dpkg --print-architecture)
cd "$(mktemp -d)"
curl -fsSLO "https://github.com/opentofu/opentofu/releases/download/v${VERSION}/tofu_${VERSION}_linux_${ARCH}.tar.gz"
curl -fsSLO "https://github.com/opentofu/opentofu/releases/download/v${VERSION}/tofu_${VERSION}_SHA256SUMS"
sha256sum --ignore-missing -c "tofu_${VERSION}_SHA256SUMS"

The last command must print tofu_1.13.9_linux_<arch>.tar.gz: OK. If it says FAILED, stop and download again. Then unpack the tofu program and put it on your path:

Bash
tar -xzf "tofu_${VERSION}_linux_${ARCH}.tar.gz" tofu
sudo install -m 0755 tofu /usr/local/bin/tofu
tofu version

Check it worked

tofu version prints OpenTofu v1.13.9 followed by your platform, for example on linux_amd64.

Step 2 · Write the configuration

Make a project folder. OpenTofu treats every .tf file in a folder as one configuration:

Bash
mkdir -p ~/tofu-demo
cd ~/tofu-demo

Create main.tf. It does four things: it pins the OpenTofu version and the two providers, declares an input variable, defines two resources, and prints one output.

Bash
cat > main.tf <<'HCL'
terraform {
  required_version = ">= 1.13.0"

  required_providers {
    random = {
      source  = "hashicorp/random"
      version = "~> 3.9"
    }
    local = {
      source  = "hashicorp/local"
      version = "~> 2.9"
    }
  }
}

variable "greeting" {
  type        = string
  description = "First word of the note."
  default     = "hello"
}

resource "random_pet" "name" {
  length = 2
}

resource "local_file" "note" {
  filename = "${path.module}/note.txt"
  content  = "${var.greeting} from ${random_pet.name.id}\n"
}

output "pet" {
  value = random_pet.name.id
}
HCL

The block is called terraform for compatibility with Terraform; OpenTofu reads it as it is. Three ideas are worth knowing:

  • A provider is a plugin that talks to one kind of system. ~> 3.9 allows any 3.x release from 3.9 up, and never 4.0.
  • A resource is one thing OpenTofu manages. random_pet.name is the type random_pet with the local name name.
  • random_pet.name.id in the file's content is a reference. OpenTofu reads it and knows the file depends on the pet name, so it creates the pet first.

Check the formatting and the syntax:

Bash
tofu fmt

tofu fmt rewrites the files in the current folder to the standard layout and prints the name of each file it changed. It prints nothing here, because the file is already formatted.

Step 3 · Initialise the folder

tofu init downloads the providers you named and writes a lock file:

Bash
tofu init

You will see lines such as Installing hashicorp/random v3.9.1... and, at the end, OpenTofu has been successfully initialized!. Two things now exist in the folder. The hidden .terraform/ folder holds the downloaded providers and should not go into Git. The file .terraform.lock.hcl records the exact provider versions and their checksums. Do commit that one, so a teammate or a build server gets the same versions you did.

Validate the configuration without touching anything:

Bash
tofu validate

Check it worked

The output is Success! The configuration is valid.

Troubleshooting

If init fails with a message about a provider not being found, check the source strings and your network access to registry.opentofu.org. If it fails with a checksum error after you edited the lock file, delete .terraform.lock.hcl and run tofu init again, then review the new file.

Step 4 · Plan, then apply the plan

A plan is OpenTofu's list of changes, made by comparing your files with what it knows exists. Save the plan to a file:

Bash
tofu plan -out=tfplan

Read it. Both resources show will be created, and the summary says Plan: 2 to add, 0 to change, 0 to destroy. Attribute values marked (known after apply) do not exist yet, such as the pet name.

Apply exactly that plan:

Bash
tofu apply tfplan

Because you pass a saved plan, OpenTofu does not ask for confirmation and applies nothing other than what you reviewed. Without a plan file, tofu apply makes a fresh plan and asks you to type yes. Saved plans can contain sensitive values, so treat tfplan like a secret and do not commit it.

Look at the result:

Bash
cat note.txt
tofu output
tofu state list

Check it worked

note.txt holds one line, hello from followed by a two-word name that is different on your machine. tofu output prints the same name as pet = "...", and tofu state list shows local_file.note and random_pet.name.

The list comes from the state, the file terraform.tfstate in the folder. It records which real objects belong to which resources. OpenTofu uses it on every run, and it can contain secrets in plain text. Never commit it. For a team, keep state in a remote backend, so people share one copy and do not overwrite each other.

Step 5 · Change the configuration

Run a plan with nothing changed:

Bash
tofu plan

The result is No changes. Your infrastructure matches the configuration. This is a property worth relying on: a plan after an apply should always be empty.

Now change an input. Plan with a different value for the variable, which is how you would pass a value per environment:

Bash
tofu plan -var greeting=goodbye

The plan says local_file.note must be replaced and marks the content line with # forces replacement. The provider cannot edit a file's content in place, so it deletes the file and writes a new one. Reading for words like replaced and destroyed is the main skill in reviewing a plan: on a real database, a replacement is data loss. The pet is not touched, because nothing about it changed.

Apply the change:

Bash
tofu apply -var greeting=goodbye -auto-approve
cat note.txt

-auto-approve skips the confirmation prompt. Use it in scripts and automation that already reviewed a plan. When you work by hand, leave it out and read what you approve. The note now says goodbye from and the same pet name.

Step 6 · Detect and repair drift

Drift is when the real world changes without OpenTofu. Simulate it by deleting the file yourself:

Bash
rm note.txt
tofu plan

OpenTofu refreshes its view of each resource before it plans. It notices the file is gone and proposes to create it again, using the greeting that is still the default in main.tf: the plan shows hello from ..., not goodbye, because the -var flag from the earlier command is not stored anywhere. That is a common surprise, and it is why inputs belong in a file. Put your own value in a file named terraform.tfvars, which OpenTofu reads automatically:

Bash
echo 'greeting = "goodbye"' > terraform.tfvars
tofu plan -out=tfplan
tofu apply tfplan
cat note.txt

The note is back and says goodbye again. A file does not hold secrets here. If a variable did, you would not put it in a committed file.

Step 7 · Destroy what you created

tofu destroy plans the removal of everything in the state and asks you to confirm by typing yes:

Bash
tofu destroy

Read the list before you answer. It names both resources and ends Plan: 0 to add, 0 to change, 2 to destroy. For a folder like this one, answer yes.

Then remove the working files if you do not want them:

Bash
ls
cd ~
rm -rf ~/tofu-demo

Check it worked

Before the rm, ls lists main.tf, terraform.tfvars, tfplan and the state files, and note.txt is gone. If you ran a real provider here, the console or CLI of that provider is the place to confirm that the objects are gone, since OpenTofu only knows what is in its state.

Next steps

  • Keep state somewhere shared and safe: store Terraform state remotely in an S3 bucket. That tutorial uses Terraform; OpenTofu has its own S3 backend documentation, and the two have diverged on some options, so read it before you copy settings across.
  • Read the language reference and the provider registry at opentofu.org/docs to see what else you can manage.
  • Put the folder in Git and commit main.tf and .terraform.lock.hcl. Add .terraform/, *.tfstate*, tfplan and terraform.tfvars to .gitignore.
  • Try the same loop with a cloud provider on a throwaway account, and run tofu destroy when you finish so nothing keeps billing.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.