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

Terraform

Create, configure, and manage Harness Artifacts registries as infrastructure as code using the Harness Terraform provider registry resource.

You can create, configure, and manage Harness Artifacts registries using Terraform, an infrastructure-as-code tool that lets you define cloud resources in configuration files instead of clicking through a UI. The Harness Terraform provider includes the harness_platform_har_registry resource, which supports virtual registries, upstream proxies, and the links between them.

Instead of creating registries by hand in the Harness console, you write a few short configuration files and run terraform apply. The result is the same, but the setup is version-controlled, repeatable, and auditable.

This page covers using the Harness Terraform provider to create and manage registries as infrastructure as code.


How it works

The integration follows a three-part pattern:

  1. Define registry resources. Write HCL configuration files that declare your registries using the harness_platform_har_registry resource. Each registry is a separate Terraform resource with its own type (VIRTUAL or UPSTREAM), package type, and configuration.

  2. Authenticate with Harness. Configure the Harness Terraform provider with your API key. The provider handles all API communication with the Harness platform.

  3. Apply the configuration. Run terraform apply to create, update, or destroy registries in your Harness project. Terraform tracks state, so subsequent runs only modify what has changed.


Before you begin

  • Harness account with Artifacts enabled. Sign up if you do not have one.

  • Terraform v1.5.0 or later installed on your machine. To install Terraform, go to Install Terraform.

  • Docker installed on your machine (used to verify the registry at the end of the tutorial). To install Docker, go to Get Docker.

  • Harness API key. Generate a Personal Access Token (PAT) or Service Account Token in the Harness UI under My Profile > + API Key > + Token.

  • Your project identifiers:

    • Account ID: Visible in your Harness URL or under Account Settings > Overview.

    • Organization ID: The identifier of your Harness organization (for example, default).

    • Project ID: The identifier of the project where you want to create the registries.


Tutorial: creating a Docker registry

This tutorial walks you through creating a fully functional Docker registry in Harness Artifacts using Terraform. You will build three resources:

  • A virtual registry, which is the URL your team uses to pull and push Docker images.

  • An upstream proxy, a caching layer that connects to Docker Hub. The first time someone pulls a public image, it is fetched from Docker Hub and cached. Every subsequent pull is served from the cache.

  • A link between them, so pulls from the virtual registry automatically resolve through Docker Hub when needed.

Each step creates its own Terraform configuration in a separate directory:

Step
What it creates
Directory

Step 1

Virtual registry (client-facing endpoint)

01-virtual-registry/

Step 2

Upstream proxy connected to Docker Hub

02-upstream-proxy/

Step 3

Link between the proxy and the virtual registry

03-link-virtual/

Each directory contains five standard Terraform files: versions.tf, providers.tf, variables.tf, main.tf, and outputs.tf.

Shared provider configuration

The versions.tf and providers.tf files are the same across all three steps. Create them once and copy them into each directory.

Harness Artifacts is a Next Gen feature. Always use platform_api_key (not the legacy api_key) when configuring the provider.

Setting credentials

Before running any step, set your credentials as environment variables:

Replace the placeholder values with your actual account ID, API key, and project scope.


Step 1: creating the virtual registry

The virtual registry is the entry point for your team's Docker operations. In this step, you create it without an upstream proxy attached. The proxy connection is made in Step 3.

Create a directory called 01-virtual-registry and add the shared versions.tf and providers.tf files, then add the following files.

variables.tf
Attribute
Description

identifier

A unique name for this registry within your project. Becomes part of the URL you use with docker pull.

space_ref

The project to create the registry in. Format: account_id/org_id/project_id.

package_type

The type of artifacts this registry holds. Options: DOCKER, HELM, MAVEN, NPM, PYPI, GO, NUGET, CARGO.

config.type

VIRTUAL for a client-facing registry, UPSTREAM for a proxy.

config.upstream_proxies

A list of upstream proxy identifiers. Empty in Step 1; populated in Step 3.

Initialize and apply:

Type yes when prompted. You should see output similar to:

Save the import_command_for_step3 value. You need it in Step 3.


Step 2: creating the upstream proxy

