Introduction
Using multiple versions of a Terraform provider in one configuration allows leveraging different features or maintaining compatibility with existing infrastructure managed by different versions of the same provider.
Expected Outcome
The configuration will support and use different versions of a Terraform provider within the same Terraform setup, enabling the management of resources with the appropriate provider version.
Use Case
This approach is useful when different modules or parts of the infrastructure require different versions of the same provider due to feature availability or compatibility reasons.
Pre-requisites
- Terraform installed on the machine.
- Basic understanding of Terraform configuration files.
- Access to the provider's source (e.g., HashiCorp registry).
Procedure
At times it may be useful to have multiple versions of the same provider in one configuration. For example, if there is a regression in a new provider version in one resource, but that new version has a new resource that is necessary in that same configuration.
Procedure
- Define Required Providers: Add the
required_providers
block to specify the different versions of the provider needed.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.0.0"
}
aws_v2 = {
source = "hashicorp/aws"
version = ">= 2.0.0, < 3.0.0"
}
}
}
- Configure Provider Aliases: Define the provider configurations with aliases to distinguish between different versions.
provider "aws" {
alias = "aws_v3"
region = "us-west-2"
}
provider "aws" {
alias = "aws_v2"
region = "us-east-1"
}
- Use Providers in Modules: Specify the provider alias in the providers block of the modules to use the appropriate provider version.
module "module_using_aws_v3" {
source = "./module_using_aws_v3"
providers = {
aws = aws.aws_v3
}
}
module "module_using_aws_v2" {
source = "./module_using_aws_v2"
providers = {
aws = aws.aws_v2
}
}
Notes
- Official documentation on using
required_providers
in Terraform -
Ensure that the modules being used are compatible with the specified provider versions.
-
Use the
terraform init
command to initialize the configuration and download the specified provider versions.