I am using terraform azure provider to deploy our infrastructure on Azure
I am using the azurerm provider
terraform {
required_version = ">= 1.3.0"
required_providers {
azurerm = {
version = "4.12.0"
source = "hashicorp/azurerm"
}
}
backend "azurerm" {}
}
I’d like now to use the azurerm version (4.12.0) and add it to a local variable
locals {
tags = merge(data.azurerm_resource_group.resource_group.tags, {
azRmVersion : "4.12.0"
})
}
I don’t want to duplicate the version in my terraform code but instead I would like to have a variable or to fetch the provider version in order to use it as value of my azRmVersion
tag.
I understand that we cannot use variables in the terraform.required_providers.azurerm.version
, is there another way that I can achieve my goal?
Why I want to do this?
While ago, I upgraded the provider to 3.111.0 and there were no issues (just because not all the resources had modifications).
After 6 months I’ve changed something in a very old resource and it was broken and I couldn’t understand why. I spent hours investigating.
So just upgrading the provider, the issue was automatically solved. It was just a bug in the specific provider version that didn’t show up during the provider upgrade since the specific resource had no modification.
So my idea is to add the provider version as a tag so whenever I want to test the new provider in dev, all my resources will have a modification.
In this way I am sure that all the resources will be tested against the new provider version.
Usually tags are harmless.
Maybe there is a better way, I am open to suggestions
Thank you
4
You cannot directly use variables or local values in the required_providers
block to define the provider version. However, you can achieve your goal of avoiding duplication by using a combination of local variables. While you cannot dynamically fetch the provider version directly, you can define it in a local variable and use that variable in your tags.
Here is the updated code to fetch the tag from the version and add the same tag to a new resource group.
terraform {
required_version = ">= 1.3.0"
required_providers {
azurerm = {
version = "4.12.0" # Specify the provider version
source = "hashicorp/azurerm"
}
}
}
provider "azurerm" {
features {} # Add the features block here
subscription_id = "09a9"
}
locals {
azurerm_version = "4.12.0" # Define the provider version here
tags = merge({
azRmVersion = local.azurerm_version
})
}
# Create a new resource group using the tags
resource "azurerm_resource_group" "example" {
name = "venkat-resources"
location = "West Europe"
tags = local.tags
}
terraform apply
After running the code, the new resource group has been created with a tag named: azRmVersion: 4.12.0.
3