> 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/3.0/use-iacm/workspaces/manage-state/migrate-state-into-harness.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Migrate State into Harness

Move existing OpenTofu or Terraform state from S3, Terraform Cloud, or another backend into Harness IaCM with the local CLI, a pipeline, or the API

Harness Infrastructure as Code Management (IaCM) can hold the state of a workspace that you already manage with another backend. Move the state into Harness with the local command-line interface (CLI), a Harness pipeline, or the Harness API, then confirm the result on the workspace **State** tab.

{% hint style="info" %}
**OpenTofu and Terraform**

The examples on this page use the `tofu` command. Every migration method works the same way for Terraform: substitute `terraform` for `tofu` in the local commands. In a pipeline, the provisioner set on the workspace determines which binary runs.
{% endhint %}

***

## Before you begin

**Setup requirement:** Optional.

You need the following to migrate state into Harness:

* **An existing workspace:** Migration targets a workspace that already exists in Harness. Go to [Create a Workspace](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/set-up/create-a-workspace.md) to create one.
* **Access State permission:** A Harness API key whose role grants **Access State** (`iac_workspace_accessstate`) on the target workspace. Go to [Configure Workspace Permissions](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/govern/configure-workspace-permissions.md) to assign it, and go to [Manage roles](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/platform-access-control/add-manage-roles.md) to update a role.
* **A Harness API key:** Required by the local CLI and API methods. Go to [Add and manage API keys](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/automation/api/add-and-manage-api-keys.md) to create one.
* **OpenTofu or Terraform CLI:** Installed locally for every method except the pipeline method.
* **Access to your current backend:** Credentials that let you read the existing state, so the CLI can initialize against it before the migration.

***

### Choose a migration method

Use the local CLI for direct control over the migration, a Harness pipeline when the workspace can already authenticate to the existing backend, and the API when you have a state file on disk. Each tab describes one method.

{% tabs %}
{% tab title="Local CLI" %}
Configure your OpenTofu or Terraform code locally to use the Harness backend, then let OpenTofu move the state.

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

1. Set authentication for your existing backend, then run `tofu init` until the project initializes successfully. If the state lives in Terraform Cloud, complete [Pull state out of Terraform Cloud](#pull-state-out-of-terraform-cloud) first.
2. In Harness, open the workspace, then select the **CLI Integration** tab.
3. Copy the HTTP backend block into your code, and remove the existing `backend` block. OpenTofu and Terraform allow only one `backend` block, so replace the old block instead of adding a second one.
4. 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>
   ```
5. Run the migration from the existing backend into Harness:

   ```bash
   tofu init -migrate-state
   ```

The HTTP backend block on the **CLI Integration** tab is prepopulated with your workspace location and identifier.
{% endtab %}

{% tab title="Harness Pipeline" %}
Run a successful apply in an IaCM pipeline with your existing backend configuration still in place. During an apply, Harness takes a snapshot of the state into Harness, which populates the **State** and **Resources** tabs of the workspace.

To migrate state with a pipeline, do the following:

1. Configure the workspace with everything an apply needs, including the credentials that authenticate to your existing backend. Set those credentials as workspace environment variables.
2. Run the pipeline and let the apply step complete.
3. Open the **State** tab of the workspace to confirm that Harness copied the state.
4. Remove the existing `backend` block from your OpenTofu code.

The next time IaCM runs the code, Harness finds no `backend` block and loads the Harness-stored state automatically.

Go to [Add Workspace Variables & Variable Files](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/set-up/add-workspace-variables-and-variable-files.md) to add the backend credentials as environment variables.
{% endtab %}

{% tab title="Harness API" %}
Load an existing state file directly into an existing workspace with one API request. The example assumes a `terraform.tfstate` file in the current directory. To pull remote state into that file, run `tofu state pull > terraform.tfstate`.

The request takes the following values:

* **`HARNESS_ACCOUNT_ID`, `HARNESS_ORG_ID`, `HARNESS_PROJECT_ID`, `HARNESS_WORKSPACE_ID`:** Identifiers of 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 **Access State** on that workspace.

```bash
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 Harness accepted the state. Because Harness versions state, send this request only to a workspace whose current state you are ready to replace. If the workspace has already run an apply, review the existing version on the **State** tab first.
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**The migrate-state plugin command**

The `migrate-state` command of the OpenTofu/Terraform Plugin step copies the state from the backend the workspace uses into the backend your configuration declares. Its documented use is moving a workspace off Harness-managed state. Go to [Migrate State Out of Harness](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/manage-state/migrate-state-out-of-harness.md) to use it.
{% endhint %}

### Pull state out of Terraform Cloud

Terraform Cloud 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 into a local backend first, then follow the **Local CLI** method.

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 the state into a local file:

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

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

Your project now uses a local backend that holds the content of your Terraform Cloud state. Continue with step 2 of the **Local CLI** method. Go to the [OpenTofu state command reference](https://opentofu.org/docs/cli/commands/state/) to review the state commands used here.

### Verify the migrated state

Open the **State** tab of the workspace in Harness and confirm that it shows the migrated state. Harness versions every state change, so the tab lists the migrated state as a version you can return to.

After the state is in Harness, prepare your code for IaCM runs:

* **Remove the backend block:** Delete the HTTP backend block or the old `backend` block from the code the workspace uses. A committed `backend` block overrides the workspace-managed backend during pipeline runs.
* **Keep local access, if you need it:** Store the IaCM HTTP backend block in a local file listed in your `.gitignore` file, so you can keep using the Harness state locally.

Go to [Use the Workspace as a Backend from a Local CLI](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/plan-and-apply/use-the-workspace-as-a-backend-from-a-local-cli.md) to keep working with the migrated state from your machine.

***

## Troubleshooting

<details>

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

The API key in `TF_HTTP_PASSWORD` has expired, or its role does not grant **Access State** on the target workspace. Set a valid key whose role includes that permission, then run `tofu init` again.

</details>

<details>

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

Keep exactly one `backend` block in your configuration, delete the `.terraform` directory, then retry. Run `tofu init -reconfigure` only if you intend to discard the record of the previous backend.

</details>

<details>

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

A running pipeline on the workspace holds the state lock. Wait for the execution to finish, then retry the migration.

</details>

<details>

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

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

</details>

<details>

<summary>The init step fails because a terraform.tfstate file exists in the execution directory</summary>

The IaCM init step fails by default when it finds a local `terraform.tfstate` file. Go to [Clear a Stuck State Lock](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/manage-state/clear-a-stuck-state-lock.md) to bypass this check for a one-time migration.

</details>

***

## Next steps

Your state now lives in Harness-managed storage, versioned and locked by Harness. Continue with one of these tasks:

* [Use Harness-Managed State](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/manage-state/use-harness-managed-state.md): Review and download state versions on the **State** tab.
* [Provision a Workspace with a Pipeline](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/plan-and-apply/provision-a-workspace-with-a-pipeline.md): Run plan and apply against the migrated state.
* [State & Backends](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/manage-state/state-and-backends.md): Review how Harness encrypts, versions, and locks state.
