CI/CD and Automation Workflows · Topic 2 of 2

Dynamic Credentials and Variable Overrides

Terraform Professional 002: share dynamic provider credentials across teams and workspaces, scope OIDC trust, and predict which variable value wins a run.

  • 5 min
In a nutshell
  • Dynamic credentials are issued per plan and apply, then discarded.
  • TFC_AWS_PROVIDER_AUTH must be true, and it has no TFC_DEFAULT_ form.
  • Always check audience and organization in the trust policy.

What it is

Dynamic provider credentials replace stored cloud keys with temporary credentials issued for each run. HCP Terraform proves its identity to the cloud using a signed workload identity token, and the credentials are discarded when the run ends.

Variable overrides decide which value wins when several sources set the same variable. Both topics come up in pipelines, since they control what a run can reach and what it deploys.

Plan or apply startsWorkload identity tokenorg, project, workspace, phaseCloud checks trustTemporary credentialsRun endscredentials discarded

How it works

Configuring one set

Set these as environment variables on the workspace or in a variable set.

ProviderEnableIdentity
AWSTFC_AWS_PROVIDER_AUTH=trueTFC_AWS_RUN_ROLE_ARN
AzureTFC_AZURE_PROVIDER_AUTH=trueTFC_AZURE_RUN_CLIENT_ID
Google CloudTFC_GCP_PROVIDER_AUTH=trueTFC_GCP_RUN_SERVICE_ACCOUNT_EMAIL plus workload provider

Azure also needs ARM_SUBSCRIPTION_ID and ARM_TENANT_ID, set as variables or in the provider block. Do not also set static credentials such as ARM_CLIENT_ID or GOOGLE_CREDENTIALS.

Scoping trust

The token's subject claim identifies the run, in the form organization:ORG:project:PROJECT:workspace:WORKSPACE:run_phase:PHASE.

An AWS trust policy should check at least the audience and the organization, or other HCP Terraform organizations could gain access. Scoping further to a project or workspace is what separates teams.

Use run_phase:* with StringLike to match both phases. Azure matches the subject as an exact string, so each phase needs its own federated credential.

Separating plan from apply

VariableUsed forFalls back to
TFC_AWS_PLAN_ROLE_ARNPlan phaseTFC_AWS_RUN_ROLE_ARN
TFC_AWS_APPLY_ROLE_ARNApply phaseTFC_AWS_RUN_ROLE_ARN

This lets the plan use a read-only role while only the apply can change resources. Each role's trust policy is scoped to its own phase.

Sharing across teams and projects

NeedWhere to set the variables
One workspace has its own roleWorkspace variables
Every workspace in a team's project uses one roleProject-scoped variable set
Selected workspaces share a roleVariable set applied to those workspaces
One or two workspaces differWorkspace variable, which overrides the set

A workspace variable overrides a non-priority variable set with the same key. A priority variable set overrides workspace variables, which is useful when a platform team must enforce a value.

Several configurations in one workspace

Tagging lets one workspace assume several roles, for example in different accounts. Add a tag to each variable name, and use TFC_DEFAULT_ for values that all configurations share.

TFC_AWS_PROVIDER_AUTH_PROD=true
TFC_AWS_RUN_ROLE_ARN_PROD=arn:aws:iam::111111111111:role/prod
variable "tfc_aws_dynamic_credentials" {
  type = object({
    default = object({ shared_config_file = string })
    aliases = map(object({ shared_config_file = string }))
  })
}

provider "aws" {
  alias               = "PROD"
  shared_config_files = [var.tfc_aws_dynamic_credentials.aliases["PROD"].shared_config_file]
}

Tags may hold letters, numbers and underscores, and TYPE is reserved. TFC_AWS_PROVIDER_AUTH has no default form, so each tagged configuration needs its own. An explicit tagged value beats its TFC_DEFAULT_ equivalent.

Outside HCP Terraform, the same idea works with CI identity. GitHub Actions, for example, can exchange an OIDC token for cloud credentials when the job has the id-token: write permission.

Variable precedence with the CLI

From highest to lowest:

  1. -var and -var-file, in command-line order, so the last one wins
  2. *.auto.tfvars and *.auto.tfvars.json, in lexical order, so the last name wins
  3. terraform.tfvars.json
  4. terraform.tfvars
  5. TF_VAR_name environment variables
  6. The default in the variable block

Only the auto-loaded files are read without being asked. A file named prod.tfvars needs -var-file=prod.tfvars.

Tracing a value

variable "size" {
  type    = string
  default = "small"
}

Sources: TF_VAR_size=medium, terraform.tfvars says large, a.auto.tfvars says xl, b.auto.tfvars says 2xl, and prod.tfvars says 3xl.

  • terraform plan gives 2xl, because b sorts after a and auto files beat terraform.tfvars.
  • terraform plan -var-file=prod.tfvars gives 3xl.
  • terraform plan -var-file=prod.tfvars -var=size=4xl gives 4xl, because the later flag wins.

The winning value replaces the whole variable, so maps from different sources are not merged. Use merge() in a local if you need that.

Precedence in HCP Terraform

From highest to lowest: priority variable sets, CLI arguments, local TF_VAR_ variables, workspace variables, project or workspace-scoped variable sets, global sets, *.auto.tfvars, then terraform.tfvars.

So a workspace variable beats a committed *.auto.tfvars file here, and -var beats a workspace variable unless a priority set holds the key. Terraform Enterprise ignores terraform.tfvars.

How the exam asks about this

  • "Plans must use read-only access while applies can write." Set TFC_AWS_PLAN_ROLE_ARN and TFC_AWS_APPLY_ROLE_ARN.
  • "Every workspace in a team's project must assume the same role." Use a project-scoped variable set.
  • "terraform.tfvars and prod.auto.tfvars set the same variable." prod.auto.tfvars wins.

What to remember for the exam

  • Dynamic credentials are issued per plan and apply, then discarded.
  • TFC_AWS_PROVIDER_AUTH must be true, and it has no TFC_DEFAULT_ form.
  • Always check audience and organization in the trust policy.
  • Tagged variables with aliased providers give one workspace several identities.
  • Auto files beat terraform.tfvars; command-line flags beat both.
  • In HCP Terraform, priority variable sets beat even command-line values.
Watch out

A priority variable set does not stop a configuration from hardcoding a value in its own arguments. Use policy checks or run tasks when the rule must be strictly enforced.

Common mistakes

Trusting only the audience claim. The default audience is shared by every organization on the platform. Check the organization in the subject as well, and narrow it to the project or workspace.

Forgetting the default mapping. Once you add extra configurations, every provider needs its authentication mapped, including the default one.

Expecting prod.tfvars to load automatically. Only terraform.tfvars and *.auto.tfvars files load by themselves. Anything else needs -var-file.

Check yourself

  • How do you give one workspace two AWS roles?

    Set tagged variables such as TFC_AWS_PROVIDER_AUTH_PROD, declare tfc_aws_dynamic_credentials, and point each aliased provider at its entry under aliases.

  • Which wins locally, a.auto.tfvars or z.auto.tfvars?

    z.auto.tfvars, because auto files load in lexical order and later values override earlier ones.

  • Does a workspace variable beat an auto.tfvars file in HCP Terraform?

    Yes. Workspace variables rank above both *.auto.tfvars and terraform.tfvars in HCP Terraform.

Topic 2 of 2 in CI/CD and Automation Workflows

Test yourself on CI/CD and Automation Workflows

The full HashiCorp Terraform Infrastructure Engineer Professional question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.