For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content
IaCMState

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 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.

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.


Before you begin

  • Existing workspace: Migration targets a workspace that already exists in Harness. Go to Create a workspace 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 to review IaCM workspace permissions. An administrator must assign a role that includes it. Go to RBAC in Harness and Manage roles to configure roles.

  • Harness API key: Required by the local CLI and API methods. Go to 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

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 to understand how Harness stores and versions state.

Migrate with the local CLI

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 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:

  4. Run tofu init -migrate-state to move your state from the existing backend into Harness.

The CLI Integration tab of a Harness IaCM workspace showing the generated HTTP backend block
Click to view full size

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

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:

  3. Remove the Terraform Cloud backend block from your code.

  4. Delete your current settings by removing the .terraform directory, then reinitialize the project:

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.

Go to the OpenTofu state command reference to review the state inspection and manipulation commands used here.

Migrate with a Harness pipeline

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 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 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

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.

A handy command to pull your remote state locally is tofu state pull > terraform.tfstate.

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.

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

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 checks apply to both.

Before you begin

  • 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

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 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 to add them, and to Dynamic backend configuration 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 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

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:

    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 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 to review the required credentials for your provider. Go to Dynamic backend configuration to pass backend values through environment variables instead of hardcoding them.

Behavior after migration

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 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 to review how each provider implements locking.


Manage IaCM workspaces with the Harness Terraform provider

You can use the Harness Terraform provider 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

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 for the curl command.

Go to the harness_platform_workspace documentation on the Terraform Registry to review all supported fields.


Troubleshooting

Harness IaCM HTTP backend returns 401 or 403 when running tofu init with TF_HTTP_PASSWORD set

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.

tofu init -migrate-state fails with a backend configuration changed error

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.

The first pipeline run after switching to an external backend fails during init

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.

Error acquiring the state lock while migrating state into a Harness IaCM workspace

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.

tofu state pull from Terraform Cloud returns empty or partial state

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


Next steps

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.

FAQs

How do I move Harness-managed state to S3?

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 to review that variable.

Last updated

Was this helpful?