Using Resource Type dynamically as part of IF statement, is it possible?

Is it possible to create some kind of “IF” condition based on the resource type to use as a variable, e.g : -

IF resource_type == azurerm_resource_group then use “rg-” as a prefix for the resource group name

Hi @davidjyeo,

The type of a resource is always defined statically, so there should never be any uncertainty as to what type a particular resource has, and thus typically no reason to make dynamic decisions based on the resource type.

However, if you can say a little more about your underlying goal I might be able to suggest a different strategy.

Hi @apparentlymart ,

Not sure how to work it but hope that the below makes some sort of sense for what I am hoping can be accomplished, looking for some kind of “map” based on resource type to create a variable. This would allow all our code to fit company standards, also this could be used across other providers (AWS,GCP…): -

resource “azurerm_resource_group” “example” {
name = “example-(IF resource IS ‘azurerm_resource_group’)”
----- result would be “example-rg”

location = “UK South”

resource “azurerm_virtual_network” “example” {
name = “example-(IF resource IS ‘azurerm_virtual_network’)”
----- result would be “example-vnet”
address_space = [“”]
location = azurerm_resource_group.example.location
resource_group_name =


Hi @davidjyeo,

Since in both of those cases you already say exactly which resource type the resource is, the typical answer would be to just write the appropriate suffix statically into the configuration, rather than writing a more complicated expression to generate it.

It sounds like part of your motivation here is to help ensure that the naming meets your local conventions, in which case a common answer to that is to use an extra policy check in your workflow, where you can make sure the result conforms to standard, regardless of how that might be achieved in the configuration:

  1. Run terraform plan -out=tfplan to generate and save a plan.
  2. Run terraform show -json tfplan to get a JSON-based description of the planned changes.
  3. Run some software written by your team to analyze that JSON structure and return an error if it doesn’t meet the standards.
  4. If the plan passed the policy check, run terraform apply tfplan to apply it.

If you are running Terraform in an automated pipeline (recommended for production) then this would be an extra step in that pipeline, to detect any configuration changes that don’t meet the standard.