The upstream proxy connects to Docker Hub and caches images locally in Harness. This resource is independent of the virtual registry you created in Step 1.

Create a directory called 02-upstream-proxy and add the shared versions.tf and providers.tf files, then add the following files.

variables.tf

The dynamic "auth" block is a Terraform pattern for conditional configuration. If anonymous mode is on, the auth block is skipped entirely. If it is off, the block includes the Docker Hub credentials. The same configuration file works for both setups.

Initialize and apply:

For anonymous Docker Hub access (the default), no additional variables are needed. This is sufficient for pulling public images such as nginx, alpine, or redis.

For authenticated Docker Hub access (higher rate limits, private image support), set these additional variables before running terraform apply:

Setting up Docker Hub credentials in Harness

  1. In Docker Hub, go to Account Settings > Security > New Access Token. A read-only token is sufficient.

  2. In Harness, go to your project, then Project Settings > Secrets > + New Secret > Text. Paste the Docker Hub token as the value. Note the secret identifier, which is the value for dockerhub_secret_identifier.

Type yes when prompted. You should see output similar to:


Step 3: linking the upstream proxy to the virtual registry

This step updates the virtual registry from Step 1 to route requests through the upstream proxy from Step 2. Because the virtual registry already exists, you need to import it into this step's Terraform state before applying.

Create a directory called 03-link-virtual and add the shared versions.tf and providers.tf files, then add the following files.

variables.tf

The only change from Step 1 is the upstream_proxies line. Instead of an empty list, it references the proxy identifier from Step 2.

Initialize, import, and apply:

1

Verify Step 2 is complete

Confirm the state file from Step 2 exists:

2

Initialize Terraform

3

Import the existing virtual registry

Use the command that Step 1 printed in its output:

Replace the values with your actual account ID, organization, project, and registry identifier. You should see:

4

Apply the update

Type yes when prompted. You should see:

0 added, 1 changed confirms Terraform recognized the existing registry and updated it to include the upstream proxy.


Verifying the setup

All three steps are complete. Verify the end-to-end flow by pulling a Docker image through the new registry.

1

Log in to the Harness registry

You should see Login Succeeded.

2

Pull an image through the virtual registry

The pull path format is pkg.harness.io/<account_id>/<registry_identifier>/<image>:<tag>. It does not include the org or project segments, even though they appear in space_ref.

On the first pull, the request flows through the chain: virtual registry, then upstream proxy, then Docker Hub. The image is cached in Harness. Subsequent pulls are served directly from the cache.

3

Verify in the Harness UI

Navigate to Artifact Registry in your project. You should see:

  • my-docker listed as a Virtual registry with dockerhub-proxy shown as its upstream.

  • dockerhub-proxy listed as an Upstream registry connected to Docker Hub.

  • The alpine image visible under the virtual registry after the pull.


Cleaning up

To remove the resources created in this tutorial, destroy them in reverse order:

Type yes at each prompt.


Troubleshooting

Terraform reports an undefined response type error during apply

The registry may have been created despite the error. Check the Harness UI. If the registry exists, run terraform import harness_platform_har_registry.NAME "account_id/org_id/project_id/registry_identifier" to adopt it, then run terraform init -upgrade to pull the latest provider and retry.

Terraform import fails with a not-found error

The import path must be exactly account_id/org_id/project_id/registry_identifier with no leading or trailing slashes and no https:// prefix. Verify each segment matches the values in your Harness project and re-run with the corrected path.

Authentication errors from the Harness Terraform provider

Use a Next Gen API key with platform_api_key (not the legacy api_key). Confirm the token has permission to manage registries in the target project. Verify that space_ref matches account_id/org_id/project_id exactly.


Next steps

  • To replicate this pattern for other package types (Helm, Maven, npm, PyPI, and others), change the package_type attribute in the resource definition. To review supported package types, go to Supported formats.

  • For the full resource reference including all attributes and data sources, go to Terraform provider reference.

  • To create registries through the Harness console instead of Terraform, go to Create a registry.

  • To host Terraform modules and providers as packages in Harness Artifacts, go to Terraform client setup.

Last updated

Was this helpful?