> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/infrastructure-as-code-management/use-iacm/iac-provisioners/opentofu/state-management.md).

# State Management

Understand how Harness IaCM manages OpenTofu state and how to work with existing state.

Harness Infrastructure as Code Management (IaCM) automatically manages remote state for your OpenTofu workspaces. When you create a workspace, Harness configures itself as the backend without requiring any backend block in your `.tf` files. State is encrypted at rest, versioned, and access is controlled through Harness RBAC permissions.

## What you will learn <a href="#what-you-will-learn" id="what-you-will-learn"></a>

* How Harness handles state when you create new infrastructure
* How to migrate existing state from other backends or Terraform Cloud
* How to use external backends like S3, GCS, or Azure instead of Harness-managed state
* How state locking prevents concurrent modifications
* How to access Harness-managed state from your local CLI

***

## Start fresh with Harness-managed state <a href="#start-fresh-with-harness-managed-state" id="start-fresh-with-harness-managed-state"></a>

When you create a new OpenTofu workspace and run your first provision pipeline, Harness automatically creates and manages the state file. You do not need to configure anything.

### What Harness handles <a href="#what-harness-handles" id="what-harness-handles"></a>

* **Automatic backend configuration:** No backend block needed in your `.tf` files. Harness injects the backend configuration at runtime during the init step.
* **State encryption:** State files are encrypted at rest.
* **Access control:** State access is controlled through Harness RBAC permissions. Go to [RBAC in Harness](https://developer.harness.io/harness-platform/use-harness-platform/platform-access-control) to understand how permissions work.
* **State versioning:** Every state change is versioned. You can view historical state versions in the workspace State tab and download previous versions if needed.
* **Workspace isolation:** Each workspace has its own state file. If you have multiple environments (dev, staging, prod), create separate workspaces so each environment has isolated state.

### View state in the UI <a href="#view-state-in-the-ui" id="view-state-in-the-ui"></a>

To view your workspace state in Harness, do the following:

1. Navigate to your workspace in the **Infrastructure** module.
2. Select the **State** tab.

The State tab shows the current state file contents in JSON format. You can download the state file or view specific resource details.

You can also view resources in the **Resources** tab, which displays a structured view of the infrastructure managed by this workspace.

***

## Migrate existing state into Harness <a href="#migrate-existing-state-into-harness" id="migrate-existing-state-into-harness"></a>

If you have existing OpenTofu or Terraform state files in another backend, you can migrate them into Harness-managed storage. Harness supports three migration methods: local CLI, pipeline-based migration, and API-based migration.

Go to [State Migration](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/state-migration.md) to learn how to migrate state using each method.

### Terraform Cloud migration <a href="#terraform-cloud-migration" id="terraform-cloud-migration"></a>

Terraform Cloud does not support direct `tofu init -migrate-state` conversion to third-party backends like Harness IaCM. To migrate from Terraform Cloud, you need to pull your state locally first, then migrate to Harness.

### To migrate from Terraform Cloud <a href="#to-migrate-from-terraform-cloud" id="to-migrate-from-terraform-cloud"></a>

1. Initialize your OpenTofu project with your Terraform Cloud backend.
2. Pull your state locally with `tofu state pull`:

   ```bash
   tofu state pull > terraform.tfstate
   ```
3. Remove the Terraform Cloud backend block from your `.tf` files.
4. Delete the `.terraform` directory and reinitialize with a local backend:

   ```bash
   rm -rf .terraform
   tofu init
   ```
5. Follow the CLI migration steps in the State Migration guide to move the local state into Harness.

At this point you are using a local backend with the content of your Terraform Cloud state, and you can proceed with the standard migration process.

Go to [OpenTofu State Command Reference](https://opentofu.org/docs/cli/commands/state/) to learn about state inspection and manipulation commands.

***

## Use external backends <a href="#use-external-backends" id="use-external-backends"></a>

If you need to use an external backend like AWS S3, Google Cloud Storage, or Azure Blob Storage instead of Harness-managed state, you can configure a backend block in your OpenTofu files and Harness will use it directly.

This is useful if:

* You are onboarding to IaCM and already have state in an external backend.
* You need state compatibility with other CI pipelines or tools.
* You want to keep state in your own infrastructure for compliance reasons.

Go to [Use Existing Remote State](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/use-backends.md) to learn how to configure external backends for S3, GCS, and Azure Blob Storage. The guide includes example backend configurations, authentication setup, and troubleshooting steps.

Go to [Initialization](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/init-configuration.md) to learn how to use environment variables for dynamic backend configuration if you need to parameterize your backend settings.

***

## Bypass the existing state-file check on init <a href="#bypass-existing-state-file-check" id="bypass-existing-state-file-check"></a>

By default, the IaCM init step checks whether a `terraform.tfstate` file already exists in the execution directory. If one is found, the step fails to prevent you from accidentally running against a stale local state file when Harness is already managing remote state.

If you intentionally want to run init in a directory that contains a local state file — for example, during a one-time migration — set `PLUGIN_DISABLE_EXISTING_STATE_FILE_CHECK=true` as a workspace environment variable on the IaCM stage:

1. Open the workspace and select the **Connectors and Variables** tab.
2. In the **Environment Variables** panel, select **+ Add Variable**.
3. Set **Key** to `PLUGIN_DISABLE_EXISTING_STATE_FILE_CHECK` and **Value** to `true`.
4. Select **Save Changes**.

Remove the variable after the migration is complete to re-enable the check on subsequent runs.

{% hint style="warning" %}
Setting this variable bypasses a guard against running OpenTofu against a local state file instead of the Harness-managed remote state. Use it only when you intend to work with a local state file, and remove it immediately after.
{% endhint %}

***

## State locking <a href="#state-locking" id="state-locking"></a>

State locking prevents concurrent modifications to your infrastructure. When a pipeline run executes a plan or apply operation, Harness locks the workspace state. Other pipeline runs that attempt to modify the same workspace will fail fast rather than queue, protecting state consistency.

Go to [OpenTofu State Locking](https://opentofu.org/docs/language/state/locking/) to understand how OpenTofu handles state locking.

### How locks work <a href="#how-locks-work" id="how-locks-work"></a>

Locks are automatically acquired when:

* A plan operation starts.
* An apply operation starts.
* A destroy operation starts.

Locks are automatically released when the operation completes or fails. A pipeline that is aborted mid-run may not release its lock. If a workspace remains locked after an aborted run, contact [Harness Support](mailto:support@harness.io).

***

## Access state from local CLI <a href="#access-state-from-local-cli" id="access-state-from-local-cli"></a>

If you need to run OpenTofu commands locally against your Harness-managed state, you can use the HTTP backend configuration provided in the workspace.

Go to [OpenTofu HTTP Backend](https://opentofu.org/docs/language/settings/backends/http/) to understand how the HTTP backend works.

### Get the HTTP backend block <a href="#get-the-http-backend-block" id="get-the-http-backend-block"></a>

To get the HTTP backend block for local CLI access, do the following:

1. Navigate to your workspace in the **Infrastructure** module.
2. Select the **CLI Integration** tab.
3. Copy the provided backend block into a local `.tf` file (for example, `backend-local.tf`).
4. Add the file to your `.gitignore` to prevent committing it to version control.

### Authenticate to the backend <a href="#authenticate-to-the-backend" id="authenticate-to-the-backend"></a>

Set the `TF_HTTP_PASSWORD` environment variable to a Harness API key with **Workspace Access State** (`iac_workspace_accessstate`) permission on the workspace:

```bash
export TF_HTTP_PASSWORD=<your-harness-api-key>
```

Go to [API keys](https://developer.harness.io/harness-platform/use-harness-platform/automation/api/add-and-manage-api-keys) to learn how to create API keys.

### Run local commands <a href="#run-local-commands" id="run-local-commands"></a>

With the backend configured and authentication set, you can run OpenTofu commands locally:

```bash
tofu init
tofu plan
tofu apply
```

These commands will use the Harness-managed state instead of creating a local state file.

{% hint style="info" %}
Keep the HTTP backend block in a local file that is in your `.gitignore` file so you can use the Harness-managed state locally without committing backend configuration to your repository.
{% endhint %}

***

## Next steps <a href="#next-steps" id="next-steps"></a>

Now that you understand how Harness manages OpenTofu state, learn how to configure default pipelines that execute OpenTofu commands automatically.

Go to [Default pipelines for OpenTofu](https://developer.harness.io/infrastructure-as-code-management/use-iacm/iac-provisioners/opentofu/default-pipelines.md) to configure reusable pipelines for common OpenTofu operations.

{% @harness-feedback/feedback %}
