> 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/continuous-delivery/use-continuous-delivery/provision-infrastructure/terraform-infra/optional-tf-var-files.md).

# Specify Terraform variables

Learn how to specify Terraform variables or fetch variable files in Terraform steps

This topic shows you how to configure Terraform variable files in your pipeline Terraform steps. You can specify inline variables or connect to remote file stores to manage configuration separately from your code.

***

## What you will learn from this topic

* How to add [inline variables](#add-inline-variables) directly in the step configuration for simple, static values.
* How to fetch [remote variable files](#add-remote-variable-files) from Git repositories to manage variables in version control.
* How to mark files as [optional](#add-remote-variable-files) so Harness ignores missing files and continues execution.

***

## Before you begin

* **Harness project access**: View, Create/Edit, and Execute permissions on Pipelines and Environments. For more information, refer to [RBAC in Harness](https://developer.harness.io/docs/platform/role-based-access-control/rbac-in-harness) to configure roles.
* **Terraform or OpenTofu installed on delegate**: Terraform or OpenTofu must be installed on the delegate to execute Terraform commands. For more information, refer to [Build custom delegate images with third-party tools](https://developer.harness.io/docs/platform/delegates/install-delegates/build-custom-delegate-images-with-third-party-tools) to install Terraform.
* **Existing pipeline with Terraform step**: You need a pipeline with a Terraform Apply, Terraform Plan, or Terraform Destroy step. For more information, refer to [Terraform provisioning with Harness](/continuous-delivery/use-continuous-delivery/provision-infrastructure/terraform-infra/terraform-provisioning-with-harness.md) to create a Terraform provisioning pipeline.
* **Harness connector for remote files** (optional): If you plan to use remote variable files, configure a connector for your repository (GitHub, GitLab, Bitbucket) or artifact store (S3, Artifactory). For more information, refer to [Connect to a Git repo](https://developer.harness.io/docs/platform/connectors/code-repositories/connect-to-code-repo) for Git connectors or [Artifactory Connector Settings Reference](https://developer.harness.io/docs/platform/connectors/cloud-providers/ref-cloud-providers/artifactory-connector-settings-reference) for Artifactory.
* **Understanding of Terraform concepts**: This topic assumes familiarity with Terraform variables, workspaces, and state files. For more information, refer to [Terraform documentation](https://developer.hashicorp.com/terraform/docs).

***

## Specify Terraform variable files

In the **Optional Configuration** > **Terraform Var Files (optional)** section of the Terraform Apply, Terraform Plan, and Terraform Destroy steps, you can add inline variables or connect to remote file stores. Each entry can represent inline variables or a connection to a remote Git repository.

When you mark remote files as optional, Harness fetches any files that exist at runtime and ignores any files that do not exist. The step continues to execute even if some files are missing.

{% hint style="success" %}
**When to use inline vs remote variables**

Use **inline variables** for simple, non-sensitive values that rarely change and are specific to one pipeline. Use **remote variable files** for configuration managed in version control, variables shared across multiple pipelines, or when you need to track changes to variable definitions.
{% endhint %}

### Add inline variables

Follow these steps to add inline Terraform variables directly in the step configuration.

{% hint style="success" %}
In some fields in this procedure, you can specify a fixed value, expression, or runtime input. For more information, refer to [Fixed values, runtime inputs, and expressions](https://developer.harness.io/docs/platform/variables-and-expressions/runtime-inputs) to understand value types and how to identify fields that offer this functionality.
{% endhint %}

1. On the **Visual** tab of **Pipeline Studio** (Harness's visual pipeline editor), select the step at which you want to configure Terraform variables.
2. In the step configuration pane that appears, expand **Optional Configuration**.
3. In **Terraform Var Files (optional)**, select **+ Add**, and then select **Add Inline**.
4. In **Identifier**, specify an identifier for the variable file.

   You can reference variables by using the identifier in Harness expressions. For example, if the identifier is `myvars`, you could refer to its content as follows:

   `<+pipeline.stages.MyStage.spec.infrastructure.infrastructureDefinition.provisioner.steps.plan.spec.configuration.varFiles.myvars.spec.content>`

   For more information, refer to [Use Harness expressions](https://developer.harness.io/docs/platform/variables-and-expressions/harness-variables) to learn how to reference values in pipelines.
5. In **Content**, provide the input variables and values for your Terraform script. Harness follows the same format as Terraform.

   For example, suppose that your Terraform script has the following variable definition:

   ```hcl
   variable "region" {  
   type = string  
   }
   ```

   In **Content**, you could enter the following:

   ```hcl
   region = "asia-east1-a"
   ```

   If you are entering secrets (for example, credentials), use Harness secret references in the value of the variable:

   ```hcl
   secrets_encryption_kms_key = "<+secrets.getValue("org.kms_key")>"
   ```

   For more information, refer to [Add Text Secrets](https://developer.harness.io/docs/platform/secrets/add-use-text-secrets) to learn how to create and reference secrets in Harness.
6. Select **Submit**. The inline variable entry is created.

### Add remote variable files

Follow these steps to specify variable files available in remote Git repositories.

1. On the **Visual** tab of **Pipeline Studio**, select the step at which you want Harness to download Terraform variable files.
2. In the step configuration pane that appears, expand **Optional Configuration**.
3. In **Terraform Var Files (optional)**, select **+ Add**, and then select **Add Remote**.
4. On the **Specify Terraform Var File Store** page:
   * From the list of file stores shown, select the file store that contains the Terraform variable files.
   * Click in the connector field, select a connector in the dialog that is displayed, and then select **Apply Selected**.

     Alternately, select the button for creating a connector. For more information, refer to [Code repo connectors](https://developer.harness.io/docs/category/code-repo-connectors) to create a connector for GitHub, GitLab, Bitbucket, or Azure Repos.
5. Select **Continue**.
6. On the **Var File Details** page, configure the file based on the connector type you selected. The fields shown depend on the store type (GitHub, GitLab, Bitbucket, S3, Artifactory, Azure Repos). The following steps use GitHub as an example.

   * In **Identifier**, enter an identifier for the remote file store entry.

     You can use the identifier to refer to variables using Harness expressions. For example, if the identifier is `myremotevars`, you could refer to its content as follows:

     `<+pipeline.stages.MyStage.spec.infrastructure.infrastructureDefinition.provisioner.steps.plan.spec.configuration.varFiles.myremotevars.spec.store.spec.paths>`

     For more information, refer to [Use Harness expressions](https://developer.harness.io/docs/platform/variables-and-expressions/harness-variables) to learn how to reference values in pipelines.

   **Example: GitHub connector**

   * In **Repo Name**, specify the name of the repository in `organization/repository` format (for example, `harness/terraform-configs`).
   * In **Git Fetch Type**, select what you want Harness to fetch:
     * **Latest from Branch**: Fetch the latest versions of the files. Use this for active development where you want the most recent variable definitions. When you select this option, the **Branch** field appears where you enter the branch name (for example, `main` or `develop`).
     * **Specific Commit Id/Git Tag**: Fetch a specific commit ID or Git tag. Use this for reproducible production deployments where you need consistent variable values. When you select this option, the **Commit Id** or **Git Tag** field appears where you enter the commit ID or tag name.
   * In **File Paths**, add one or more file paths from the root of the repository to the variable file. Select **+ Add File Path** to add more file paths.
   * Select **Optional** if you want Harness to ignore any files that do not exist at run time. Harness downloads available files and does not fail the step on account of missing files. If all files are missing, the step continues with no variable files loaded.
7. Select **Submit**. The entry is created.

{% hint style="info" %}
**Variable precedence**

When you specify multiple variable files, Terraform applies them in the order listed. Later files override earlier files if they define the same variable. This follows standard Terraform variable precedence rules.
{% endhint %}

### Configure using YAML

<details>

<summary>YAML example</summary>

The following YAML shows the configuration for remote Terraform variable files:

```yaml
                      varFiles:
                        - varFile:
                            type: Remote
                            identifier: optional_var_files_123
                            spec:
                              optional: true
                              store:
                                type: Github
                                spec:
                                  gitFetchType: Branch
                                  repoName: "organization/repository"
                                  branch: main
                                  paths:
                                    - terraform/localresource/localresourcevars/localresource.tfvars
                                    - inexistentaasd123a.tvfars
                                    - iasd13sadfsa6757.tfvars
                                  connectorRef: vlicaprerequisites
```

</details>

### View execution logs

At execution time, Harness logs the `.tfvars` files that were found and fetched. You can verify which files were successfully loaded in the step execution logs. The variables appear in the Terraform Plan output.

The following example shows the execution logs when Harness fetches remote variable files:

```
Starting Git Fetch Files

Fetching GIT VAR_FILES files with identifier: optional-var-fil12313
Git connector Url: https://github.com/vlica-harness/prerequisites.git
Branch: main

Fetching following Files :
- terraform/localresource/localresourcevars/localresource.tfvars
- inexistent131.tfvars
- asfafainexistent.tfvars
- asdsadasdsa.tfavr
Successfully fetched following files:
- terraform/localresource/localresourcevars/localresource.tfvars
```

***

## Troubleshooting

<details>

<summary>File path not found error in Harness Terraform step even with optional marked true</summary>

Check that file paths are relative to the repository root. Review execution logs to see which files were fetched and which were skipped. If optional is true, missing files do not fail the step.

</details>

<details>

<summary>Connector authentication failure when fetching remote Terraform variable files in Harness CD</summary>

Verify that the connector credentials are correct and that the service account has read access to the repository. Check connector settings in Harness Platform.

</details>

<details>

<summary>Variable precedence when multiple Terraform variable files define the same variable in Harness</summary>

Terraform applies variable files in the order listed. Later files override earlier files for the same variable. Reorder files in the Terraform Var Files section to control precedence.

</details>

<details>

<summary>Harness expression evaluation errors in inline Terraform variables</summary>

Check expression syntax and ensure referenced values exist in the pipeline context. Verify that secrets and variables are defined at the correct scope (pipeline, project, organization, or account).

</details>

<details>

<summary>Secret reference not resolving in inline Terraform variables in Harness CD</summary>

Verify that the secret exists at the correct scope and that you have permission to access it. Check the secret path in the expression and ensure it matches the secret identifier. An administrator must grant secret access permissions if needed.

</details>

***

## Next steps

* [Provision with the Terraform Apply step](/continuous-delivery/use-continuous-delivery/provision-infrastructure/terraform-infra/run-a-terraform-plan-with-the-terraform-apply-step.md): Run your Terraform pipeline.
* [Terraform how-tos](/continuous-delivery/use-continuous-delivery/provision-infrastructure/terraform-infra/terraform-provisioning-with-harness.md): Explore advanced Terraform workflows in Harness.

{% @harness-feedback/feedback %}
