Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.
AgentsAutomationTerraform

Automate with Terraform

Create and manage Harness Artifacts registries, upstream proxies, filtering rules, and Dependency Firewall as code with the Terraform provider.

The Harness Terraform provider lets you create and manage registries, upstream proxies, artifact filtering rules, and Dependency Firewall configuration as code. This page provides ready-to-use HCL for the most common automation patterns.

Provider setup

terraform {
  required_version = ">= 1.5.0"

  required_providers {
    harness = {
      source  = "harness/harness"
      version = ">= 0.30.0"
    }
  }
}

provider "harness" {
  endpoint         = "https://app.harness.io/gateway"
  account_id       = var.harness_account_id
  platform_api_key = var.harness_platform_api_key
}

Always use platform_api_key (not the legacy api_key); Artifact Registry is a Harness NextGen feature.

Credentials via environment variables: never hardcode them.

Standard variables.tf for all configs:


Virtual registry

A virtual registry is the client-facing endpoint your team uses to push and pull artifacts.

Supported package_type values: DOCKER, HELM, MAVEN, NPM, PYTHON, GO, NUGET, CARGO, GENERIC, RPM, DEBIAN, ALPINE, WOLFI, SWIFT, COMPOSER, DART, RUBYGEMS, CRAN, CONAN, CONDA, PUPPET, HUGGING_FACE, TERRAFORM_MODULE.


Upstream proxy

An upstream proxy caches artifacts from a public or private source registry. Attach it to a virtual registry to serve cached content.


Virtual registry wired to an upstream proxy

The full pattern: create both resources and wire them together in a single config. This is the recommended approach for new infrastructure.

The depends_on ensures the proxy is created before the virtual registry references its identifier.


Artifact filtering rules

Add allowed_pattern and blocked_pattern to restrict which artifacts can be pushed to or pulled from a registry. Each pattern is a full regex match against the artifact name.


Dependency Firewall

Dependency Firewall evaluates artifacts fetched through an upstream proxy against security policies before caching them. Configure it on the upstream proxy registry, not the virtual registry.

firewall_mode value

Behavior

Omitted / "ALLOW"

Firewall disabled

"ENABLED"

Firewall on; artifacts that trigger a Warn policy are cached and usable (violation logged)

"QUARANTINE"

Firewall on; artifacts that trigger a Warn policy are cached but blocked until manually unquarantined

Dependency Firewall is behind the HAR_DEPENDENCY_FIREWALL feature flag. Contact Harness Support to enable it. Applying firewall_mode without the flag enabled has no effect.


Import an existing registry

If a registry was created through the Harness UI, bring it under Terraform management.

The import path is always account_id/org_id/project_id/registry_identifier, with no leading slash, no https://, and no . separators.

HCL import block (Terraform ≥ 1.5, recommended):

Run terraform plan; Terraform shows what would change without applying. Run terraform apply to complete the import.

CLI import (all Terraform versions):

After importing, run terraform plan to confirm the existing state matches your configuration before applying any changes.


Remote state for teams and CI/CD

Local state (terraform.tfstate) only works on a single workstation. For shared infrastructure, configure a remote backend.

Never commit *.tfstate or *.tfstate.* files to source control. Add this .gitignore:


Run Terraform in CI/CD

GitHub Actions

Store HARNESS_API_KEY as a repository secret. Use a service account token scoped to the Artifact Registry Admin role, not a personal access token.

Harness CI pipeline


Troubleshooting

Error: `undefined response type` during terraform apply

The registry may have been created despite the error. Check the Harness UI. If the registry exists, import it:

Then run terraform init -upgrade to pull the latest provider version and retry.

Error: `terraform import` fails with "not found"

The import path must be exactly account_id/org_id/project_id/registry_identifier. Common mistakes:

  • Leading or trailing slash

  • Using the registry URL (pkg.harness.io/...) instead of the identifier path

  • Using . instead of / as the separator

Run terraform state list after a successful import to confirm the resource was added.

Error: `401 Unauthorized` from the Harness provider

Confirm:

  1. platform_api_key is set (not the legacy api_key).

  2. The token has not expired. Check in Account Settings > Service Accounts > [account] > Tokens.

  3. The service account has the Artifact Registry Admin or Contributor role at the scope matching space_ref.

Error: `403 Forbidden`: provider authenticates but cannot create the registry

The service account has valid credentials but lacks permission to create registries at the scope in space_ref. Go to Account Settings > Service Accounts > [account] > Role Bindings and assign the Artifact Registry Admin role at the project, org, or account scope that matches your space_ref.

upstream_proxies change is ignored on terraform apply

If the virtual registry was created before the upstream proxy existed, Terraform may have stored the initial empty list in state. Run:

If the plan still shows no changes but the proxy is not attached, the resource may need to be re-imported or the state may be stale. Force a replace:


Next steps

Last updated

Was this helpful?