Safeguard
Developer Tools•

How should you store Terraform registry credentials in CI?

Terraform can read host tokens from TF_TOKEN environment variables instead of the CLI config file. How the naming works, and a safer setup for pipelines.

Safeguard Security Team
3 min read

In CI, prefer a host specific TF_TOKEN_ environment variable, fed from your secret manager, over a credentials block saved in a CLI config file. It keeps the token out of files on the runner and lets you scope it to one host. This feature needs Terraform 1.2.0 or later.

The two ways to supply a token

The CLI configuration file accepts a credentials block for each host, with a token value. If you work interactively, terraform login can write that file for you. The alternative is an environment variable named TF_TOKEN_ followed by the host name with periods turned into underscores. For example, TF_TOKEN_app_terraform_io supplies the bearer token for app.terraform.io.

Names with non ASCII characters must be converted to punycode with an ACE prefix first. Hyphens in host names can be encoded as double underscores. If several variables evaluate to the same host, Terraform picks the one defined last.

Why the environment variable suits pipelines

PropertyConfig fileEnvironment variable
Token written to diskYesNot by Terraform
Per hostYesYes
Easy to inject from a secret storeNeeds a file stepDirect
Visible in process listings or logsRarelyPossible if you print the environment

The environment variable still needs care: do not echo the environment in debug steps, and restrict who can read job logs.

A credentials helper

Terraform also supports a credentials_helper block that points to a program for retrieving credentials. It is consulted only for hosts not set in a credentials block. Terraform does not ship any helpers, so you write or install your own.

Token type matters

For HCP Terraform and Terraform Enterprise, the token for command line use must be a user token or a team token. Organization tokens cannot be used for command line Terraform actions. Prefer a team token scoped to the workspaces a pipeline needs.

Steps

  1. Create a team token with the narrowest access that works.
  2. Store it in your CI secret store.
  3. Expose it only to the job that runs Terraform, as the correct TF_TOKEN_ variable.
  4. Rotate it on a schedule and after any suspected exposure.
  5. Make sure the host name in your module sources and backend matches the credentials host, as the documentation requires.

Quick check

Does the variable name use dots?

No. Periods in the host name become underscores.

Can I use the same token for several hosts?

Each host has its own variable, so set one per host.

Are organization tokens fine for CLI runs?

Not for command line Terraform actions, according to the documentation.

Safeguard offers SBOM and dependency scanning and policy gates that can block a pull request on findings, and you can try a free scan at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.