Skip to main content
Managing infrastructure as code with OpenTofu relies on state files to map configurations to real resources and track metadata, such as dependencies, for proper creation and deletion order. While local state works for small setups, it becomes a bottleneck in larger teams and complex environments. Remote state backends solve these challenges by providing shared storage, locking, and encryption.

Local State Files

When you initialize a project locally, you’ll see:
A simple resource in main.tf might look like:
The corresponding snippet from terraform.tfstate:
Local state files often contain sensitive data. Do not commit terraform.tfstate or any .tfstate files to version control.

Built-in State Locking

OpenTofu locks the local state file during operations to prevent concurrent writes:
If you run another tofu apply in parallel, you’ll encounter:
However, version control platforms like GitHub do not support file-level locking, leading to potential merge conflicts and corrupted state:
The image illustrates a workflow involving GitHub, two users, and an S3 bucket, showing the management of main.tf and terraform.tfstate files. The terraform.tfstate file is not stored in GitHub but is managed between the users and the S3 bucket.

Benefits of Remote State Backends

Remote backends centralize state storage, enable reliable locking, and secure data at rest and in transit. They integrate with popular cloud services:
The image illustrates a state locking process for "OpenTofu" using a remote state backend, with options like AWS S3, HashiCorp Consul, and Google Cloud Storage, showing operations and infrastructure interactions.
Once configured, OpenTofu automatically loads state from the remote backend and uploads updates after each apply:
The image is a diagram illustrating "State Locking" with OpenTofu State as the remote state backend, showing integration with AWS S3, Google Cloud Storage, and HashiCorp Consul. It highlights features like automatic state file management and support for state locking across various backends.

Configuring AWS S3 as a Remote Backend

Ensure you have:
  • An existing S3 bucket for storing state
  • (Optional) A DynamoDB table for locking
The image illustrates a remote backend setup for managing state with "OpenTofu State" and "State Locking" components, showing a connection from a local environment to a remote state backend. It includes details about the object and value configurations for a bucket, key, region, and DynamoDB table.
  1. In your project directory, you’ll see:
  2. Update main.tf to add an S3 backend:
  3. Run tofu apply and note the backend initialization error:
  4. Initialize and migrate your state:
    Responding with yes migrates your local state to S3. You can then remove the local terraform.tfstate file.
  5. Future applies use the remote backend:

Inspecting and Modifying State

OpenTofu provides the tofu state command group for safe state inspection and modifications. Avoid editing state files manually. Examples:
Understanding and implementing remote state with state locking ensures a secure, collaborative, and reliable infrastructure lifecycle management workflow.

Watch Video