OpenTofu 101

Migrating from Terraform to Opentofu

Featured image

In the ever-evolving landscape of Infrastructure as Code, the Linux Foundation’s recent unveiling of the OpenTofu project marks a watershed moment. Launched on Sept. 20, at Open Source Summit Europe, in Bilbao, Spain, OpenTofu has emerged as a compelling alternative to Hashicorp’s Terraform, especially in light of the company’s recent shift from a Mozilla Public License to a Business Source License.

Backed by solid and big companies like Harness, Gruntwork and Spacelift, with a starting commitment of at least 18 full-time developers for the next five years, OpenTofu is not just another project but a collective commitment to open collaboration and innovation in the Infrastructure as Code and Cloud Native adoption. One of its most promising features is its commitment to backward compatibility, making it an attractive option for those considering to migrate from other Infrastructure as Code tools.

In this quick guide, we’ll walk you through the steps to get started with the OpenTofu alpha release.

How Do I Download OpenTofu?

Go to the release page of the OpenTofu repo and select the binary corresponding to the operating system on which you will be using OpenTofu.

What does this means?

First, a complete renaming of Terraform to OpenTofu had to be done for legal reasons. The team also had to ensure a suitable OpenTofu registry was in place, as the existing Terraform registry is only allowed to be used with Terraform.

From an end-user perspective, it is just a difference in the command line. Instead of calling terraform, you will now call tofu. All of the existing Terraform commands that were present from 1.5.x still work like a charm.

Ok, and what about the Provider Registry?

The original Terraform registry is essentially a metadata server that shows all the information regarding the providers, with the providers actually living in third-party sources like GitHub. The team created its own registry, ensuring that all existing 2,260 providers will continue to work without violating Hashicorp terms of service.

The release of the registry is considered alpha, with work underway to make a production-ready registry. The main GitHub repository already has a lot of stars and open issues, this is a great thing for a new project that came somewhat out of the blue. Find the repo here.

So, that’s the lowdown on getting started with OpenTofu. It’s pretty exciting stuff in the IaC world!

OpenTofu Commands

Once downloaded and installed into your local environment, there is little difference between the Terraform 1.5.x and OpenTofu commands. The basic commands remain the same:

tofu
Usage: tofu [global options] <subcommand> [args]
 
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
 
Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure
 
All other commands:
  console       Try OpenTofu expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote OpenTofu modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a OpenTofu resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  metadata      Metadata related commands
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  test          Execute integration tests for OpenTofu modules
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current OpenTofu version
  workspace     Workspace management
 
Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.

tofu init — Initializes the working directory where the configuration files are located. All modules, providers and plug-ins are downloaded or upgraded during this time.tofu plan — Executes a “dry run,” meaning that the resources are not actually created; instead you can see a preview of what to expect if the apply were to happen.tofu apply — Executes a run in which the resources defined in your configuration files are actually created.

How Do I Migrate from Terraform to OpenTofu?

It’s really simple; you only need to run OpenTofu commands on your existing state files. As seen here, I have an existing state file that was created using Terraform 1.5.0 and the null resource provider:

{
  "version": 4,
  "terraform_version": "1.5.0",
  "serial": 1,
  "lineage": "d8914af4-4a72-d67d-b390-8307ed7bdf86",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "null_resource",
      "name": "example",
      "provider": "provider[\"registry.terraform.io/hashicorp/null\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "3925604235592252374",
            "triggers": null
          },
          "sensitive_attributes": []
        }
      ]
    }
  ],
  "check_results": null
}

I added the OpenTofu binary, and then ran a tofu init:


tofu init
 
Initializing the backend...
 
Initializing provider plugins...
- Reusing previous version of hashicorp/null from the dependency lock file
- Using previously-installed hashicorp/null v3.2.1
 
OpenTofu has been successfully initialized!
 
You may now begin working with OpenTofu. Try running "tofu plan" to see
any changes that are required for your infrastructure. All OpenTofu commands
should now work.
 
If you ever set or change modules or backend configuration for OpenTofu,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Ran a tofu plan:

tofu plan
null_resource.example: Refreshing state... [id=3925604235592252374]
 
No changes. Your infrastructure matches the configuration.
 
OpenTofu has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

For sanity check and validating the usage, I ran a tofu apply, and we can see that the only thing that changed was the terraform version, which now shows 1.6.0, which is the current version of OpenTofu I am testing on, not terraform.


{
  "version": 4,
  "terraform_version": "1.6.0",
  "serial": 1,
  "lineage": "d8914af4-4a72-d67d-b390-8307ed7bdf86",
  "outputs": {},
}

Everything worked as expected, and there was no need to do any major migrations; just update the binary.

Conclusion

Terraform’s decision to adopt a Business Source License has raised concerns about the long-term implications for users and the open-source community, this change has led to a search for alternatives that maintain the open-source ethos, a role that OpenTofu is stepping into. With its commitment to backward compatibility and the support of a robust community, OpenTofu presents a viable and promising alternative for those who rely heavily on Terraform for their IaC needs.

The transition to OpenTofu, backed by substantial community support and development commitment, offers a pathway to continue leveraging the power of IaC without the limitations imposed by the new licensing model. This move is not just about maintaining the status quo; it’s an opportunity to contribute to and shape an emerging tool that could lead the next wave of IaC solutions and also improve on a more open source tool for the existing use cases of IaC.

As the IaC landscape continues to evolve, embracing tools like OpenTofu could be crucial in staying aligned with the ethos of open, collaborative development while ensuring that our infrastructure management tools remain robust, flexible, and accessible.

Build On!