terraform apply ignores -target= when using a plan file

Created on 8 Jun 2017  ยท  6Comments  ยท  Source: hashicorp/terraform

Terraform Version

$ terraform -version
Terraform v0.9.8

Affected Resource(s)

Probably terraform core

Debug Output

$ AWS_DEFAULT_PROFILE="webops-management" aws-profile terraform plan -out=terraform.tfplan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

(...)

Your plan was also saved to the path below. Call the "apply" subcommand
with this plan file and Terraform will exactly execute this execution
plan.

Path: terraform.tfplan

- module.continuous-integration.aws_iam_role_policy.ci_jobs

~ module.continuous-integration.ci_infra_asg.aws_autoscaling_group.asg
    load_balancers.#: "0" => "1"
    load_balancers.0: "" => ""


Plan: 0 to add, 1 to change, 1 to destroy.

$ AWS_DEFAULT_PROFILE="webops-management" aws-profile terraform apply -target=module.continuous-integration.ci_infra_asg.aws_autoscaling_group.asg terraform.tfplan
module.continuous-integration.aws_iam_role_policy.ci_jobs: Destroying... (ID: jenkins-role:jenkins-jobs-00ae8e9c81b3a899bfc11ee82b)
module.continuous-integration.aws_iam_role_policy.ci_jobs: Destruction complete
module.continuous-integration.ci_infra_asg.aws_autoscaling_group.asg: Modifying... (ID: <REDACTED>)
  load_balancers.#: "0" => "1"
  load_balancers.0: "" => ""
module.continuous-integration.ci_infra_asg.aws_autoscaling_group.asg: Modifications complete (ID: <REDACTED>)

Apply complete! Resources: 0 added, 1 changed, 1 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path:

Outputs:

role_name = jenkins-role

Expected Behavior

-target= should have acted as a filter as per the help text:

  -target=resource       Resource to target. Operation will be limited to this
                         resource and its dependencies. This flag can be used
                         multiple times.

Actual Behavior

A resource that was not targeted was deleted.

Steps to Reproduce

  1. Generate a plan file with more than one resource affected
  2. Apply the plan file, but restrict -target= to only one of the resources
bug cli

Most helpful comment

Hi all! Sorry for this confusion.

It is indeed a bug that Terraform does not produce an error in this case. apply is specified with many of the same arguments as plan because when used without a plan file it implicitly runs a plan inline, and thus these options are used. We should check for their use in the case where a plan is _not_ being created (because one was already provided) and rejected them.

We would not apply further targets here at plan time, since it is considered a bug if Terraform does anything other than exactly what is shown in the plan; to make a more restricted set of changes, run plan again with a different set of targets.

All 6 comments

I just had the same problem: given the explanation of plan I feel i could have expected it though

Your plan was also saved to the path below. Call the "apply" subcommand with this plan file and Terraform will exactly execute this execution plan.

If the help text for target just explains that it does not work when combined with a plan I would be happy.

I would say that if Terraform will ignore --target when given a plan file, it should instead refusing to run and explain why.

Anything less than that I would still consider a bug.

Ideally it would just use --target to filter down the changeset in the plan file.

Hi all! Sorry for this confusion.

It is indeed a bug that Terraform does not produce an error in this case. apply is specified with many of the same arguments as plan because when used without a plan file it implicitly runs a plan inline, and thus these options are used. We should check for their use in the case where a plan is _not_ being created (because one was already provided) and rejected them.

We would not apply further targets here at plan time, since it is considered a bug if Terraform does anything other than exactly what is shown in the plan; to make a more restricted set of changes, run plan again with a different set of targets.

Hello! :robot:

This issue relates to an older version of Terraform that is no longer in active development, and because the area of Terraform it relates to has changed significantly since the issue was opened we suspect that the issue is either fixed or that the circumstances around it have changed enough that we'd need an updated issue report in order to reproduce and address it.

If you're still seeing this or a similar issue in the latest version of Terraform, please do feel free to open a new bug report! Please be sure to include all of the information requested in the template, even if it might seem redundant with the information already shared in _this_ issue, because the internal details relating to this problem are likely to be different in the current version of Terraform.

Thanks!

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phinze picture phinze  ยท  167Comments

glenjamin picture glenjamin  ยท  112Comments

shubhambhartiya picture shubhambhartiya  ยท  72Comments

kforsthoevel picture kforsthoevel  ยท  86Comments

ncraike picture ncraike  ยท  77Comments