> 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/remote-backends/state-migration.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# State Migration

Migrate OpenTofu and Terraform state into Harness IaCM, or move it out to an external backend you control.

When you use Harness Infrastructure as Code Management (IaCM), one of its benefits is automatic state storage for your workspace. Harness state is used automatically when no backend code block is configured in your OpenTofu or Terraform code. You can also use the Harness-stored state locally or in other workflows through an HTTP backend block configured for your workspace location and identifier.

If you currently use third-party state storage, you can [continue to use that backend as configured](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/use-backends.md) with Harness IaCM. If you want Harness to hold the state instead, migrate it with the local CLI, a Harness pipeline, or the Harness API.

If you need to move state in the other direction, out of Harness-managed storage and into an external backend you control, go to [Migrate state out of Harness](#migrate-from-harness-managed-state-to-an-external-backend).

{% hint style="info" %}
**OPENTOFU AND TERRAFORM**

The examples on this page use the `tofu` command. Every migration path on this page works the same way for Terraform. For the local CLI paths, substitute `terraform` for `tofu` in the commands. For the pipeline paths, the step is the same either way, and the provisioner configured on the workspace determines which binary runs.
{% endhint %}

***

## Before you begin <a href="#before-you-begin" id="before-you-begin"></a>

* **Existing workspace:** Migration targets a workspace that already exists in Harness. Go to [Create a workspace](https://developer.harness.io/infrastructure-as-code-management/platform/workspaces/create-workspace.md) to create one.
* **State access permission:** You need a Harness API key with **Workspace Access State** (`iac_workspace_accessstate`) on the target workspace. Go to the [Permissions reference](https://developer.harness.io/harness-platform/use-harness-platform/platform-access-control/permissions-reference#infrastructure-as-code) to review IaCM workspace permissions. An administrator must assign a role that includes it. Go to [RBAC in Harness](https://developer.harness.io/harness-platform/use-harness-platform/platform-access-control) and [Manage roles](https://developer.harness.io/harness-platform/use-harness-platform/platform-access-control/add-manage-roles) to configure roles.
* **Harness API key:** Required by the local CLI and API methods. Go to [Add and manage API keys](https://developer.harness.io/harness-platform/use-harness-platform/automation/api/add-and-manage-api-keys) to create one.
* **OpenTofu or Terraform CLI:** Required locally for every method except the pipeline method.
* **Access to your current backend:** Credentials that let you read your existing state, so the CLI can initialize against it before the migration.

***

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

Harness supports three ways to move existing state into Harness-managed storage. Use the local CLI when you want direct control over the migration, a Harness pipeline when your workspace can already authenticate to the existing backend, and the API when you have a state file on disk.

Harness versions every state change, so you can review the result of any method in the workspace **State** tab. Go to [State management](https://developer.harness.io/infrastructure-as-code-management/use-iacm/iac-provisioners/opentofu/state-management.md) to understand how Harness stores and versions state.

### Migrate with the local CLI <a href="#migrate-with-the-local-cli" id="migrate-with-the-local-cli"></a>

Migrate your state files to Harness IaCM by configuring your OpenTofu or Terraform codebase locally.

To migrate state with the local CLI, do the following:

1. Set authentication for your existing backend configuration and initialize your project successfully with `tofu init`. If your existing state lives in Terraform Cloud, go to [Migrate from Terraform Cloud](#migrate-from-terraform-cloud) first.
2. Go to the workspace in Harness and select the **CLI Integration** tab, then copy the HTTP backend block shown there into your OpenTofu code and remove the existing backend block. OpenTofu and Terraform allow only one backend block, so replace the old block rather than adding a second one.
3. Set the `TF_HTTP_PASSWORD` environment variable to your Harness API key so the CLI can authenticate to the IaCM backend:

   ```bash
   export TF_HTTP_PASSWORD=<your-harness-api-key>
   ```
4. Run `tofu init -migrate-state` to move your state from the existing backend into Harness.

<figure><img src="https://3575326923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ffhx6XzjvtJmfv5gPWZ5m%2Fuploads%2Fgit-blob-3102fde8133fdd82f1793d40acef70cb6fafe0ad%2Fworkspace-cli-integration.png?alt=media" alt="The CLI Integration tab of a Harness IaCM workspace showing the generated HTTP backend block"><figcaption><p>Click to view full size</p></figcaption></figure>

*The CLI Integration tab provides the HTTP backend block, prepopulated with your workspace location and identifier*

To validate the migration, open the workspace **State** tab in Harness and confirm the state is populated.

Once the state is in Harness, remove the backend block from your codebase to prepare the code for execution in IaCM. You can also keep the IaCM HTTP backend block in a local file listed in your `.gitignore` file, so you can keep using the Harness state locally. A committed backend block overrides the workspace-managed backend during pipeline execution, so it must not be checked in.

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

Terraform Cloud (TFC) does not support a direct `tofu init -migrate-state` conversion to third-party backends such as Harness IaCM. Pull the state out of Terraform Cloud first, then run the local CLI migration against that local state.

To move Terraform Cloud state to a local backend, do the following:

1. Initialize your project successfully against your Terraform Cloud remote state.
2. Pull your state locally:

   ```bash
   tofu state pull > terraform.tfstate
   ```
3. Remove the Terraform Cloud backend block from your code.
4. Delete your current settings by removing the `.terraform` directory, then reinitialize the project:

   ```bash
   rm -rf .terraform
   tofu init
   ```

At this point you are using a local backend that holds the content of your Terraform Cloud state, and you can continue with [Migrate with the local CLI](#local-cli).

Go to the [OpenTofu state command reference](https://opentofu.org/docs/cli/commands/state/) to review the state inspection and manipulation commands used here.

### Migrate with a Harness pipeline <a href="#migrate-with-a-harness-pipeline" id="migrate-with-a-harness-pipeline"></a>

Another strategy is to run a successful `tofu apply` in an IaCM pipeline **with your existing backend configuration still set**. During an apply, Harness takes a snapshot of your state into Harness, which is what populates the **State** and **Resources** tabs in the workspace.

To migrate state with a pipeline, do the following:

1. [Configure your workspace](https://developer.harness.io/infrastructure-as-code-management/platform/workspaces/create-workspace.md) with everything needed to run an `apply`, including the credentials that authenticate to your existing backend. Set those credentials as workspace environment variables. Go to [Connectors and variables](https://developer.harness.io/infrastructure-as-code-management/platform/iacm-connectors-variables/connectors-variables.md) to add them.
2. Run the pipeline and let the `apply` step complete.
3. Open the **State** tab in the target workspace to confirm the state was copied into Harness.
4. Remove the existing backend block from your OpenTofu code.

The next time IaCM executes the code, Harness sees that no backend is configured and loads the Harness-stored state automatically.

### Migrate with the Harness API <a href="#migrate-with-the-harness-api" id="migrate-with-the-harness-api"></a>

You can use the Harness API to load an existing state file directly into an existing workspace. The example below assumes a `terraform.tfstate` file in the current directory.

{% hint style="info" %}
A handy command to pull your remote state locally is `tofu state pull > terraform.tfstate`.
{% endhint %}

The request takes the following values:

* **`HARNESS_ACCOUNT_ID`, `HARNESS_ORG_ID`, `HARNESS_PROJECT_ID`, `HARNESS_WORKSPACE_ID`:** Identifiers for the target workspace. All four appear in the workspace URL in Harness, and in the address of the backend block on the workspace **CLI Integration** tab.
* **`HARNESS_PLATFORM_API_KEY`:** A Harness API key with **Workspace Access State** on that workspace.

```shell
HARNESS_ACCOUNT_ID=some_account_id
HARNESS_PLATFORM_API_KEY=some_api_token
HARNESS_ORG_ID=some_org
HARNESS_PROJECT_ID=some_project
HARNESS_WORKSPACE_ID=some_workspace

curl -isX POST --fail-with-body \
  "https://app.harness.io/gateway/iacm/api/orgs/${HARNESS_ORG_ID}/projects/${HARNESS_PROJECT_ID}/workspaces/${HARNESS_WORKSPACE_ID}/terraform-backend?accountIdentifier=${HARNESS_ACCOUNT_ID}" \
  -H "x-api-key: ${HARNESS_PLATFORM_API_KEY}" \
  -H "Content-Type: application/json" \
  --data-binary "@terraform.tfstate"
```

Use `--data-binary` rather than `-d`, because `-d` strips newlines from the file and sends it as form-encoded content instead of JSON.

A `2xx` response means the state was accepted. Open the workspace **State** tab to confirm the contents. Because Harness versions state, run this call against a workspace whose current state you are ready to replace, and review the existing version in the **State** tab first if the workspace has already run an apply.

***

## Migrate state out of Harness <a href="#migrate-state-out-of-harness-migrate-from-harness-managed-state-to-an-external-backend" id="migrate-state-out-of-harness-migrate-from-harness-managed-state-to-an-external-backend"></a>

To move state from an IaCM-managed backend to an external backend such as an Azure Storage Account, AWS S3, or GCS bucket, use the OpenTofu or Terraform native backend migration. Harness exposes each workspace's state as a standard HTTP backend, so the native migration treats the move as an ordinary backend-to-backend transfer.

You can run that migration two ways:

* **In a Harness pipeline:** Declare the external backend in your IaC code, then run the `migrate-state` command in an OpenTofu/Terraform Plugin step. Harness copies the state from its own backend into the backend your code declares. Use this path when the pipeline can already authenticate to the external backend.
* **Locally with the CLI:** Initialize against the Harness backend, replace the backend block, and run `tofu init -migrate-state`. Use this path when you want to inspect and back up the state yourself before the transfer.

Both paths produce the same result, and the [Before you begin](#before-you-begin-out) checks apply to both.

### Before you begin <a href="#before-you-begin-out" id="before-you-begin-out"></a>

* **Confirm the workspace is up to date.** Run the provision pipeline and verify that the plan reports no changes. This ensures that the migrated state matches your deployed infrastructure.
* **Create the external backend.** Verify that the bucket or storage account already exists, and that you can authenticate to it from wherever you run the migration, either the pipeline or your machine.
* **Plan a maintenance window.** Schedule the migration for a period when no other pipeline runs against the workspace.

### Migrate with a Harness pipeline <a href="#migrate-out-with-a-pipeline" id="migrate-out-with-a-pipeline"></a>

To migrate state out of Harness with a pipeline, do the following:

1. Add a `backend` block for your external backend to your IaC code and commit it. Go to [Use Existing Remote State](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/use-backends.md) to review example blocks per provider. OpenTofu and Terraform allow only one backend block, so replace any existing block rather than adding a second one.
2. Set the backend credentials as workspace environment variables so the pipeline can authenticate to the external backend. Go to [Connectors and variables](https://developer.harness.io/infrastructure-as-code-management/platform/iacm-connectors-variables/connectors-variables.md) to add them, and to [Dynamic backend configuration](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/init-configuration.md) to pass backend values through environment variables instead of hardcoding them.
3. Open the pipeline in the editor, add an **OpenTofu/Terraform Plugin** step to your IaCM stage, and select **migrate-state** from the **Command** dropdown. Go to [Migrate state](https://developer.harness.io/infrastructure-as-code-management/use-iacm/iacm-cli-commands/terraform-plugins.md#migrate-state) to review what the command does.
4. Run the pipeline and let the `migrate-state` step complete.
5. Verify the migration. Run a plan against the workspace and confirm that it reports no changes. A plan that proposes to create your whole infrastructure means the run initialized against an empty backend and the state did not transfer.

### Migrate with the local CLI <a href="#migrate-out-with-the-local-cli" id="migrate-out-with-the-local-cli"></a>

To migrate state out of Harness with the local CLI, do the following:

1. Go to the workspace's **CLI Integration** tab and copy the `backend "http"` block into a local `.tf` file.
2. Set `TF_HTTP_PASSWORD` to your Harness API key, then run `tofu init` to initialize against the Harness backend.
3. Back up the state before you change backends:

   ```bash
   tofu state pull > terraform.tfstate
   ```

   This writes a plaintext copy of the state to local disk. If your state contains sensitive values, protect that file as you would any other secret, and delete it after you verify the migration in step 6.

   `tofu init -migrate-state` transfers the state between the two backends without creating this local file. To avoid creating a local copy, skip this step and use the version history in the workspace **State** tab as your rollback path.
4. Replace the `backend "http"` block with a `backend` block for your external backend. Go to [Use Existing Remote State](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/use-backends.md) to review example blocks per provider. OpenTofu and Terraform allow only one backend block, so remove the HTTP block rather than adding a second one.
5. Run `tofu init -migrate-state` to copy the state into the external backend. When OpenTofu asks whether to copy the existing state, confirm the migration. Add `-force-copy` to confirm the migration automatically in a non-interactive shell.
6. Verify the migration. Run `tofu state list` against the external backend and check that your resources are present. Then run `tofu plan` and check that it reports no changes. Delete the local backup from step 3.
7. Commit the new `backend` block to your repository.
8. Set the backend credentials as workspace environment variables so pipeline runs can authenticate to the external backend. Go to [Use Existing Remote State](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/use-backends.md) to review the required credentials for your provider. Go to [Dynamic backend configuration](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/init-configuration.md) to pass backend values through environment variables instead of hardcoding them.

### Behavior after migration <a href="#behavior-after-migration" id="behavior-after-migration"></a>

The migration moves the source of truth for your workspace state, and it moves responsibility for locking with it.

|                  | State source of truth | State locking      |
| ---------------- | --------------------- | ------------------ |
| Before migration | Harness backend       | Harness state lock |
| After migration  | External backend      | External backend   |

After you commit the external `backend` block, Harness initializes the workspace against that backend instead of its own HTTP backend. The `init` command does not add `-migrate-state` or `-reconfigure` on its own, which is why the transfer needs either the `migrate-state` command step or the local CLI migration. Any `-backend-config` flags you provide through workspace environment variables are included in the command.

If you migrated with the local CLI, the first pipeline run after the backend block changes may fail because the working directory still records the previous backend. Set the `PLUGIN_INIT_RECONFIGURE` workspace environment variable to add `-reconfigure` to that run, and remove it after the run succeeds. Go to [Backend reconfiguration](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/init-configuration.md#backend-reconfiguration) to review how this variable affects backend initialization and when to use it.

IaCM continues to snapshot state into Harness after the migration. The **State** and **Resources** tabs remain populated, while the external backend remains the source of truth for the workspace state.

The external backend manages state locking after the migration. When Harness detects an external backend in your configuration, it stops applying its own state lock and relies on the locking mechanism the external backend provides. Verify that locking is configured on the external backend to protect against concurrent runs. Go to [Use Existing Remote State](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/use-backends.md) to review how each provider implements locking.

***

## Manage IaCM workspaces with the Harness Terraform provider <a href="#manage-iacm-workspaces-with-the-harness-terraform-provider" id="manage-iacm-workspaces-with-the-harness-terraform-provider"></a>

You can use the [Harness Terraform provider](https://registry.terraform.io/providers/harness/harness/latest/docs) to create and manage IaCM workspaces as code, rather than configuring them through the Harness UI. The provider exposes the `harness_platform_workspace` resource, which maps to the same workspace schema the IaCM API uses.

This pattern is useful when you want to version-control your workspace configuration, replicate workspaces across environments, or create workspaces as part of a bootstrapping pipeline.

### Example: create a workspace and import its state <a href="#example-create-a-workspace-and-import-its-state" id="example-create-a-workspace-and-import-its-state"></a>

```hcl
terraform {
  required_providers {
    harness = {
      source  = "harness/harness"
      version = "~> 0.30"
    }
  }
}

provider "harness" {
  endpoint   = "https://app.harness.io/gateway"
  account_id = var.harness_account_id
  platform_api_key = var.harness_api_key
}

resource "harness_platform_workspace" "my_workspace" {
  name             = "my-workspace"
  identifier       = "my_workspace"
  org_id           = var.org_id
  project_id       = var.project_id
  provisioner_type = "opentofu"
  provisioner_version = "1.9.0"
  cost_estimation_enabled = false

  repository           = "my-org/my-infra-repo"
  repository_branch    = "main"
  repository_path      = "infra/app"
  repository_connector = "account.github_connector"

  provider_connector = "account.aws_oidc"
}
```

After Terraform creates the workspace, use the Harness API or the local CLI migration steps to load your existing state file into the workspace. Go to [Migrate with the Harness API](#migrate-with-the-harness-api) for the `curl` command.

Go to the [harness\_platform\_workspace documentation](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_workspace) on the Terraform Registry to review all supported fields.

***

## Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

<details>

<summary>Harness IaCM HTTP backend returns 401 or 403 when running tofu init with TF_HTTP_PASSWORD set</summary>

Confirm TF\_HTTP\_PASSWORD holds a valid Harness API key that has not expired, and that the key's role grants Workspace Access State (iac\_workspace\_accessstate) on the target workspace.

</details>

<details>

<summary>tofu init -migrate-state fails with a backend configuration changed error</summary>

Run tofu init -reconfigure only if you intend to discard the record of the previous backend. To migrate state, keep exactly one backend block in your configuration and delete the .terraform directory before retrying.

</details>

<details>

<summary>The first pipeline run after switching to an external backend fails during init</summary>

The working directory still records the previous backend. Set the PLUGIN\_INIT\_RECONFIGURE workspace environment variable to true so Harness adds -reconfigure to that init, then remove the variable after the run succeeds. Migrate the state to the external backend first, because -reconfigure discards the record of the previous backend without copying any state.

</details>

<details>

<summary>Error acquiring the state lock while migrating state into a Harness IaCM workspace</summary>

Wait for any running pipeline on the workspace to finish before migrating, because it holds the state lock. Retry the migration once the execution completes.

</details>

<details>

<summary>tofu state pull from Terraform Cloud returns empty or partial state</summary>

Confirm you are authenticated to the correct Terraform Cloud organization and workspace, and that the project initialized successfully against the cloud backend before pulling.

</details>

***

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

Your state now lives where you intend it to, either in Harness-managed storage or in a backend you control. Configure the pipeline that will operate against it next.

* [Use Existing Remote State](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/use-backends.md): Configure S3, GCS, or Azure Blob Storage as your backend.
* [Dynamic backend configuration](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/init-configuration.md): Pass backend values through environment variables at init time.
* [State management](https://developer.harness.io/infrastructure-as-code-management/use-iacm/iac-provisioners/opentofu/state-management.md): Review how Harness encrypts, versions, and locks state.

## FAQs <a href="#faqs" id="faqs"></a>

<details>

<summary>How do I move Harness-managed state to S3?</summary>

Declare the S3 backend in your configuration, then run the `migrate-state` command on an OpenTofu/Terraform Plugin step. Harness copies state from its HTTP backend into the backend your code declares. You can also initialize against the Harness backend locally, replace the backend block, and run `tofu init -migrate-state`. Confirm the plan reports no changes before you migrate. After the backend block changes, the first pipeline init may still record the previous backend. Set `PLUGIN_INIT_RECONFIGURE` for that run, then remove it. Go to [Backend reconfiguration](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/init-configuration.md#backend-reconfiguration) to review that variable.

</details>

{% @harness-feedback/feedback %}
