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 plan" should produce an error when given a plan file #19235

Closed
eedwards-sk opened this issue Oct 31, 2018 · 7 comments
Closed

"terraform plan" should produce an error when given a plan file #19235

eedwards-sk opened this issue Oct 31, 2018 · 7 comments
Assignees
Milestone

Comments

@eedwards-sk
Copy link

eedwards-sk commented Oct 31, 2018

Terraform Version

v0.11.10

Terraform Configuration

s3 backend with terraform files containing aws data sources

Actual Behavior

running terraform plan <plan_name> updates the remote state

Expected Behavior

running terraform plan <plan_name> does not update the remote state

Steps to Reproduce

  1. terraform init with s3 backend config (remote state file does not exist)
  2. terraform plan -out=tfplan . (remote state file does not exist)
  3. terraform plan tfplan (state file now exists)

Additional Context

running inside concourse ci pipeline

according to documentation, state should not be updated on a plan, e.g.:
https://www.terraform.io/guides/running-terraform-in-automation.html

Although the plan step updates the state to match real resources, thus ensuring an accurate plan, the updated state is not persisted, and so this command can safely be used to produce "throwaway" plans that are created only to aid in code review.

https://www.terraform.io/docs/commands/plan.html

This command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state.

I'm trying to use the terraform plan <plan_name> command to view an existing plan, so that an operator can confirm the changes

I want to use -refresh=true because the operator should know what the actual effects will be when ran

However, the state file should not be updated when viewing the plan, instead it should refresh and then 'throwaway' the state, correct?

@eedwards-sk eedwards-sk changed the title terraform plan updates the statefile viewing a terraform plan updates the statefile Nov 2, 2018
@eedwards-sk eedwards-sk changed the title viewing a terraform plan updates the statefile 0.11.10 viewing a terraform plan updates the statefile Nov 2, 2018
@eedwards-sk
Copy link
Author

Is there anything I can do to help surface this issue better?

As it stands, I cannot use "viewing planfiles" in my workflow, or I risk unintended changes to the remote state.

@apparentlymart
Copy link
Contributor

Hi @eedwards-sk! Sorry for this strange behavior, and thanks for reporting it.

Passing an already-existing plan file to terraform plan is, I think, not something that is intentionally allowed but rather something that came as a consequence of how some code is shared between the various main Terraform commands. The behavior is, therefore, strange and unexpected. I expect the way we will resolve this issue is to make terraform plan tfplan generate an error, since that command doesn't really make sense within the intended Terraform workflow.

To view a plan file that was already created, you can use terraform show tfplan instead. That will just render the plan to the terminal, in a similar way to what terraform plan generated in the first place.

@eedwards-sk
Copy link
Author

eedwards-sk commented Nov 2, 2018

@apparentlymart

Thanks so much for the response and clarity over the workflow. I'm trying to build a CI pipeline with a 'view plan' step, so this was throwing quite the wrench in it. I'm excited to try the show command instead.

Generating an error when running against a plan file would definitely help, as would updating the docs on the plan command. That's where I mostly got set on this trail, specifically these two lines from https://www.terraform.io/docs/commands/plan.html:

Usage: terraform plan [options] [dir-or-plan]

If the command is given an existing saved plan as an argument, the command will output the contents of the saved plan. In this scenario, the plan command will not modify the given plan. This can be used to inspect a planfile.

Edit: also the output of terraform plan --help:

Usage: terraform plan [options] [DIR-OR-PLAN]

  Generates an execution plan for Terraform.

  This execution plan can be reviewed prior to running apply to get a
  sense for what Terraform will do. Optionally, the plan can be saved to
  a Terraform plan file, and apply can take this plan file to execute
  this plan exactly.

  If a saved plan is passed as an argument, this command will output
  the saved plan contents. It will not modify the given plan.

Thanks again.

@apparentlymart
Copy link
Contributor

Thanks for pointing out the docs there. I'm not sure what is the story behind that; perhaps this used to be the way to do this prior to the implementation of the terraform show command and the docs have grown outdated.

I'm going to rescope this issue slightly to cover updating that documentation and removing what seems to be vestiges of an earlier usage that is no longer fully functional.

@apparentlymart apparentlymart changed the title 0.11.10 viewing a terraform plan updates the statefile "terraform plan" should produce an error when given a plan file Nov 2, 2018
@apparentlymart
Copy link
Contributor

From a quick inspection of the code, I see that the error message I mentioned is already in place in master ready to be included in the forthcoming v0.12.0 release:

if planFileReader != nil {
c.showDiagnostics(tfdiags.Sourceless(
tfdiags.Error,
"Invalid configuration directory",
fmt.Sprintf("Cannot pass a saved plan file to the 'terraform plan' command. To apply a saved plan, use: terraform apply %s", configPath),
))
return 1
}

So the remaining work here is to get the docs updated. Since the behavior change here is already included in the v0.12.0 scope I'm going to add this issue to the v0.12.0 milestone to represent the need to update the docs as well before release.

@elliot-resdiary
Copy link

@apparentlymart

Passing an already-existing plan file to terraform plan is, I think, not something that is intentionally allowed but rather something that came as a consequence of how some code is shared between the various main Terraform commands.

The documentation for the plan command continues to state the following:

If the command is given an existing saved plan as an argument, the command will output the contents of the saved plan. In this scenario, the plan command will not modify the given plan. This can be used to inspect a planfile.

However, it seems this behavior was made unavailable in 0.12 and terraform show displays a lower resolution view of the plan than what's shown when the terraform plan command was run previously. Has this functionality been lost?

@ghost
Copy link

ghost commented Sep 24, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants