Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

HCP Terraform (Terraform Cloud) — Complete Guide to Variables, Scope & Precedence

Updated: October 2026 | Based on current HashiCorp documentation

HCP Terraform provides several ways to pass values to Terraform code. These include variable defaults, .tfvars files, environment variables, workspace variables, project/organization variable sets, and run-specific CLI arguments.

The three most important things to understand are:

  1. Type: Is it a Terraform input variable or an environment variable?
  2. Scope: Does the value apply to one run, one workspace, a project, or the entire organization?
  3. Precedence: When the same variable is defined in multiple places, which value wins?

One particularly important rule: Workspace variables normally override variable sets, but Priority Variable Sets can override even workspace variables and command-line values.

1. Two types of variables in HCP Terraform

FeatureTerraform variableEnvironment variable
PurposeConfigure Terraform resources and modulesConfigure providers, authentication and Terraform execution
Exampleinstance_typeAWS_REGION
Access in .tf codevar.instance_typeUsually through provider or Terraform process behavior
Workspace UI categoryTerraform VariableEnvironment Variable
Supports HCL checkboxYesNo
Supports Sensitive flagYesYes
Can be in variable setsYesYes

Example A: Terraform input variable

In variables.tf:

variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t3.micro"
}Code language: JavaScript (javascript)

In HCP Terraform, configure:

Category: Terraform variable
Key:      instance_type
Value:    t3.mediumCode language: HTTP (http)

And access it in code:

resource "aws_instance" "example" {
  ami           = var.ami_id
  instance_type = var.instance_type
}Code language: JavaScript (javascript)

Terraform uses t3.medium, overriding the default t3.micro.

Example B: Environment variable

In HCP Terraform:

Category: Environment variable
Key:      AWS_REGION
Value:    ap-northeast-1Code language: HTTP (http)

Terraform’s AWS provider can consume the environment variable:

provider "aws" {
  # Region may be supplied by AWS_REGION
}Code language: PHP (php)

Important: AWS_REGION does not automatically become var.aws_region. If you want var.aws_region, declare an input variable and provide its value through a Terraform variable or TF_VAR_aws_region.

For AWS authentication, prefer OIDC-based dynamic provider credentials over permanent access keys.

2. All ways to set and pass variable values

MethodWhere definedScopeExample
Default valuevariables.tfTerraform configurationdefault = "t3.micro"
terraform.tfvarsRepository / configuration directoryConfigurationinstance_type = "t3.small"
*.auto.tfvarsRepository / configuration directoryConfigurationinstance_type = "t3.medium"
*.tfvars.jsonRepository / configuration directoryConfigurationJSON-formatted inputs
TF_VAR_*Shell environmentCurrent process / runTF_VAR_instance_type
CLI -varTerraform commandSingle run-var="instance_type=t3.large"
CLI -var-fileTerraform commandSingle run-var-file=prod.tfvars
Workspace variableHCP Terraform UI/APIOne workspaceinstance_type = t3.xlarge
Workspace-scoped variable setHCP TerraformSelected workspacesShared configuration
Project-scoped variable setHCP TerraformAll workspaces in projectShared project settings
Global variable setHCP TerraformAll organization workspacesOrganization defaults
Priority variable setHCP TerraformSelected scope, enforced precedenceProtected configuration

You can also create and update workspace variables and variable sets programmatically using the HCP Terraform API or the Terraform tfe provider. These are management methods, not additional precedence levels.

3. Variable scope mapping

Who owns a variable set vs where it applies?

These are separate concepts in modern HCP Terraform.

OwnershipAssignment scopeEffect
Organization-ownedGlobalEvery workspace in the organization
Organization-ownedSpecific projectsAll workspaces in selected projects
Organization-ownedSpecific workspacesOnly selected workspaces
Project-ownedEntire projectAll workspaces in that project
Project-ownedSpecific workspacesOnly selected workspaces within that project

A project does not have a separate ordinary variable-value layer. Project-wide values are provided through variable sets.

Similarly, Teams and Users manage access permissions; they are not variable scopes.

4. Complete HCP Terraform variable precedence

This is the most important reference table.

Highest priority at the top. The first applicable source with a matching variable type and key wins.

RankVariable sourcePriority enabled
1Global variable setYes
2Organization-owned, project-scoped variable setYes
3Organization-owned, workspace-scoped variable setYes
4Project-owned, project-scoped variable setYes
5Project-owned, workspace-scoped variable setYes
6CLI -var / -var-fileN/A
7Local TF_VAR_* for CLI-driven remote runsN/A
8Workspace-specific variableN/A
9Project-owned, workspace-scoped variable setNo
10Project-owned, project-scoped variable setNo
11Organization-owned, workspace-scoped variable setNo
12Organization-owned, project-scoped variable setNo
13Global variable setNo
14*.auto.tfvars filesN/A
15terraform.tfvarsN/A
—Terraform input variable defaultLowest fallback

This is the precedence order published in HashiCorp’s current HCP Terraform documentation. It also explains why ownership and Priority status can matter more than the apparent specificity of the scope.

The table focuses on HCP Terraform’s published tiers. Terraform also supports the JSON variants *.auto.tfvars.json and terraform.tfvars.json; their ordering follows Terraform’s normal variable-file rules.

Five rules worth memorizing

  • A workspace variable overrides normal, non-priority variable sets.
  • Run-specific CLI values override workspace variables, unless a matching Priority Variable Set takes precedence.
  • A Priority Global Variable Set has the highest precedence.
  • Within otherwise equal variable sets, the set whose name sorts first lexically wins. For example, A-settings wins over B-settings.
  • Two global variable sets cannot define conflicting variables of the same key and type.

Unlike the normal non-priority case, an organization-owned priority set can outrank a project-owned priority set, as shown in the ranking.

5. Interactive precedence example

Suppose node_count is defined at several levels for the same workspace.

The CLI override represents a CLI-driven run. The priority example uses the documented HCP Terraform precedence order.

Example: What happens when values conflict?

Sourcenode_count
variables.tf default1
terraform.tfvars2
dev.auto.tfvars3
Normal project variable set5
Workspace variable6

The effective value is 6, because the workspace variable overrides the lower-ranked sources.

If you now run:

terraform plan -var="node_count=7"Code language: JavaScript (javascript)

The effective value becomes 7 in a CLI-driven remote run.

But if a Priority Global Variable Set defines node_count = 8, then the effective value becomes 8, even when the CLI specifies 7.

6. Practical examples for each method

Method A — Values in .tfvars files

terraform.tfvars:

node_count = 2
environment = "development"Code language: JavaScript (javascript)

production.auto.tfvars:

node_count = 3
environment = "production"Code language: JavaScript (javascript)

Terraform automatically reads these files when they are included in the configuration. The *.auto.tfvars values have precedence over terraform.tfvars.

A custom file such as production.tfvars must be explicitly supplied using -var-file rather than relying on automatic loading.

Method B — TF_VAR_* in your terminal

export TF_VAR_node_count=4
export TF_VAR_environment="staging"

terraform planCode language: JavaScript (javascript)

With a CLI-driven HCP Terraform remote run, these inputs can override workspace variables and normal variable sets. They do not replace the provider credentials and other execution-environment settings required in HCP Terraform.

HashiCorp Developer

Method C — Complex values through Workspace Variables

Terraform code:

variable "subnet_ids" {
  type = list(string)
}Code language: PHP (php)

HCP Terraform UI:

Category: Terraform
Key: subnet_ids

Value:
["subnet-abc123", "subnet-def456"]

HCL: EnabledCode language: CSS (css)

For lists, maps and objects, enable the HCL checkbox when entering HCL expressions in the HCP Terraform UI.

HashiCorp Developer

Method D — Set variables through the HCP Terraform Runs API

The Runs API also supports run-specific values. For example, a run creation payload may include:

{
  "data": {
    "type": "runs",
    "attributes": {
      "variables": [
        {
          "key": "node_count",
          "value": "5"
        }
      ]
    },
    "relationships": {
      "workspace": {
        "data": {
          "type": "workspaces",
          "id": "ws-EXAMPLE"
        }
      }
    }
  }
}Code language: JSON / JSON with Comments (json)

These are supplied to a particular run, rather than permanently updating the workspace. Values must be valid HCL literals. This is useful for CI/CD orchestration and automation.

HashiCorp Developer

Method E — Manage HCP Terraform variables using Terraform

You can manage the platform’s variable configuration through the official hashicorp/tfe provider:

resource "tfe_variable" "example" {
  workspace_id = tfe_workspace.example.id

  key      = "instance_type"
  value    = "t3.medium"
  category = "terraform"
}Code language: JavaScript (javascript)

This creates a workspace Terraform variable with the normal workspace-variable precedence. The tfe_variable_set, tfe_project_variable_set, and tfe_workspace_variable_set resources support reusable sets and their assignments.

Terraform Registry

+1

7. Critical difference: Execution mode and workflow

The HCP Terraform execution model affects which sources are available.

Execution / workflowWorkspace variablesVariable setsLocal CLI overrides
VCS-driven remoteYesYesNot supplied by VCS runs
UI-triggered remoteYesYesNot from user’s local shell
CLI-driven remoteYesYesYes
API-driven remoteYesYesRun-specific API values
Agent executionYesYesWhen CLI-driven
Local execution modeNoNoStandard local Terraform precedence

HashiCorp Developer

+2

The Local execution mode exception is especially important. Even though Terraform state is stored in HCP Terraform, HCP Terraform does not evaluate its workspace variables or variable sets for local execution.

Likewise, in CLI-driven remote runs, shell variables on your laptop such as AWS_ACCESS_KEY_ID are not automatically forwarded as remote provider credentials. Configure the execution environment in HCP Terraform or use dynamic authentication.

