> 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/help-and-reference/reference/opentofu-and-terraform-plugin-commands.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# OpenTofu & Terraform Plugin Commands

OpenTofu and Terraform plugin commands in IaCM pipelines, from init, plan and apply to detect-drift, import and migrate-state, with the PLUGIN\_ flags they read

An IaCM plugin step runs one OpenTofu or Terraform command against a workspace during a pipeline. Infrastructure as Code Management (IaCM) supports 13 commands, from `init`, `plan` and `apply` to Harness-specific operations such as `detect-drift` and `migrate-state`.

When you create a provision pipeline, Harness adds the `init`, `plan` and `apply` commands for you. You add other command steps yourself to validate code, detect drift, bring existing resources under management, or move state between backends. This page lists every command, what it reads, how it affects the pipeline, and the `PLUGIN_` variables that change its flags.

***

## What you will learn from this topic

* **Command reference:** What each of the 13 plugin commands does and how it affects the pipeline.
* **Binary and version requirements:** Which binary a step runs and the minimum provisioner version each command needs.
* **Plan flags:** The `PLUGIN_` variables that set `plan` flags, and the `TF_CLI_ARGS_*` variables the steps read or ignore.
* **Command examples:** The configuration blocks and step YAML that the `import`, `removed` and `plan` commands use.

***

## Before you begin

This reference assumes you know how IaCM runs OpenTofu and Terraform in a pipeline. Go to [IaCM Pipelines, Stages & Plugin Steps](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/plan-and-apply/iacm-pipelines-stages-and-plugin-steps.md) to review how stages and plugin steps fit together. Go to [Operations: Init, Plan, Apply, Destroy & Refresh](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/plan-and-apply/operations-init-plan-apply-destroy-and-refresh.md) to review the operations these commands make up.

### Command reference

Each plugin step runs one command. The following table lists every command, what it does, and how it affects the pipeline:

| Command              | What it does                                                                                                                 | Pipeline behavior                                                                                            |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| `init`               | Initializes the backend, installs providers and downloads modules into the working directory.                                | Must run before any other command in the stage.                                                              |
| `plan`               | Refreshes state, compares it with the configuration, and lists the changes `apply` would make.                               | Succeeds when it finds changes. Fails only on an error, such as invalid configuration or a missing provider. |
| `apply`              | Runs the planned changes and updates the state file.                                                                         | Fails when the plan cannot run, for example because of provider errors or missing credentials.               |
| `destroy`            | Removes every resource the workspace manages, in reverse dependency order.                                                   | Fails when a resource cannot be deleted, for example because of a dependency or a provider error.            |
| `plan-destroy`       | Lists the resources `destroy` would remove, without removing them.                                                           | Makes no changes. Use it to review the impact before you run `destroy`.                                      |
| `plan-refresh-only`  | Plans a state update that matches the real infrastructure, without applying pending code changes.                            | Makes no infrastructure changes.                                                                             |
| `apply-refresh-only` | Writes the refreshed state from `plan-refresh-only` to the state file.                                                       | Makes no infrastructure changes. It only updates the Harness state file.                                     |
| `detect-drift`       | Compares the real infrastructure with the state and reports differences, known as drift. It runs `plan` with specific flags. | Fails the pipeline when it finds drift.                                                                      |
| `import`             | Brings existing resources under management from `import` blocks in your code, without re-creating them.                      | Runs as a pipeline step, so you do not import outside Harness.                                               |
| `removed`            | Stops managing resources declared in `removed` blocks, and keeps them running when `destroy = false`.                        | Runs as a pipeline step, so you do not edit state outside Harness.                                           |
| `migrate-state`      | Copies the state from the backend the workspace uses into the backend your code declares, like `init -migrate-state`.        | Run it before any `init` against the new backend.                                                            |
| `validate`           | Checks the configuration for syntax errors, unresolved references and missing arguments.                                     | Fails the pipeline on any error, so invalid code does not reach `plan` or `apply`.                           |
| `fmt`                | Rewrites `.tf` and `.tfvars` files in the working directory to the canonical style.                                          | Succeeds even when it reformats files, unless `TF_CLI_ARGS_fmt` is set to `-check`.                          |

