Removing region from Resource Group name

Viewed 33

How would I go about removing the location code (aus) from the Resource Group creation.

from:

rg-d-lxr-aus-app1

to

rg-d-lxr-app1

I only want this to affect the only RG and not any other resources eg vnets/keyvaults etc

app1

locals.tf

locals {

  full_env_code  = format("%s-%s-%s", lower(var.environment_code), lower(var.deployment_code), lower(var.location_code))
  
}

resource_groups.tf

module "rg-app1" {
  source                 = "git::ssh://git@ssh.dev.azure.com/*"
  resource_group_name    = format("rg-%s-%s", local.full_env_code, lower(var.name_suffix))
  location               = var.location
}

variables.tf

 variable "location" {
  description = "Location in which to deploy resources"
  # default = "Australia Southeast"
}

variable "environment_code" {
  description = "Environment code"
  # default = "d"
}

variable "environment" {
  description = "Environment"
  # default = "d"
}

variable "deployment_code" {
  description = "Deployment code"
  # default = "d"
}

variable "location_code" {
  description = "Location code"
  # default = "d"
}

dev.tfvars

environment      = "non-prod"
environment_code = "d"
deployment_code  = "my org"
location_code    = "aus"
location         = "Australia Southeast"
name_suffix      = "app1"

Output of TF Plan

    Terraform will perform the following actions:
    
  # module.rg-sharegate.azurerm_resource_group.rg will be created
  + resource "azurerm_resource_group" "rg" {
      + id       = (known after apply)
      + location = "australiasoutheast"
      + name     = "rg-d-lxr-aus-app1"
    }

    Plan: 1 to add, 0 to change, 0 to destroy.

I am thinking we need to modify the RG module and use the split command, but i'm not sure how to go about it

module "rg-app1" {
  
  resource_group_name = format("rg-%s-%s", local.full_env_code, lower(var.name_suffix))
1 Answers

You can use a mixture of built-in Terraform functions to achieve what you're after.

Local Code to recreate a MRE for you:

locals {
  rg_string = "rg-d-lxr-aus-app1"
  // Use split by "-" to create an array so we can use contains()
  is_rg           = contains(split("-", local.rg_string), "rg")
  final_rg_string = local.is_rg ? replace(local.rg_string, "aus-", "") : local.rg_string

  not_rg_string       = "blah-d-lxr-aus-app1"
  is_not_rg           = contains(split("-", local.not_rg_string), "rg")
  final_not_rg_string = local.is_not_rg ? replace(local.not_rg_string, "aus-", "") : local.not_rg_string
}

Result using Terraform Console:

> local.is_rg
true
> local.final_rg_string
"rg-d-lxr-app1"
> local.is_not_rg
false
> local.final_not_rg_string
"blah-d-lxr-aus-app1"

Links:

https://www.terraform.io/language/functions/replace

https://www.terraform.io/language/functions/contains

https://www.terraform.io/language/functions/split

You'll likely not need to duplicate the logic but I did just for demonstrative purposes. For example you can just use your resource_group_name and create your true/false conditions. If you need more help with that please let me know.

Related