Terraform — Provider Versioning
Provider plugins are released separately
while we running terraform -init, if the version number is not specified the latest version of the provider will be installed.
For production, it's always recommended to use a specific version number.
Example:
terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 3.0”
}
}
}
In the above example ~>3.0, represents the latest provider which is available under 3.x. below are different types of arguments you can specify
version =”= 2.10" this will install the provider which is 2.10
version =”< = 2.0" this will install the provider which is less than 2.0
version =”> = 2.0, < = 2.3" this will install the provider which is between 2.0 and 2.3
if the specific version is specified, terraform will not install the latest version of the provider.
Once you create a terraform -init
Let's say we run the terraform init with version = “~>2.0”.It will create a file called terraform. lock.hcl , with version and constraints in it. Since ~>2.0 was mentioned, it will get the max version of the provider which is 2.70
what if, if you want to upgrade the provider by changing the version number in the main.tf file
and try to run terraform init
you will get below error :
Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/aws: locked provider registry.terraform.io/hashicorp/aws 2.70.0 does not match configured version constraint ~> 3.0; must use
│ terraform init -upgrade to allow selection of new versions
to overcome this, either delete the terraform. lock.hcl file or use terraform init -upgarde