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

# Initialization

Initialize your infrastructure dynamically with environment variables

At the first step of most infrastructure provisioning pipelines, you prepare your working directory with the `terraform init` command. Part of this step includes setting up your backend environment for Terraform state management. Harness enables the use of environment variables to dynamically update your backend configuration in Terraform.

Generally, you have two approaches, [hardcoded backend configuration](https://developer.hashicorp.com/terraform/language/backend), or partial configuration, where you specify an empty or partially configured backend block and populate the necessary values through environment variables.

## Apply environment variables <a href="#apply-environment-variables" id="apply-environment-variables"></a>

Using environment variables allows for a more dynamic and flexible backend configuration.

### Partial configuration example <a href="#partial-configuration-example" id="partial-configuration-example"></a>

```hcl
terraform {
  backend "s3" {}
}
```

In this example, the backend configuration is left empty, allowing us to supply the values using environment variables during the `terraform init` command.

### Set environment variables <a href="#set-environment-variables" id="set-environment-variables"></a>

You can set the environment variables used to configure the backend in various ways, such as directly in the terminal or through Infrastructure pipelines. Here are the variables corresponding to the partial configuration:

{% hint style="info" %}
**IMPORTANT**

To set up an environment variable that populates your terraform backend configuration as part of the `terraform init -backend-config` command (for example: `terraform init -backend-config="bucket=sample_s3_bucket" -backend-config="key=terraform.tfstate" -backend-config="region=us-east-1"`), you must name your variables in the following format:

`PLUGIN_INIT_BACKEND_CONFIG_` followed by a unique identifier to distinguish your variable.
{% endhint %}

When initializing an AWS S3 bucket for example, and passing the `bucket`, `key`, `region` and `dynamodb_table` parameters, Harness recommends naming your variables similar to the following:

* `PLUGIN_INIT_BACKEND_CONFIG_BUCKET`
* `PLUGIN_INIT_BACKEND_CONFIG_KEY`
* `PLUGIN_INIT_BACKEND_CONFIG_REGION`
* `PLUGIN_INIT_BACKEND_CONFIG_DYNAMODB_TABLE`

### Add new environment variables <a href="#add-new-environment-variables" id="add-new-environment-variables"></a>

To add new environment variables, follow these steps:

{% tabs %}
{% tab title="Interactive guide" %}
{% embed url="<https://app.tango.us/app/embed/aa30519d-534a-4017-a0bb-8d0439f5258a>" %}
{% endtab %}

{% tab title="Step-by-step" %}

1. Navigate to the **Infrastructure** module.
2. Select **Workspaces**, then select the **Variables** tab.
3. Select **New Environment Variable**.
4. Add your variables:
   * **Key**: PLUGIN\_INIT\_BACKEND\_CONFIG\_*IDENTIFIER*
   * **Value**: Parameter `key=value` pair, e.g. `region=us-east-1`

![Terraform init environment variables](https://3575326923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ffhx6XzjvtJmfv5gPWZ5m%2Fuploads%2Fgit-blob-971db9949d79c0092a7fb83da91f4dbffaa64461%2Ftf-init-env-variables.png?alt=media)
{% endtab %}
{% endtabs %}

## Backend reconfiguration <a href="#backend-reconfiguration" id="backend-reconfiguration"></a>

The `PLUGIN_INIT_BACKEND_CONFIG` variables populate a partial backend block. A separate variable controls how Harness initializes a working directory whose backend has changed since the last run.

When your code contains no backend block, Harness injects its own HTTP backend at init time. When your code does contain a backend block, Harness initializes against that block instead, and does not automatically add `-migrate-state` or `-reconfigure` to the command.

Changing the backend block, for example when you move a workspace from Harness-managed state to an external backend, leaves the previous backend recorded in the working directory. OpenTofu and Terraform stop and ask you to either migrate the state or discard the record, but the step fails because no one responds to the prompt. To get past that first run, add the following workspace environment variable:

* **Key**: `PLUGIN_INIT_RECONFIGURE`
* **Value**: `true`

This adds `-reconfigure` to the init command that discards the record of the previous backend. It is only needed on the first run after the backend block changes, so remove it once that run succeeds.

{% hint style="warning" %}
**RECONFIGURE DOES NOT MOVE STATE**

`-reconfigure` discards the record of the previous backend without copying any state to the external backend. Migrate the state first, otherwise the run initializes against an empty backend and the next plan proposes to create your whole infrastructure again. Use the `migrate-state` plugin command step or the local CLI migration to transfer the state, then set this variable only if a subsequent init still fails. Go to [Migrate state out of Harness](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/state-migration.md#migrate-state-out-of-harness-migrate-from-harness-managed-state-to-an-external-backend) to move state to a backend you control.
{% endhint %}

## Review variable usage <a href="#review-variable-usage" id="review-variable-usage"></a>

Once your Terraform backend configuration environment variables are set, and you have a provision pipeline, run the pipeline and review the console log during the `init` step to confirm your variable values match what is used in the pipeline.

![Pipeline environment variable usage](https://3575326923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ffhx6XzjvtJmfv5gPWZ5m%2Fuploads%2Fgit-blob-2de71794010fd5d53f5c191e3071a6a72b3b2cc9%2Ftf-initi-variable-log.png?alt=media)

Notice the list of backend config values, shortly followed by the generated `terraform init` command including the `-backend-config` label with accompanying parameters, populated with your environment variable values.

## Conclusion <a href="#conclusion" id="conclusion"></a>

Using environment variables for backend configuration in Terraform provides a flexible and dynamic approach, making it easier to manage different environments and improve security. By following the steps outlined in this topic, you can set up and use environment variables to configure your Terraform backend dynamically.

Go to [Terraform documentation on partial configuration](https://developer.hashicorp.com/terraform/language/backend#partial-configuration) to review supported backend configurations.

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

<details>

<summary>Does Harness inject a backend automatically when my code has no backend block?</summary>

Yes. When your code contains no backend block, Harness injects its own HTTP backend at init. When your code contains a backend block, Harness initializes against that block without adding `-migrate-state` or `-reconfigure`. Backend values you pass yourself are workspace environment variables, and the init log shows them as `-backend-config` arguments. Go to [Migrate state out of Harness](https://developer.harness.io/infrastructure-as-code-management/use-iacm/remote-backends/state-migration.md#migrate-state-out-of-harness-migrate-from-harness-managed-state-to-an-external-backend) to move to an external backend.

</details>

{% @harness-feedback/feedback %}
