Skip to content

Add variables to all commands that load configuration#38276

Merged
dbanck merged 20 commits intomainfrom
dbanck/command-vars
Mar 17, 2026
Merged

Add variables to all commands that load configuration#38276
dbanck merged 20 commits intomainfrom
dbanck/command-vars

Conversation

@dbanck
Copy link
Copy Markdown
Member

@dbanck dbanck commented Mar 13, 2026

This PR updates most Terraform commands and lets them accept variables via the CLI or a backend.

The new resolveConstVariables helper checks if const variables are present in the configuration, but don't have a value yet. In that case we try to fetch them via a backend. This will only work for backends that implement the ConstVariableSupplier interface. (Which the cloud backend does)

The precedence is the same as for existing commands: a CLI supplied value will override a value from a workspace.

Command CLI vars Backend injects vars
apply yes yes
plan yes yes
refresh yes yes
import yes yes
console yes yes
query yes yes
init yes no
test yes no
show yes no
validate new 🎉 new 🎉
graph new 🎉 new 🎉
providers new 🎉 new 🎉
providers lock new 🎉 new 🎉
providers mirror new 🎉 new 🎉
providers schema new 🎉 new 🎉
modules new 🎉 new 🎉
state mv new 🎉 new 🎉
state pull new 🎉 new 🎉
state push new 🎉 new 🎉
state replace-provider new 🎉 new 🎉
state rm new 🎉 new 🎉
taint new 🎉 new 🎉
get new 🎉 new 🎉

Review Notes: The commits allow stepping through the changes one by one

Target Release

1.15.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

@dbanck dbanck force-pushed the dbanck/command-vars branch 2 times, most recently from 61f3f54 to fe56406 Compare March 13, 2026 17:07
@dbanck dbanck marked this pull request as ready for review March 13, 2026 17:07
@dbanck dbanck requested review from a team as code owners March 13, 2026 17:07
dbanck added 20 commits March 17, 2026 12:49
Instead of passing a "magic" boolean as third parameter, we now have two
functions `ParseConstVariableValues` and `ParseVariableValues`.
We never want to stub const variables, so we will always try to get
values for them. The cloud backend now always fetches variable values so
const vars can be satisfied.
The new `resolveConstVariables` helper checks if const variables are
present in the configuration, but don't have a value yet. In that case
we try to fetch them via a backend. This will only work for backends
that implement the ConstVariableSupplier interface. (Which the `cloud`
backend does)

The precedence is the same as for existing commands: a CLI supplied
value will override a value from a workspace
* modules
* providers
* providers lock
* providers mirror
* validate
* state mv
* state pull
* state push
* state replace provider
* state rm
* taint
* get
@dbanck dbanck force-pushed the dbanck/command-vars branch from fe56406 to c25adc4 Compare March 17, 2026 11:50
Copy link
Copy Markdown
Contributor

@DanielMSchmidt DanielMSchmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job 🎉

defer done()

path, err := ModulePath(cmdFlags.Args())
path, err := ModulePath(nil)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this nill now when we added the args before?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now do the check for extra args in the arguments package. The ModulePath() call now is only used to get the path. I think in the long run we can fully replace it

"github.com/hashicorp/terraform/internal/tfdiags"
)

// ConstVariableSupplier is an optional interface that backends can implement
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make sure to document this properly, could be confusing for people on the remote backend if their backend does not load the vars.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point. I'll look for a good place to document this

@dbanck dbanck merged commit 977a0fb into main Mar 17, 2026
7 checks passed
@dbanck dbanck deleted the dbanck/command-vars branch March 17, 2026 15:41
@dbanck dbanck mentioned this pull request Mar 18, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants