> 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

This topic shows how to get started with the Harness Terraform Provider.

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

Go to [Create personal API keys and tokens](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/automation/api/add-and-manage-api-keys.md#create-personal-api-keys-and-tokens) to generate a PAT.

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

Keep the following capabilities and limitations in mind when using the Harness Terraform Provider:

* 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](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/platform-access-control/provision-users-with-okta-scim.md), [OneLogin](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/platform-access-control/provision-users-and-groups-with-one-login-scim.md) or [Microsoft Entra ID](https://developer.harness.io/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.\
  Go to [Configuration Language](https://www.terraform.io/language) to review the Terraform configuration language syntax.
* 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](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/automation/api/add-and-manage-api-keys.md).

```hcl
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**

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

Go to [Harness Organization](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_organization) to try out this resource.

**Add Project details**

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

Go to [Harness Project](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_project) to try out this resource.

**Add Encrypted Text Secret details**

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

Go to [Harness Text Secret](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_secret_text) to try out this resource.

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

Go to [harness\_platform\_pipeline](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_pipeline) to view an example usage 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.

```bash
terraform init
```

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

```bash
terraform plan
```

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

```bash
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.​ Go to [State](https://www.terraform.io/language/state) to learn how Terraform tracks resource 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:

```bash
terraform import
```

Go to [Lifecycle of a Terraform Resource](https://freecontent.manning.com/the-lifecycle-of-a-terraform-resource/) to understand the life cycle of a Terraform resource.

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

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

{% @harness-feedback/feedback %}