8. Recommended variable design for AWS infrastructure

For a real infrastructure project, I recommend separating shared configuration, environment-specific settings and authentication.

VariableTypeRecommended scope
default_tagsTerraformOrganization-owned variable set or project set
environmentTerraformWorkspace
aws_regionTerraformProject set or workspace
vpc_cidrTerraformWorkspace
eks_cluster_nameTerraformWorkspace
node_countTerraformWorkspace
TF_LOGEnvironmentWorkspace, only for debugging
TFC_AWS_PROVIDER_AUTHEnvironmentWorkspace or relevant variable set
TFC_AWS_RUN_ROLE_ARNEnvironmentWorkspace or relevant variable set
Database passwordSensitive Terraform or provider environment variableWorkspace or restricted variable set

For AWS dynamic credentials, configure the HCP Terraform OIDC integration and required environment variables such as:

TFC_AWS_PROVIDER_AUTH = true

TFC_AWS_RUN_ROLE_ARN =
arn:aws:iam::123456789012:role/terraform-roleCode language: JavaScript (javascript)

These values configure HCP Terraform authentication rather than declaring application-level input variables.

HashiCorp Developer

9. Other ways values reach Terraform code

Not every value used by Terraform comes from input variables.

Child-module inputs are passed explicitly from the root module:

module "vpc" {
  source = "./modules/vpc"

  cidr = var.vpc_cidr
}Code language: JavaScript (javascript)

Workspace outputs can provide values to another workspace through provider data sources or tfe_outputs:

data "tfe_outputs" "network" {
  organization = "my-org"
  workspace    = "network-prod"
}

output "vpc_id_from_network" {
  value = data.tfe_outputs.network.nonsensitive_values.vpc_id
}Code language: JavaScript (javascript)

These fetched values are resolved through data sources and do not participate in the input-variable precedence table.

Terraform Registry

+1

For Terraform Stacks, variable sets work differently: Stacks reference selected sets explicitly through store blocks, rather than using the workspace precedence hierarchy.

HashiCorp Developer

10. Best practices and common pitfalls

  • Prefer variable sets for shared values and workspace variables for environment-specific values.
  • Avoid duplicate keys across multiple non-priority sets unless overriding them is intentional.
  • Use Priority Variable Sets selectively, because they can prevent workspace teams from overriding values.
  • Do not place secrets in committed .tfvars files.
  • Mark sensitive HCP Terraform values appropriately, while remembering that Sensitive does not guarantee secrets stay out of Terraform state.
  • Use dynamic provider credentials rather than persistent AWS access keys.
  • Check the workspace Variables page for OVERWRITTEN markers when debugging conflicts.
  • Treat changing a widely shared variable set as a potentially multi-workspace infrastructure change.

HashiCorp Developer

+1

Final reference: Which source should I use?

RequirementBest choice
General fallback valueVariable default
Environment configuration maintained in Git.auto.tfvars
Common configuration across organizationGlobal variable set
Shared configuration for one projectProject-scoped variable set
Different value for every workspaceWorkspace variable
Single execution overrideCLI -var / -var-file or run API
Provider credentialsDynamic credentials / environment variables
Centrally enforced configurationPriority variable set, with policy enforcement where necessary
Values from another Terraform deploymentProvider data source / tfe_outputs

The overall rule is that scope determines where a variable applies, while precedence determines which value wins. Ownership and Priority settings are the two HCP Terraform-specific details that make this different from ordinary local Terraform.

Official documentation: HCP Terraform Variables & Precedence, Manage Variables and Variable Sets, and Terraform Input Variable Precedence.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Terraform Cloud (HCP Terraform) — Organization, Project, Workspace, Team & User Mapping

Updated: October 15, 2026 | Based on current HashiCorp documentation Terraform Cloud is now called HCP Terraform. Its architecture separates infrastructure organization from user access management (RBAC)….

Read More

Terraform Cloud vs Terraform Enterprise

Terraform Cloud (now called HCP Terraform) and Terraform Enterprise (TFE) are HashiCorp’s infrastructure automation platforms. The primary difference is their deployment model: Both provide Terraform state management,…

Read More

Top 10 Treasury Management Systems: Features, Pros, Cons & Comparison

Introduction Treasury Management Systems (TMS) are specialized software platforms designed to help organizations manage their cash, liquidity, financial risk, and treasury operations in a structured, secure, and…

Read More

DevOps Best Practices in 2026: What Actually Works at Enterprise Scale

DevOps best practices used to be a fairly settled conversation. Automate everything, ship small changes often, run production the way you developed it. That advice hasn’t stopped…

Read More

Best AI presentation software in 2026: 4 tools DevOps teams can trust

Slide decks used to devour whole afternoons. As of June 2026, AIApps benchmarks show that today’s AI presentation software cuts build time by up to 80 percent,…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x