0
0
TerraformHow-ToBeginner · 4 min read

How to Set Provider Version in Terraform Correctly

To set a provider version in Terraform, use the required_providers block inside the terraform block and specify the provider name with the version attribute. This locks the provider to a specific version or version range, ensuring consistent behavior during deployments.
📐

Syntax

The provider version is set inside the terraform block using required_providers. You specify the provider name and the version constraint.

  • terraform: The main block for Terraform settings.
  • required_providers: Defines which providers and versions Terraform should use.
  • version: The version or version range to lock the provider.
terraform
terraform {
  required_providers {
    <provider_name> = {
      source  = "<namespace>/<provider>"
      version = "<version_constraint>"
    }
  }
}
💻

Example

This example sets the AWS provider to version 4.0.0 exactly. Terraform will use this version when managing AWS resources.

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

provider "aws" {
  region = "us-west-2"
}
Output
Terraform will download and use AWS provider version 4.0.0 for all AWS resource management.
⚠️

Common Pitfalls

Common mistakes include:

  • Not specifying the source attribute, which is required for Terraform 0.13 and later.
  • Using deprecated syntax like provider "aws" { version = "x.y.z" } which no longer works.
  • Setting overly broad version constraints that allow unexpected upgrades.

Always use the required_providers block inside terraform and specify exact or carefully ranged versions.

terraform
/* Wrong (legacy) way - deprecated and ignored in recent Terraform versions */
provider "aws" {
  version = "~> 3.0"
  region  = "us-west-2"
}

/* Correct way */
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

provider "aws" {
  region = "us-west-2"
}
📊

Quick Reference

TermDescription
terraformMain block for Terraform settings
required_providersDefines providers and their version constraints
sourceProvider source address (namespace/provider)
versionVersion or version range of the provider

Key Takeaways

Always set provider versions inside the terraform block using required_providers.
Specify both source and version to avoid ambiguity and errors.
Avoid deprecated version settings inside provider blocks.
Use exact or carefully ranged versions to control upgrades.
Terraform 0.13+ requires this new syntax for provider version management.