mgross
mgross

Reputation: 652

Terraform: Manage provider versions in one file

How does one integrate best version constraints from only one file into different Terraform modules in a project directory? I.e. we have 3 different files in our Terraform project, that should use the following version constraint header:

terraform {
  required_version = "~> 0.13.0"
  required_providers {
    azurerm =  "~> 2.19.0"
  }
}

provider "azurerm" {
  features {}
}

Now I thought I could just generate a versions.tf at the top-level and reference that file via the file command, e.g. like so

file("../versions.tf")

This does not work, but I am not yet very experienced in Terraform. Is there a better way than just copying the version code snippet into all 3 files.

Upvotes: 4

Views: 1442

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74209

From Terraform's perspective, each module has its own set of provider dependencies. The idea is that each module describes the provider versions that it specifically is compatible with, without any regard to what other modules might need. Terraform will then combine all of those version constraints together and select the newest version that is compatible with all of them.

In general you should need to change the version constraint of a provider in a module only when that module starts using a feature that isn't available in earlier versions. If other modules still use only features that were available in their earlier versions then there's no need to change their version constraints.

The forthcoming Terraform v0.14 release (about to go into beta at the time I'm writing this) will include a new mechanism to generate a version "lock" file you can include in your version control to have Terraform remember which version of each provider is used by each of your configurations. Under Terraform v0.14 it will therefore no longer be necessary to proactively use ~> constraints on your root modules, because Terraform will adopt newer versions only when explicitly asked using terraform init -upgrade.

Upvotes: 5

Related Questions