Terraform: Trying to plan or apply with an incorrect `-target` has no error message

Created on 2 Nov 2020  路  3Comments  路  Source: hashicorp/terraform

Terraform Version

Terraform v0.13.5

Terraform Configuration Files

resource "null_resource" "test" {}

Debug Output

TF_LOG=trace terraform apply -target asdf
2020/11/02 10:24:04 [INFO] Terraform version: 0.13.5  
2020/11/02 10:24:04 [INFO] Go runtime version: go1.14.10
2020/11/02 10:24:04 [INFO] CLI args: []string{"/usr/local/bin/terraform", "apply", "-target", "asdf"}
2020/11/02 10:24:04 [DEBUG] Attempting to open CLI config file: /Users/<username>/.terraformrc
2020/11/02 10:24:04 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2020/11/02 10:24:04 [DEBUG] checking for credentials in "/Users/<username>/.terraform.d/plugins"
2020/11/02 10:24:04 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2020/11/02 10:24:04 [DEBUG] will search for provider plugins in /Users/<username>/.terraform.d/plugins
2020/11/02 10:24:04 [TRACE] getproviders.SearchLocalDirectory: /Users/<username>/.terraform.d/plugins is a symlink to /Users/w107566/.terraform.d/plugins
2020/11/02 10:24:04 [DEBUG] ignoring non-existing provider search directory /Users/<username>/Library/Application Support/io.terraform/plugins
2020/11/02 10:24:04 [DEBUG] ignoring non-existing provider search directory /Library/Application Support/io.terraform/plugins
2020/11/02 10:24:04 [INFO] CLI command args: []string{"apply", "-target", "asdf"}
Usage: terraform apply [options] [DIR-OR-PLAN]

  Builds or changes infrastructure according to Terraform configuration
  files in DIR.

  By default, apply scans the current directory for the configuration
  and applies the changes appropriately. However, a path to another
  configuration or an execution plan can be provided. Execution plans can be
  used to only execute a pre-determined set of actions.

Options:

  -auto-approve          Skip interactive approval of plan before applying.

  -backup=path           Path to backup the existing state file before
                         modifying. Defaults to the "-state-out" path with
                         ".backup" extension. Set to "-" to disable backup.

  -compact-warnings      If Terraform produces any warnings that are not
                         accompanied by errors, show them in a more compact
                         form that includes only the summary messages.

  -lock=true             Lock the state file when locking is supported.

  -lock-timeout=0s       Duration to retry a state lock.

  -input=true            Ask for input for variables if not directly set.

  -no-color              If specified, output won't contain any color.

  -parallelism=n         Limit the number of parallel resource operations.
                         Defaults to 10.

  -refresh=true          Update state prior to checking for differences. This
                         has no effect if a plan file is given to apply.

  -state=path            Path to read and save state (unless state-out
                         is specified). Defaults to "terraform.tfstate".

  -state-out=path        Path to write state to that is different than
                         "-state". This can be used to preserve the old
                         state.

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

  -var 'foo=bar'         Set a variable in the Terraform configuration. This
                         flag can be set multiple times.

  -var-file=foo          Set variables in the Terraform configuration from
                         a file. If "terraform.tfvars" or any ".auto.tfvars"
                         files are present, they will be automatically loaded.

Crash Output

N/A

Expected Behavior

An error message should be displayed explaining why the apply failed. Such as:

Target resource "asdf" [ could not be resolved | does not exist | is invalid | etc ]

Actual Behavior

Help output is displayed, with no explanation as to why.

Steps to Reproduce

  1. echo 'resource "null_resource" "test" {}' > main.tf
  2. terraform init
  3. terraform apply -target asdf
  4. Observe help output with no explanation of why the given arguments are invalid
  5. terraform apply -target null_resource.test
  6. terraform apply is successful

Additional Context

This behavior is confusing and leads to poor user experience. The user cannot be sure if they gave the wrong target, or if they made some sort of syntactical error elsewhere in the command arguments.

bug cli explained v0.13

Most helpful comment

Hi @williams-brian, thanks for reporting this. It is a very unhelpful way of failing. I can confirm that this issue exists in Terraform 0.13 and for most of the 0.12 series too.

I think the underlying issue is to do with how we're using the CLI package. Our parser for target addresses is here: https://github.com/hashicorp/terraform/blob/3ed08e3566b6e4b9842b26ffdd43426596f9c51e/command/flag_kv.go#L58-L61

This error is no longer being rendered to the UI. It was in Terraform 0.12.0:

Screen Shot 2020-11-02 at 12 11 40

That seems to have stopped working sometime before 0.12.10. Even then, the error message is drowned out by the long usage help text, so I don't think that's particularly useful.

One option here could be to delay the parsing of the target address until after this parsing stage, allowing us to give more helpful error messages.

All 3 comments

Hi @williams-brian, thanks for reporting this. It is a very unhelpful way of failing. I can confirm that this issue exists in Terraform 0.13 and for most of the 0.12 series too.

I think the underlying issue is to do with how we're using the CLI package. Our parser for target addresses is here: https://github.com/hashicorp/terraform/blob/3ed08e3566b6e4b9842b26ffdd43426596f9c51e/command/flag_kv.go#L58-L61

This error is no longer being rendered to the UI. It was in Terraform 0.12.0:

Screen Shot 2020-11-02 at 12 11 40

That seems to have stopped working sometime before 0.12.10. Even then, the error message is drowned out by the long usage help text, so I don't think that's particularly useful.

One option here could be to delay the parsing of the target address until after this parsing stage, allowing us to give more helpful error messages.

Being new to Terraform I've recently spent two hours (and logged an issue https://github.com/hashicorp/terraform/issues/27032) just to find out that the value that I'm using the the -target option is invalid.

Fixing this item would make life a bit easier for new Terraform users.

Thank you!

This issue still exists on Terraform 0.14.

Was this page helpful?
0 / 5 - 0 ratings