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 reports incompatible version pointing at the last "terraform.required_version" line, even if it is a previous line which is the unmatched constraint #25833

Closed
dcmoeller opened this issue Aug 13, 2020 · 12 comments · Fixed by #25898
Labels
bug explained a Terraform Core team member has described the root cause of this issue in code

Comments

@dcmoeller
Copy link

After upgrading to Terraform 0.13.0 my first invocation fails with

Error: Unsupported Terraform Core version

  on ../../common/google-cloud-platform.tf line 3, in terraform:
   3: 	required_version = "~> 0.12.0"

This configuration does not support Terraform version 0.13.0. [...]

Here is a snippet from mentioned google-cloud-platform.tf file:

terraform {
        required_version = "~> 0.12.0"
}

OK, let's try it with required_version = "~> 0.13.0":

Error: Unsupported Terraform Core version

  on ../../common/google-cloud-platform.tf line 3, in terraform:
   3: 	required_version = "~> 0.13.0"

This configuration does not support Terraform version 0.13.0. [...]

Weird.
Surprisingly all the following yields equivalent negative results:

  • required_version = ">= 0.13.0"
  • required_version = ">= 0.12.0"
  • required_version = "> 0.12.0"

Am I missing something, or is this a bug?

Regards

Terraform Version

$ terraform version
Terraform v0.13.0

Debug Output

The same output as reported above.

Additional Context

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS"
@dcmoeller dcmoeller added bug new new issue not yet triaged labels Aug 13, 2020
@alisdair
Copy link
Contributor

I am unable to reproduce this issue, and can't see how it could be happening. Can you provide a small reproduction configuration which demonstrates the problem, along with a list of commands you are running?

Attempted reproduction

This works as expected:

  1. Create a file at a/b/main.tf:

    module "common" {
      source = "../../common"
    }
  2. Create a file at common/google-cloud-platform.tf:

    terraform {
      required_version = "~> 0.12.0"
    }
  3. Initialize the main module:

    $ cd a/b/ && terraform init
    Initializing modules...
    
    Error: Unsupported Terraform Core version
    
      on ../../common/google-cloud-platform.tf line 2, in terraform:
       2:   required_version = "~> 0.12.0"
    
    Module module.common (from ../../common) does not support Terraform version
    0.13.0. To proceed, either choose another supported Terraform version or
    update this version constraint. Version constraints are normally set for good
    reason, so updating the constraint may lead to other errors or unexpected
    behavior.
  4. Update the version constraint in common/google-cloud-platform.tf to "~> 0.13.0"

  5. Rerun init:

    $ terraform init
    Initializing modules...
    
    Initializing the backend...
    
    Initializing provider plugins...
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.

@alisdair alisdair added waiting for reproduction unable to reproduce issue without further information and removed new new issue not yet triaged labels Aug 13, 2020
@dcmoeller
Copy link
Author

dcmoeller commented Aug 14, 2020

Hi Alisdair,

thanks for your attempt to reproduce my observation.
I've issued terraform refresh which gives me the reported issues, but also terraform init gives the same (negative) results.

Some background info: I'm using shell scripts terraform-common.sh to bring me from a folder holding environment specific configs to a common target folder for terraform launch, like so:

#!/usr/bin/env bash

COMMON_FOLDER="$(cd "$( dirname "${BASH_SOURCE[0]}" )/../../common/" && pwd )"
echo "Executing 'terraform $@ $COMMON_FOLDER':"
terraform "$@" "$COMMON_FOLDER"

Launching ./terraform-common.sh refresh gives the reported output:

Executing 'terraform refresh /home/christian/Projects/[...]/common':

Error: Unsupported Terraform Core version

  on ../../common/google-cloud-platform.tf line 3, in terraform:
   3: 	required_version = "~> 0.13.0"

This configuration does not support Terraform version 0.13.0. To proceed,
[...]

For execution of terraform init I'm using a different shell script terraform-init.sh, which invokes the aformentioned terraform-common.sh adding -backend-config switch:

#!/usr/bin/env bash

./terraform-common.sh init "$@" -backend-config ./terraform.gcs-backend.config

Launching ./terraform-init.sh gives same issue:

Executing 'terraform init -backend-config ./terraform.gcs-backend.config /home/christian/Projects/[...]/common':

Error: Unsupported Terraform Core version

  on ../../common/google-cloud-platform.tf line 3, in terraform:
   3: 	required_version = "~> 0.13.0"

This configuration does not support Terraform version 0.13.0. To proceed,
[...]

terraform.gcs-backend.config only contains Google Cloud Service configs.

Hint: My workspace misses something like main.tf from the minimal setup you've described (I've not missed it because it works with terraform 0.12.X).
By the way, I'm able to reproduce your minimal setup successfully.

@alisdair
Copy link
Contributor

Unfortunately I still can't reproduce the issue you're seeing. The shell scripts alone don't seem to explain what could be causing this, and without your full configuration I can't understand what is happening on your system.

Can you provide a small reproduction test case? One way to do that would be as a pull request to our issue reproductions repository, but if it's easier to respond here that would be great too!

@jufemaiz
Copy link

Pretty sure I'm hitting somethign similar.

Constraint:

terraform {
  required_version = ">= 0.12"
}

I'm using the Docker container: hashicorp/terraform:light

Output of an attempt to plan:

Error: Unsupported Terraform Core version
--
  |  
  | on versions.tf line 2, in terraform:
  | 2:   required_version = ">= 0.12"
  |  
  | This configuration does not support Terraform version 0.13.0. To proceed,
  | either choose another supported Terraform version or update this version
  | constraint. Version constraints are normally set for good reason, so updating
  | the constraint may lead to other errors or unexpected behavior.

@dcmoeller
Copy link
Author

So, seems I've fixed it myself without knowing which step exactly makes Terraform happy 😄
Here are the commands I've executed:

$ ./terraform-common.sh 0.13upgrade
Executing 'terraform 0.13upgrade /home/christian/Projects/[...]/common':

This command will update the configuration files in the given directory to use [...]

We recommend using this command in a clean version control work tree, so that [...]

Would you like to upgrade the module in ../../common?
  Only 'yes' will be accepted to confirm.

  Enter a value: yes

-----------------------------------------------------------------------------

Error: Unsupported Terraform Core version

  on ../../common/dns.tf line 3, in terraform:
   3: 	required_version = "~> 0.12.6"

This configuration does not support Terraform version 0.13.0. To proceed, [...]

So here it stumbles over the same issue, but on another location in my Terraform code.
After changing this line to "~> 0.13.0" this happens:

$ ./terraform-common.sh 0.13upgrade
Executing 'terraform 0.13upgrade /home/christian/Projects/[...]/common':

This command will update the configuration files in the given directory to use [...]

We recommend using this command in a clean version control work tree, so that [...]

Would you like to upgrade the module in ../../common?
  Only 'yes' will be accepted to confirm.

  Enter a value: yes

-----------------------------------------------------------------------------

Upgrade complete!

Use your version control system to review the proposed changes, make any
necessary adjustments, and then commit.

Fine! Now let's issue the refresh command from my original post (which has provoked the problem):

$ ./terraform-common.sh refresh
Executing 'terraform refresh /home/christian/Projects/[...]/common':

Error: Could not load plugin

Plugin reinitialization required. Please run "terraform init".

Plugins are external binaries that Terraform uses to access and manipulate [...]

7 problems:

- Failed to instantiate provider "registry.terraform.io/hashicorp/google" to
obtain schema: unknown provider "registry.terraform.io/hashicorp/google"
[...]

OK, so let's run init:

$ ./terraform-init.sh 
Executing 'terraform init -backend-config ./terraform.gcs-backend.config /home/christian/Projects/[...]/common':

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/google versions matching "~> 3.0"...
[...]
- Installing -/google v3.34.0...
- Installed -/google v3.34.0 (signed by HashiCorp)
[...]

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* -/google: version = "~> 3.34.0"
[...]

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see [...]

If you ever set or change modules or backend configuration for Terraform, [...]

Final step: Rerun refresh command like above:

