> 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/harness-platform/3.0/harness-platform-resources/automation/terraform-provider/harness-terraform-provider.md).

# Harness Terraform Provider quickstart

Terraform is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently.​

Harness Terraform Provider is a library that you can use to create Harness Infrastructure. You can administer and use Harness functionality from within your Terraform setup using Harness Terraform Provider.

This quickstart shows you how to write your configurations in Terraform and provision your Harness resources using the Harness Terraform Provider.

#### Before you begin <a href="#before-you-begin" id="before-you-begin"></a>

* [Introduction to Terraform](https://www.terraform.io/intro)
* [Terraform Registry](https://www.terraform.io/registry)
* [Terraform Configuration Language](https://www.terraform.io/language)

#### Prerequisites <a href="#prerequisites" id="prerequisites"></a>

* You must have a Harness Account.
* You must have an admin setup for your Harness Account.
* You must have a Personal Access Token (PAT) or a Service Access Token (SAT).

For detailed steps on how to generate a PAT, see [Create personal API keys and tokens](/harness-ai/use-harness-platform/automation/api/add-and-manage-api-keys.md#create-personal-api-keys-and-tokens).

#### Important <a href="#important" id="important"></a>

* Harness Terraform Provider lets you provision the following:
  * Organizations
  * Projects
  * Permissions setup
  * Connectors
  * Secrets
  * Pipelines
* You cannot provision users using Harness Terraform Provider.\
  You can provision users through SCIM using [Okta](/harness-platform/3.0/harness-platform-resources/platform-access-control/provision-users-with-okta-scim.md), [OneLogin](/harness-platform/3.0/harness-platform-resources/platform-access-control/provision-users-and-groups-with-one-login-scim.md) or [Microsoft Entra ID](/harness-platform/3.0/harness-platform-resources/platform-access-control/provision-users-and-groups-using-azure-ad-scim.md).
* You cannot run or monitor your Pipelines using Harness Terraform Provider.

#### Why use Harness Terraform Provider? <a href="#why-use-harness-terraform-provider" id="why-use-harness-terraform-provider"></a>

Harness Terraform Provider lets you use Terraform scripts to configure Harness. Through scripts, it supports the creation of all the key Harness objects, including Projects, Pipelines, Connectors, and Secrets.

It thus lets you create a repeatable and scalable structure of Harness infrastructure.​​

#### Visual summary <a href="#visual-summary" id="visual-summary"></a>

Here is a quick overview of Harness Terraform Provider:

* You write your Terraform Configuration in `.tf` file.\
  You declare all the resources that represent your infrastructure objects in this file.\
  For more details, see [Configuration Language](https://www.terraform.io/language).
* Next, you initialize, plan, and apply your resources using the following commands:
  * `terraform init`
  * `terraform plan`
  * `terraform apply`
* Once you confirm Terraform apply, a state file `terraform.tfstate` is generated.
* Your resources are provisioned successfully.

#### Install Harness Terraform Provider <a href="#install-harness-terraform-provider" id="install-harness-terraform-provider"></a>

To install the Harness Terraform Provider, copy, and paste the following code into your Terraform configuration.

Enter your Harness Account Id in `account_id`.

The account Id is in every URL when using Harness:

`https://app.harness.io/ng/#/account/``**{accountid}**``/home/get-started`

Enter your PAT or SAT in `platform_api_key`.

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

Harness recommends using SAT to install the Harness Terraform Provider.
{% endhint %}

For detailed steps on how to generate a PAT or SAT, go to [Manage API keys](/harness-ai/use-harness-platform/automation/api/add-and-manage-api-keys.md).

```
terraform {  
    required_providers {  
        harness = {  
            source = "harness/harness"  
            version = "<version_number>"  
        }  
    }  
}

provider "harness" {  
    endpoint   = "https://app.harness.io/gateway"  
    account_id = "<your_harness_accountid"  
    platform_api_key    = "your_pat"  
}
```

#### Add Harness Resource configurations to Terraform files <a href="#add-harness-resource-configurations-to-terraform-files" id="add-harness-resource-configurations-to-terraform-files"></a>

Before you start provisioning Harness Resources, make sure you have written your Terraform Configuration as declarative scripts as a`.tf` file.

The `.tf` Terraform file will contain the Harness infrastructure details that you want to provision.

Let us see examples of adding the following resources to your Terraform Configuration:

* Organization
* Project
* Encrypted Text Secret within a Project

**Add Organization details**

```
resource "harness_platform_organization" "org" {​  
    name      = "Terraform Example Org"  
    identifier = "terraform_example_org"  
    description = "Demo Organization, created through Terraform"  
}
```

Your Organization now appears in the list of Organizations within the scope of your Harness Account.

To try it out, see [Harness Organization](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_organization).

**Add Project details**

```
resource "harness_platform_project" "project" {  
    name      = "Terraform Test Project"  
    identifier = "terraform_test_project"  
    org_id    = "terraform_example_org"  
}
```

Your Project now appears in the list of Projects within the scope of your Organization.

To try it out, see [Harness Project](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_project).

**Add Encrypted Text Secret details**

```
resource "harness_platform_secret_text" "textsecret" {​  
    identifier  = "terraform_example_secret"  
    name        = "Terraform Example Secret"  
    description = "This is a text Secret, generated through Terraform"  
    org_id      = "terraform_example_org"  
    project_id  = "terraform_test_project"  
    tags        = ["example:tags"]  
  
    secret_manager_identifier = "harnessSecretManager"  
    value_type                = "Inline"  
    value                     = "secret"  
    lifecycle {  
        ignore_changes = [  
            value,  
        ]  
    }  
}
```

Your Text Secret now appears in the list of Secrets within the scope of your Project.

To try it out, see [Harness Text Secret](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_secret_text).

If you do not provide the `org_id` and the `project_id` in the configuration, the Secret is created at the Account scope.

**Add Pipeline details**

For an example, see the example usage at [harness\_platform\_pipeline](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_pipeline) and copy it into your pipeline.

Your pipeline now appears in the list of pipelines within the scope of your project.

#### Provision Harness infrastructure <a href="#provision-harness-infrastructure" id="provision-harness-infrastructure"></a>

You can create the Harness infrastructure by performing the following steps:

1. **Initialize**: run the following command to initialize a working directory and set up the correct version of the Harness Terraform Provider.

```
terraform init
```

This command also creates a `.terraform` directory and downloads specific plugins. 2. **Plan**: run the following command to add desired resources.

```
terraform plan
```

This command creates an execution plan. 3. **Apply**: run the following command to apply the plan you just created.

```
terraform apply
```

This command executes your plan.Once you confirm the execution plan, Terraform generates a state file `terraform.tfstate` to track the state of all the resources it manages. This file is used to detect differences during the plan and detect configuration drift.​ For more details, see [State](https://www.terraform.io/language/state).

#### Terraform import <a href="#terraform-import" id="terraform-import"></a>

You can provision existing resources that you have created and bring them under Terraform management.

To do this, use the following command:

```
terraform import
```

To learn more about the life cycle of a Terraform resource, see [Lifecycle of a Terraform Resource](https://freecontent.manning.com/the-lifecycle-of-a-terraform-resource/).

To try out the Harness Terraform Provider, see the [Harness Provider documentation](https://registry.terraform.io/providers/harness/harness/latest/docs).

<details>

<summary>Permanent plan diff after removing description from harness_platform_infrastructure</summary>

Set description to an empty string explicitly rather than removing the attribute. This gives the provider a concrete value to send to the API, preventing the refresh from overriding with the stale cached value. Tracked in PL-71852.

</details>

#### Next steps <a href="#next-steps" id="next-steps"></a>

* [Customize Terraform Configuration with Variables](https://www.terraform.io/language/values)
* [Reuse Configuration with Modules](https://www.terraform.io/language/modules)
