Terraform: Terraform reports incompatible version pointing at the last "terraform.required_version" line, even if it is a previous line which is the unmatched constraint

Created on 13 Aug 2020  ·  12Comments  ·  Source: hashicorp/terraform

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"
bug explained

All 12 comments

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.
    

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.

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!

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.

So, seems I've fixed it myself without knowing which step exactly makes Terraform happy :smile:
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! :+1:
Hope you're able to get an idea what causes the issue in the first place.

@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.

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?

@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.

@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!

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

@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.

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