Use Existing Remote State
Learn how to reuse existing remote state backends in IaCM.
Harness IaCM lets you reuse existing remote state backends, such as AWS S3, Google Cloud Storage (GCS), or Azure Blob Storage, without migrating to Harness-managed storage. Just point your backends.tf file to your existing backend, and Harness will use it directly with OpenTofu.
This approach is ideal if you are onboarding to IaCM, already use remote backends, or need compatibility with other systems or CI pipelines.
Prerequisites
Your repository contains a valid
backends.tffile.Your existing remote state file (for example,
.tfstate) is accessible and versioned.Harness has read/write access to the remote backend. Go to Set environment variables to configure access.
You use the Plan and Apply steps in your pipeline, not custom script steps.
Backend authentication credentials (for example, for GCS, S3, Azure) are configured via environment variables in the Harness Workspace.
Example backends
terraform {
backend "s3" {
bucket = "my-existing-tfstate-bucket"
key = "envs/dev/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}Ensure your Harness AWS connector or environment variables grant permission to access the bucket and DynamoDB table. OpenTofu S3 Backend Docs
terraform {
backend "gcs" {
bucket = "my-tfstate-bucket"
prefix = "envs/dev"
}
}Your GCP connector or Workspace-level environment variables must include the necessary IAM roles (for example,
roles/storage.objectAdmin). OpenTofu GCS Backend Docs
terraform {
backend "azurerm" {
resource_group_name = "tf-state"
storage_account_name = "tfstateaccount"
container_name = "tfstate"
key = "terraform.tfstate"
}
}Configure access to Azure backend using environment variables like
ARM_CLIENT_ID,ARM_CLIENT_SECRET, and others. OpenTofu AzureRM Backend Docs
Configure and test your pipeline
To provision your workspace with an existing remote backend, configure your pipeline using OpenTofu steps as shown below:
If your workspace requires OpenTofu/Terraform variables or environment variables, add them in your workspace settings.
If no variables are specified, Harness uses any defaults defined in the source code (for example, variables.tf files in your repo). Go to Declaring variables to review how to define variables in your source code.
To test your setup: Run your pipeline to execute the init, plan, and apply steps (and any approval plugins you have configured). Go to IaCM Setup pipeline for the full setup pipeline.
Check the logs during the approval or apply step to verify successful initialization of the remote backend.
Ensure your
backends.tffile is present in the repository connected to your workspace.
State locking considerations
Each remote backend implements its own locking mechanism:
S3 uses DynamoDB for locking.
GCS relies on object metadata.
Azure Blob uses blob leases.
OpenTofu handles lock acquisition and release during pipeline execution. There is no additional locking layer in IaCM. Locks are managed entirely by OpenTofu based on the backend settings.
Troubleshooting and best practices
Best Practices
Use versioned buckets or lock tables for safe collaboration.
Keep
backends.tfin version control, but avoid hardcoding secrets.Define backend credentials via environment variables in the Harness UI or Workspace settings.
If migrating from Terraform Cloud or OpenTofu Cloud, remove any
cloud {}blocks and rely on backend definitions instead.Validate that only one process accesses a given state at a time to prevent corruption.
Run speculative plans only from pipelines to ensure consistent remote state access.
Use workspace variables or pipeline inputs for backend paths when multiple environments share the same backend configuration.
Related links
Last updated
Was this helpful?