`detect-drift` is not an OpenTofu or Terraform command. It is a Harness operation that you typically run on a schedule or before a provision.

{% hint style="info" %}
**Add a command step to a pipeline**

To add a command step, open the pipeline in the editor, add a step to the IaCM stage, then select **OpenTofu/Terraform Plugin**. Select the command from the **Command** dropdown.
{% endhint %}

### Binary selection

The commands are the same for both provisioners, but each workspace runs its own binary. Harness selects it from the workspace provisioner:

* **OpenTofu workspace:** Plugin steps run the `tofu` binary.
* **Terraform workspace:** Plugin steps run the `terraform` binary.

The step type is the same for both provisioners. The following example shows the `init`, `plan` and `apply` steps of a provision stage:

```yaml
execution:
  steps:
    - step:
        type: IACMTerraformPlugin
        name: init
        identifier: init
        spec:
          command: init
    - step:
        type: IACMTerraformPlugin
        name: plan
        identifier: plan
        spec:
          command: plan
    - step:
        type: IACMTerraformPlugin
        name: apply
        identifier: apply
        spec:
          command: apply
```

### Version requirements

Most commands work with every supported provisioner version. The `import` and `removed` blocks need a minimum version:

| Command                                                                   | Minimum OpenTofu version | Minimum Terraform version                                              |
| ------------------------------------------------------------------------- | ------------------------ | ---------------------------------------------------------------------- |
| `init`, `plan`, `apply`, `destroy`                                        | 1.6                      | 1.0                                                                    |
| `plan-destroy`, `plan-refresh-only`, `apply-refresh-only`, `detect-drift` | 1.6                      | 1.0                                                                    |
| `validate`, `fmt`                                                         | 1.6                      | 1.0                                                                    |
| `import` (block syntax)                                                   | 1.6                      | 1.5                                                                    |
| `removed` (block syntax)                                                  | 1.7                      | Not available in IaCM (needs 1.7; IaCM supports Terraform up to 1.5.x) |

{% hint style="warning" %}
**Terraform 1.6 and later is not supported**

IaCM supports MPL-licensed Terraform up to 1.5.x. BSL-licensed releases from 1.6.0 are not supported, so use OpenTofu where you need the `removed` block.
{% endhint %}

