How to add the RG prefix to the start of `local.full_env_code` within resource_groups.tf

Viewed 25

How would I go about adding the RG prefix to the start of local.full_env_code within resource_groups.tf so my TF Plan output would change from:

d-myorg-aus-app1

to

rg-d-myorg-aus-app1

terraform-azure-module-resourcegroup

main.tf

resource "azurerm_resource_group" "rg" {
  name     = format("%s-%s", var.full_env_code, lower(var.name_suffix))
  location = var.location
}

outputs.tf

output "resource_group_name_id" {
  value       = azurerm_resource_group.rg.id
  description = "id of resource group"
}

output "resource_group_name" {
  value       = azurerm_resource_group.rg.name
  description = "name of resource group"
}

output "location" {
  value       = azurerm_resource_group.rg.location
  description = "location of resource group"
}

variables.tf

variable "location" {
  type        = string
  description = "location of resource group"
}

variable "name_suffix" {
  description = "Name to append to the full_env_code. "
}

variable "full_env_code" {
  description = "Environment, Deployment, Location code"
}

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/*"
  name_suffix            = "app1"
  location               = var.location
  full_env_code          = local.full_env_code
}

variables.tf

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

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

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

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

dev.tfvars

environment_code = "d"
deployment_code  = "myorg"
location_code    = "aus"
location         = "Australia Southeast"

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     = "d-myorg-aus-app1"
    }

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

That should be fairly easy to do, just add another %s and substitute it with "rg", i.e.,:

resource "azurerm_resource_group" "rg" {
  name     = format("%s-%s-%s", "rg", var.full_env_code, lower(var.name_suffix))
  location = var.location
}

Or if it has to be done in the local variable:

locals {

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