TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Infrastructure as Code / Open Source / Software Development

Getting Started with OpenTofu Alpha

A quick guide to installation and importing Terraform state files.
Oct 18th, 2023 9:41am by
Featued image for: Getting Started with OpenTofu Alpha
Image from Denis Belitsky on Shutterstock.
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 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. 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 Is Included?

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 and below will continue to work.

What Do I Need to Know 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. If you’re interested in contributing to the registry, you can find the repo here.

OpenTofu Commands

Once downloaded, 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 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 good measure, I ran a `tofu apply`, and we can see that the only thing that changed was the version, which now shows 1.6.0, which is the current version of OpenTofu I am testing on.
{
  "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.

Use It in Scalr Today!

To switch to OpenTofu in Scalr, you simply need to update your workspace settings. Go to your pipeline settings and search for version 1.6.0 and save. The next run will update your workspace to use OpenTofu!

How Do I Report Issues or Contribute?

With any alpha release, it is expected that improvements will be needed and bugs will be identified. If you are interested in contributing or find an issue, please feel free to open an issue in the public repo here.
Aquileo | GroupCreated with Sketch.
TNS owner Insight Partners is an investor in: Pragma, Spacelift.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.