Go to [What's Supported](https://developer.harness.io/infrastructure-as-code-management/3.0/start-here/get-started/whats-supported.md) to review the supported provisioner versions.

### Network requirements

During `init`, OpenTofu downloads providers from the OpenTofu registry and modules from the sources your configuration references. Make sure the build infrastructure can reach the registry and every module or provider source.

Where outbound access is not available, use a custom plugin image with the providers already installed, or a private module registry. Go to [Build & Use a Custom Plugin Image](https://developer.harness.io/infrastructure-as-code-management/3.0/automation-and-self-hosting/self-hosted-and-restricted-environments/build-infrastructure/build-and-use-a-custom-plugin-image.md) to add providers to an image. Go to [Run IaCM in an Air-Gapped Environment](https://developer.harness.io/infrastructure-as-code-management/3.0/automation-and-self-hosting/self-hosted-and-restricted-environments/restricted-networks/run-iacm-in-an-air-gapped-environment.md) to plan for restricted networks.

### Plan flags

The `plan` step builds its command line from `PLUGIN_` variables, not from `TF_CLI_ARGS_plan`. Set these as workspace environment variables or step environment variables to override a flag:

| Variable                   | CLI flag          | Default | What it does                                                                 |
| -------------------------- | ----------------- | ------- | ---------------------------------------------------------------------------- |
| `PLUGIN_LOCK`              | `-lock=`          | `true`  | Turns state locking on or off for the operation.                             |
| `PLUGIN_LOCK_TIMEOUT`      | `-lock-timeout=`  | Not set | Sets how long to retry acquiring a lock before failing.                      |
| `PLUGIN_PLAN_REFRESH`      | `-refresh=`       | `true`  | Refreshes state before planning. Set it to `false` to skip the refresh.      |
| `PLUGIN_PLAN_REFRESH_ONLY` | `-refresh-only`   | `false` | Plans a state refresh only, with no infrastructure changes.                  |
| `PLUGIN_PLAN_REPLACE`      | `-replace=`       | `[]`    | Forces replacement of resources by address prefix. Accepts several prefixes. |
| `PLUGIN_PLAN_TARGET`       | `-target=`        | `[]`    | Limits the plan to resources by address prefix. Accepts several prefixes.    |
| `PLUGIN_PLAN_DESTROY`      | `-destroy`        | `false` | Plans the destruction of every managed resource.                             |
| `PLUGIN_OUT`               | `-out=`           | Not set | Sets the path where the plan file is saved.                                  |
| `PLUGIN_STATE`             | `-state=`         | Not set | Sets the path of a state file to use for the plan.                           |
| `PLUGIN_ALLOW_DEFERRAL`    | `-allow-deferral` | Not set | Allows deferred changes in the plan output.                                  |

The **target** and **replace** fields in the plan step's **Optional Configuration** section set `PLUGIN_PLAN_TARGET` and `PLUGIN_PLAN_REPLACE`. For example, `target=tfcode.bucket1` targets one resource. When the `plan` step uses a target or a replacement, the following `apply` or `destroy` step keeps the same constraint.

The following example turns off the state refresh on a `plan` step with a step environment variable:

```yaml
- step:
    type: IACMTerraformPlugin
    name: plan
    identifier: plan
    spec:
      command: plan
      envVariables:
        PLUGIN_PLAN_REFRESH: "false"
```

To run several pipelines in sequence that each change one resource, give each pipeline its own `PLUGIN_PLAN_TARGET`. The `apply` or `destroy` step then changes only the targeted resources.

### Parallelism and other flag variables

Two more variables change how the commands run:

* **`PLUGIN_TF_PARALLELISM`:** Sets the parallelism of the `apply` step. The value is an integer from 1 to 10. A value outside that range falls back to `10`, and `1` forces serial resource operations.
* **`TF_CLI_ARGS_init` and `TF_CLI_ARGS_validate`:** IaCM appends their values to the `init` and `validate` commands. For example, `TF_CLI_ARGS_init=-reconfigure` forces backend reconfiguration.

{% hint style="warning" %}
**TF\_CLI\_ARGS\_plan has no effect**

The `plan` step ignores `TF_CLI_ARGS_plan` and every other `TF_CLI_ARGS_*` variable except `TF_CLI_ARGS_fmt`, with no error. Set the matching `PLUGIN_` variable instead, for example `PLUGIN_PLAN_REFRESH=false`.
{% endhint %}

Go to [Environment Variables Read by IaCM](https://developer.harness.io/infrastructure-as-code-management/3.0/help-and-reference/reference/environment-variables-read-by-iacm.md) to review every `TF_` variable the plugin reads, and go to [Plugin Environment Variables](https://developer.harness.io/infrastructure-as-code-management/3.0/help-and-reference/reference/plugin-environment-variables/plugin-environment-variables.md) to review every `PLUGIN_` variable.

### Configuration examples

The `import` and `removed` commands read blocks you declare in your code. Select a command to view its block:

{% tabs %}
{% tab title="import" %}
The following block brings an existing S3 bucket under management:

```hcl
import {
  to = aws_s3_bucket.my_bucket
  id = "my-existing-bucket"
}
```

`aws_s3_bucket.my_bucket` is the resource address in your code, and `my-existing-bucket` is the bucket identifier in AWS. The `import` step links the bucket to your code without destroying and re-creating it.
{% endtab %}

{% tab title="removed" %}
The following block removes an EC2 instance from the state file and keeps it running:

```hcl
removed {
  from = aws_instance.<instance_name>
  lifecycle {
    destroy = false
  }
}
```

The `lifecycle { destroy = false }` block keeps the resource in your cloud account. Without the `lifecycle` block, or with `destroy = true`, the `removed` step can destroy the resource.
{% endtab %}
{% endtabs %}

Go to [Import, Remove & Move Resources](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/plan-and-apply/import-remove-and-move-resources.md) to run the full import and removal workflow.

### Command use cases

Several commands exist for one situation each. The following list maps a situation to the command to run:

* **Preview a destroy:** Run `plan-destroy` to review what `destroy` would remove, then run `destroy` when you are ready.
* **Reconcile drift while code changes are pending:** Run `plan-refresh-only`, then `apply-refresh-only`. The state matches the real infrastructure, and unreviewed code changes are not applied.
* **Catch manual changes:** Run `detect-drift` on a schedule. For example, when someone creates an EC2 instance by hand next to a managed SQS queue, the step reports the instance as drift and fails. You can then import it, delete it, or leave it unmanaged.
* **Move off Harness-managed state:** Declare the external backend in your code, set its credentials as workspace environment variables, then run `migrate-state`. The external backend becomes the source of truth.
* **Fail on unformatted code:** Set the `TF_CLI_ARGS_fmt` pipeline variable to `-check`. The `fmt` step then exits with a non-zero code when a file needs reformatting.

Go to [Define variables](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/variables-and-expressions/add-a-variable.md) to add a pipeline variable such as `TF_CLI_ARGS_fmt`.

{% hint style="warning" %}
**Run migrate-state before init against a new backend**

An `init` that reaches an empty backend leaves the workspace with no state. The next plan then proposes to create your whole infrastructure again.
{% endhint %}

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 move state to an external backend, and go to [Review & Resolve Drift](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/operate/review-and-resolve-drift.md) to reconcile drift with `plan-refresh-only`.

### Permissions by command

The permission a command needs depends on whether it changes state:

| Command type   | Commands                                                   | Permissions                                               |
| -------------- | ---------------------------------------------------------- | --------------------------------------------------------- |
| Read-only      | `plan`, `validate`, `fmt`                                  | **Execute** on the pipeline and **View** on the workspace |
| State-changing | `apply`, `destroy` and the other commands that write state | **Execute** on the pipeline and **Edit** on the 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 workspace roles.

***

## Troubleshooting

<details>

<summary>OpenTofu plugin command fails with a binary not found error</summary>

The step could not find an OpenTofu binary for the workspace. Check that the workspace provisioner is OpenTofu and that its version is supported. If the stage uses a custom plugin image, check that the image contains that OpenTofu version. Go to [Build & Use a Custom Plugin Image](https://developer.harness.io/infrastructure-as-code-management/3.0/automation-and-self-hosting/self-hosted-and-restricted-environments/build-infrastructure/build-and-use-a-custom-plugin-image.md) to rebuild the image.

</details>

<details>

<summary>TF_CLI_ARGS_plan does not change the plan step flags</summary>

The `plan` step does not read `TF_CLI_ARGS_plan`. Set the matching `PLUGIN_` variable from the plan flags table instead, then run the pipeline again.

</details>

<details>

<summary>The next plan proposes to create every resource after a backend change</summary>

The workspace ran `init` against an empty backend before its state moved. Run a `migrate-state` step to copy the state into the new backend. 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 follow the migration in order.

</details>

***

## Next steps

Use these commands in a pipeline:

* [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): Build a pipeline that runs `init`, `plan` and `apply`.
* [Detect Drift](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/operate/detect-drift.md): Schedule `detect-drift` to catch manual changes.
* [Run a Local Plan with the Harness CLI](https://developer.harness.io/infrastructure-as-code-management/3.0/use-iacm/workspaces/plan-and-apply/run-a-local-plan-with-the-harness-cli.md): Run a plan against a workspace from your machine.
* [Plugin Environment Variables](https://developer.harness.io/infrastructure-as-code-management/3.0/help-and-reference/reference/plugin-environment-variables/plugin-environment-variables.md): Review every `PLUGIN_` variable the plugin reads.
