OpenTofu
OpenTofu
February 2, 2024

Understanding OpenTofu Remote Backends

By
Ryan Fee

OpenTofu Backends serve as the storage and retrieval mechanism for OpenTofu state files. The state file is crucial as it keeps track of the current state of your infrastructure, enabling OpenTofu to plan and apply changes accurately. By default, OpenTofu stores the state locally on the machine where the OpenTofu commands are executed. However, as teams and projects grow, the limitations of local state become evident. This is where OpenTofu Backends become advantageous and a best practice.

Advantages to using a Backend

Before we get into the various types of backends, which have advantages and disadvantages of their own, we’ll talk about some high level reason why any backend is needed:

  • Remote State Storage: OpenTofu backends facilitate the storage of state files remotely, providing a centralized location accessible by multiple team members. This remote storage is crucial for collaboration, as it eliminates the need to share state files manually and ensures that everyone is working with the latest infrastructure state.
  • Concurrency and Locking: In collaborative environments, concurrent OpenTofu executions can lead to conflicts if not managed properly. Most OpenTofu backends offer locking mechanisms to prevent multiple users from modifying the state simultaneously, ensuring consistency and avoiding conflicts. This feature is especially vital when multiple team members are working on the same infrastructure.
  • Scalability: As infrastructure grows in complexity and spans multiple environments, the need for scalable solutions becomes apparent. OpenTofu backends, particularly remote ones like Scalr, Amazon S3, or Azure Storage provide a scalable platform for storing and managing state files, supporting large and dynamic infrastructures.

Types of remote Backends:

Two types of backends can be used with OpenTofu, a standard backend or a remote operations backend. We’ll break down the differences between these two:

Standard: 

A standard remote backend is one in which you store OpenTofu state only. An example of this is storing the state in an AWS S3 bucket. The S3 option provides a locking capability through DynamoDB, but the main reason to use this type of backend is to ensure state is stored locally and not on individual machines,

Example Usage:

terraform {
 backend "s3" {
   bucket = "mybucket"
   key    = "path/to/my/key"
   region = "us-east-1"
 }
}

The equivalent option for state storage in a bucket also exists in Google Cloud Storage as well as Azure Blob Storage.

Remote Operations:

The more advanced backend is a remote operations backend that not only stores the state, but also executes the OpenTofu runs, stores the run history, logs, variables, and more within a workspace. Remote operation backends become the central hub for all of your OpenTofu activity with the ability to create structure and integrate with the rest of your DevOps ecosystem. Scalr is an example of a remote operations backend that works for Terraform or OpenTofu. Terraform Cloud is the other remote operations backend in the space but only supports Terraform at this time.

Integrating with a remote backend is simple, as you only need to enter the Scalr URL of your account, the environment name, and lastly the workspace where the run should execute: 

Example Usage:

terraform {
  backend "remote" {
    hostname = "<account-name>.scalr.io"
    organization = "<scalr-environment-name>"

    workspaces {
      name = "<workspace-name>"
    }
  }
}

Upon running a tofu init a Scalr workspace will be created for the operations and state to be stored in.

A remote operations backend, like Scalr, not only provides what is mentioned above but also enables much more being that the runs actually execute in the backend:

Integrations:

  • Plug into Github, Gitlab, Azure DevOps, or BitBucket to create a module registry or enable a GitOps workflow for the OpenTofu code. 
  • Natively integrate with collaboration tools like Slack or MS Teams to be notified of OpenTofu run events and take action.
  • Enable Open Policy Agent to ensure your OpenTofu runs are compliant with your organization's standards.
  • Stream OpenTofu run events and metrics to Datadog or any product that supports the OpenMetrics project to analyze overall operations.
  • Being that remote operations backends usually come with a UI and structure, you will be able to integrate with a SAML 2.0 solution, like Okta, to secure who has access to the OpenTofu workspaces.

Visibility:

  • Use built-in reporting to understand module, provider, and OpenTofu version usage.
  • OpenTofu run dashboards show the current runs in the queue as well as historical run information.

Security:

  • A remote operations backend, like Scalr, comes with a full organizational model that allows for isolated environments to separate workspaces by users, teams, cloud credentials, or more.
  • Through a robust RBAC model you can set who can create runs, who can approve applies, who will manage the module registry, or who will override OPA policies and more.

While a standard OpenTofu backend usually helps DevOps teams get started with infrastructure as code, a remote operations backend is what helps those teams take things to the next level. 

Find out more about configuring a remote operations backend in the official docs here.

Summary:

OpenTofu backends play a pivotal role in streamlining IaC workflows, offering benefits such as remote state storage, concurrency and locking, scalability, and support for various backend types. As organizations embrace OpenTofu for managing their infrastructure, leveraging the capabilities of OpenTofu backends becomes essential for achieving a scalable, collaborative, and efficient IaC pipeline. By adopting these best practices, teams can navigate the complexities of infrastructure management with confidence and reliability.

Try it out in Scalr for free today!

Note: While this blog references Terraform, everything mentioned in here also applies to OpenTofu. New to OpenTofu? It is a fork of Terraform 1.5.7 as a result of the license change from MPL to BUSL by HashiCorp. OpenTofu is an open-source alternative to Terraform that is governed by the Linux Foundation. All features available in Terraform 1.5.7 or earlier are also available in OpenTofu. Find out the history of OpenTofu here.

Start using the OpenTofu & Terraform platform of the future.

A screenshot of the modules page in the Scalr Platform