Importing Existing Harness Infrastructure into Terraform - Step by Step

1.) Go to https://registry.terraform.io/providers/harness/harness/latest/docs and search for the resource you’d like to import using the Filter on the left hand side. For example harness_platform_pipeline.

2.) Scroll down to the bottom of the page for a line similar to this:

terraform import harness_platform_pipeline.example <org_id>/<project_id>/<pipeline_id>

This is the line you will need to run after creating the main.tf file and running terraform init.

3.) Create a main.tf file with similar contents:

terraform {
  required_providers {
    harness = {
      source = "harness/harness"
    }
  }
}

#Configure the Harness provider for Next Gen resources
provider "harness" {
  endpoint         = "https://app.harness.io/gateway"
  account_id       = "your_account_id_goes_here"
  platform_api_key = "your_api_key_goes_here"
}

resource "harness_platform_pipeline" "example" {}

4.) Run:

terraform init

5.) Run:

terraform import harness_platform_pipeline.example <org_id>/<project_id>/<pipeline_id>

substituting any necessary values

For example:

terraform import harness_platform_pipeline.example testorg/CSETest1/test_pipeline_123

6.) Inspect the state file (terraform.tfstate). It should have the resource assuming no errors occurred.

7.) Run:

terraform show

which will give you an output similar to the following:

# harness_platform_pipeline.example:
resource "harness_platform_pipeline" "example" {
    id               = "test_pipeline_123"
    identifier       = "test_pipeline_123"
    name             = "test_pipeline_123"
    org_id           = "testorg"
    project_id       = "CSETest1"
    template_applied = false
    yaml             = <<-EOT
        pipeline:
          name: test_pipeline_123
          identifier: test_pipeline_123
          projectIdentifier: CSETest1
          orgIdentifier: testorg
          tags: {}
          stages:
            - stage:
                name: testing
                identifier: testing
                description: ""
                type: Approval
                spec:
                  execution:
                    steps:
                      - step:
                          type: ShellScript
                          name: Shell Script_1
                          identifier: ShellScript_1
                          spec:
                            shell: Bash
                            onDelegate: true
                            source:
                              type: Inline
                              spec:
                                script: echo "hello world"
                            environmentVariables: []
                            outputVariables: []
                          timeout: 15m
                tags: {}
    EOT
}

8.) Copy over the output from step 7 into the main.tf file.

9.) Remove the line with id.

For example:

id               = "test_pipeline_123"

10.) At this point you can modify and run

terraform apply

For example modify line with

timeout: 15m

to be

timeout: 30m

and run

terraform apply