Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform console: Option to evaluate in a planned state #34342

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

apparentlymart
Copy link
Contributor

@apparentlymart apparentlymart commented Dec 1, 2023

Previously terraform console always evaluated in a kinda strange context where resource instance data comes from the prior state, but other derived of values end up being calculated dynamically based on the current configuration, which is okay for simple cases but can be confusing if the configuration has changed significantly since the most recent apply, or if there haven't yet been any applied changes.

Now we'll allow an optional new mode where Terraform runs the normal plan phase (as if running terraform plan) and then uses the resulting planned state as the basis for evaluation, allowing evaluation against a partial approximation of what the world ought to look like if these changes were applied, without having to actually apply them first.

As with the previous use of the eval walk, it's possible that an erroneous situation will still produce a partial evaluation scope, and so the console still allows evaluation against that scope but with a caveat that it might produce unexpected results. In practice this can be useful for debugging situations like when unknown values block planning of an object, to allow inspection of the values that are contributing to that blocked planning operation even though the plan is not yet complete.


Working on #34338 yesterday, and realizing that the test language has requirements that are a mix of some requirements from the stacks language and some requirements from terraform console, made me recall an old idea of allowing terraform console to run in the context of a generated plan as a way to have more information available in scope, and as an additional option for debugging situations where unknown values are ending up in unfortunate places.

Since that PR already did most of the work to make this possible, this PR is a little extra value-add from #34338 to fill a little gap we've been hoping to fill for a long while now.

Here's an (admittedly rather contrived) example of using terraform console -plan in a configuration which cannot fully complete planning due to an unknown value in an inconvenient location:

$ terraform console -plan

│ Error: Invalid count argument

│   on deferred-changes-module-exp.tf line 6, in module "child":
│    6:   count  = length(null_resource.a.id)

│ The "count" value depends on resource attributes that cannot be determined
│ until apply, so Terraform cannot predict how many instances will be created.
│ To work around this, use the -target argument to first apply only the
│ resources that the count depends on.



│ Warning: Due to the problems above, some expressions may produce unexpected results.


> null_resource.a
{
  "id" = (known after apply)
  "triggers" = tomap(null) /* of string */
}
>  

Although hopefully soon we'll finish working on #30937 and thus the question of where an unknown value originated will become less significant, I think it's still pretty helpful to be able to peek into the details of what values contributed to a plan, such as with the situations I was aiming to improve on in #34312: why is my postcondition failing? why is the provider proposing to replace this object?

Copy link
Contributor

@alisdair alisdair left a comment

Choose a reason for hiding this comment

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

This is delightful! Just a tiny question inline.

internal/command/console.go Show resolved Hide resolved
Previously "terraform console" always evaluated in a kinda strange context
where resource instance data comes from the prior state, but other derived
of values end up being calculated dynamically based on the current
configuration, which is okay for simple cases but can be confusing if the
configuration has changed significantly since the most recent apply, or
if there haven't yet been any applied changes.

Now we'll allow an optional new mode where Terraform runs the normal plan
phase (as if running "terraform plan") and then uses the resulting
_planned state_ as the basis for evaluation, allowing evaluation against
a partial approximation of what the world ought to look like if these
changes were applied, without having to actually apply them first.

As with the previous use of the eval walk, it's possible that an erroneous
situation will still produce a partial evaluation scope, and so the
console still allows evaluation against that scope but with a caveat that
it might produce unexpected results. In practice this can be useful for
debugging situations like when unknown values block planning of an object,
to allow inspection of the values that are contributing to that blocked
planning operation even though the plan is not yet complete.
@apparentlymart apparentlymart merged commit 7055650 into main Dec 1, 2023
7 checks passed
@apparentlymart apparentlymart deleted the f-cmd-graph-plan branch December 1, 2023 22:35
Copy link

github-actions bot commented Dec 1, 2023

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

Copy link

github-actions bot commented Jan 1, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants