'How to pin terraform provider?

I'm working on terraform rds cluster for building aurora , Can someone help me on how to pin the aws provider version to 2.0 ?

Is this a correct way to do it?

provider "aws" {
  region = "us-east-1"

  version = "<= 2.0"
}

Also my module earlier used 2.46 version, should i follow any steps while trying to do the downgrade?

Using terraform 0.12.6 version



Solution 1:[1]

You need to use just the equals operator = and not <=, which means "less than or equal to version 2.0".

If you want it to use version 2.0 only:

provider "aws" {
  region = "us-east-1"

  version = "= 2.0"
}

See also https://www.terraform.io/docs/configuration/terraform.html#specifying-required-provider-versions

Solution 2:[2]

For anyone finding this on a search engine, the answer by Nathan was correct in 2020, but setting a version constraint in a provider block is now deprecated.

The new recommended method is to set required_providers in a global configuration terraform block.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "= 2.0"
    }
  }
}

If instead you want to pin to a major version and allow the minor version to increment, use the ~> operator, which allows only the rightmost version specified to increment. E.g. ~> 3.0 allows upgrade for any version in the 3.x space. More information can be found on the Version Constraints documentation.

See official documentation on provider requirements for more information.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nathan Griffiths
Solution 2 Clete2