hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.42k stars 9.51k forks source link

confusing output when multiple required_version attributes set #22239

Open mattnworb opened 5 years ago

mattnworb commented 5 years ago

If you run terraform 0.12upgrade to upgrade a module or a repo to the 0.12 syntax, any existing terraform.required_version attributes are not changed - but 0.12upgrade will add a new versions.tf file to the directory with:

terraform {
  required_version = ">= 0.12"
}

After successfully executing terraform 0.12upgrade, a subsequent terraform init will fail with

Error: Unsupported Terraform Core version

This configuration does not support Terraform version 0.12.5. To proceed,
either choose another supported Terraform version or update the root module's
version constraint. Version constraints are normally set for good reason, so
updating the constraint may lead to other errors or unexpected behavior.

It took me a while to figure out that this was caused by a terraform block in main.tf which was also setting the required_version like below.

I think ideally terraform 0.12upgrade would remove any now-out-of-date required_version blocks when it mutates the files, and/or the error message about "does not support Terraform version x.y.z" could point to exactly which line in which .tf file was used as the required_version source of truth, to aid in finding the problem.

Terraform Version

Terraform v0.12.5

Terraform Configuration Files

in main.tf, before running terraform 0.12upgrade:

terraform {
  backend "gcs" {
    ...
  }

  required_version = "~> 0.11.13"
}

locals {
  ...
}
...

after running terraform 0.12upgrade, the main.tf above is unchanged, but versions.tf is added with:

terraform {
  required_version = ">= 0.12"
}

Debug Output

https://gist.github.com/mattnworb/327d9388a4ab687e72f55f959e121e07

note that the trace output does not indicate which line/file any discovered instances of recognized_version are used from

Steps to Reproduce

  1. Have an existing repo with terraform.required_version set to "~> 0.11.13" or something similar in a file not named versions.tf.
  2. Run terraform 0.12upgrade
  3. Run terraform init, observe the error output that the version is not supported.
teamterraform commented 5 years ago

Hi @mattnworb ! Thanks for opening this issue, and sorry for the confusing behavior.

I re-titled the issue to focus on the output. We aren't going to make many more changes to the behavior of terraform 0.12upgrade, but we can and should print a more informative error message in this particular instance - similarly, in cases where a root module and child module both have a required_version - to let the user know where the version is set.

mattnworb commented 5 years ago

@teamterraform thanks, that sounds like it would be very helpful 👍