In this guide, you’ll learn how to migrate an existing Terraform project to OpenTofu, verify the changes, and then roll back to Terraform. We’ll cover:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Applying the initial Terraform configuration
- Installing OpenTofu
- Backing up state files
- Initializing and planning with OpenTofu
- Updating resources and applying changes
- Rolling back to Terraform
1. Apply the Initial Terraform Configuration
-
Navigate to your project directory and review main.tf:
-
Initialize and apply:
-
When prompted, enter
yes. You should see: - Verify that terraform.txt exists with the correct content.
2. Install OpenTofu
Refer to the OpenTofu installation guide and select the installer for your OS.Make sure to check your distribution using
cat /etc/os-release before running the installer.
Automated Debian/Ubuntu Installer
3. Backup the Terraform State
Always archive your existing Terraform state before migrating. Losing state can lead to resource drift or duplicates.
4. Initialize with OpenTofu
Switch to the OpenTofu workflow:5. Preview the OpenTofu Plan
Compare the two workflows with this quick reference:| Action | Terraform Command | OpenTofu Command |
|---|---|---|
| Initialize | terraform init | tofu init |
| Plan | terraform plan | tofu plan |
| Validate | terraform validate | tofu validate |
| Apply | terraform apply | tofu apply |
local_file.file resource with filename = "terraform.txt".
6. Update the main.tf for OpenTofu
Change main.tf to point to a new file and content:7. Apply with OpenTofu
Apply the OpenTofu configuration:yes when prompted.

8. Roll Back to Terraform
-
Backup the OpenTofu state:
-
Re-initialize with Terraform:

-
Revert main.tf to the original Terraform resource:
-
Apply with Terraform:
Enter
yesand observe: