Terraform Azure

Hello,
How can I detect an environment and location from the given virtual machine name?
for Example: vm-dns-pr-we-001
prod = production
us = Western Europe
This information will be used to attach the VM to the correct VNet/subnet and placed in the correct RG.
Thanks

Hi @bil.bens ,

actually I’m wondering how the VM name is built, meaning aren’t environment and location defined before the VM name is constructed?

Other than that the name can be split into pieces based on a naming convention in place.
After that do a lookup within maps.

locals {
vmname = "vm-dns-pr-we-001"
location = {
  "we" = "West Europe"
}

environment = {
  "pr" = "production"
}

longlocation = lookup(local.location,split("-",local.vmname)[3], "nolocation")
longenvironment = lookup(local.environment,split("-",local.vmname)[2], "test")

}