$ ./terraform-common.sh refresh
Executing 'terraform refresh /home/christian/Projects/[...]/common':
[...]
data.google_service_account.terraform: Refreshing state... [id=...]
data.google_storage_bucket_object.defaultTerraformState: Refreshing state... [id=...]
[...]
Outputs:

[...]

Bingo! 👍
Hope you're able to get an idea what causes the issue in the first place.

@alisdair
Copy link
Contributor

@jufemaiz That looks similar, yes. I still can't see how it's possible and can't reproduce the issue. I believe the problem must be an error message pointing at the wrong position in the configuration, but that's just a guess.

Are you able to pare down your configuration to a minimal reproduction case? Doing so would allow us to fix this misleading error message.

@alisdair alisdair added the waiting-response An issue/pull request is waiting for a response from the community label Aug 17, 2020
@dcmoeller
Copy link
Author

OK, what do you mean by configuration? Can you give me some hints where to look for what? Or is there a terraform command to spit it out? Or a debug CLI switch?

@ghost ghost removed waiting-response An issue/pull request is waiting for a response from the community labels Aug 18, 2020
@jufemaiz
Copy link

@alisdair let me see what I can do. It's not an easy one – initial state needs to be set up, then use the new image and hope it fails.

terraform {
  required_version = "~> 0.12.20"

  backend "local" {
    path = "terraform.tfstate"
  }
}

provider "aws" {
  region  = "ap-southeast-2"
  version = "~> 2.48"
}

terraform {
  required_version = ">= 0.12"
}

resource "aws_sns_topic" "test_topic" {
  name = "test_topic"
}
---
version: '3.7'
services:
  terraform:
    image: hashicorp/terraform:light
    command: ['terraform', 'apply']
    entrypoint: ''
    environment:
      AWS_DEFAULT_REGION: ap-southeast-2
      AWS_ACCESS_KEY_ID:
      AWS_SECRET_ACCESS_KEY:
      AWS_SESSION_TOKEN:
      # SSH Support
      SSH_AUTH_SOCK: /ssh-agent
    tty: true
    volumes:
    - ./:/app/
    - ${HOME}/.terraformrc:/root/.terraformrc:ro
    - ${HOME}/.ssh/known_hosts:/root/.ssh/known_hosts:ro
    - ${SSH_AUTH_SOCK}:/ssh-agent
    working_dir: '/app'

The problem lies with:

  required_version = "~> 0.12.20"

is causing the issue but isn't being indicated in the error message (hence the reference to >=0.12.

@alisdair
Copy link
Contributor

@jufemaiz Thank you, this was very helpful! I have an even smaller reproduction now:

terraform {
  required_version = "~> 0.12.20"
}

terraform {
  required_version = ">= 0.12"
}

This causes terraform init to display the wrong source line for the version constraint:

$ terraform init

Error: Unsupported Terraform Core version

  on main.tf line 6, in terraform:
   6:   required_version = ">= 0.12"

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

@dcmoeller We now have a simple reproduction of the issue, and I'm glad you were able to get your configuration working too. Thanks for the bug report!

@alisdair alisdair changed the title Seems Terraform 0.13.0 not evaluates "terraform.required_version" correctly Terraform reports incompatible version pointing at the last "terraform.required_version" line, even if it is a previous line which is the unmatched constraint Aug 18, 2020
@alisdair alisdair added confirmed a Terraform Core team member has reproduced this issue and removed waiting for reproduction unable to reproduce issue without further information labels Aug 18, 2020
@alisdair alisdair added explained a Terraform Core team member has described the root cause of this issue in code and removed confirmed a Terraform Core team member has reproduced this issue labels Aug 18, 2020
@alisdair
Copy link
Contributor

There's an explanation of the source of the bug and a fix in #25898.

@dcmoeller
Copy link
Author

@jufemaiz Thanks for great support in distilling the problem down to a few lines.
@alisdair Thank you for keeping an eye even on such weird issues.

@ghost
Copy link

ghost commented Oct 11, 2020

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 as resolved and limited conversation to collaborators Oct 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug explained a Terraform Core team member has described the root cause of this issue in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants