0
votes

I am optimizing my terraform code by using modules. When i create a resource group module it works perfectly well but it creates two resource groups i.e.

  1. Temp-AppConfiguration-ResGrp
  2. Temp-AppServices-ResGrp

instead it should only create

Temp-AppConfiguration-ResGrp

Code Resourcegroup.tf.

resource "azurerm_resource_group" "resource" {
  name     = "${var.environment}-${var.name_apptype}-ResGrp"
  location = var.location
  tags = {
    environment = var.environment
  }
}
output "resource_group_name" {
  value = "${var.environment}-${var.name_apptype}-ResGrp"
}

output "resource_group_location" {
  value = var.location
}

Variable.tf

variable "name_apptype" {
  type    = string
  default = "AppServices"
}
variable "environment" {
  type    = string
  default = "Temp"
}
variable "location" {
  type    = string
  default = "eastus"
}

Main.tf

 module "resourcegroup" {
  source = "../Modules"
  name_apptype = "AppConfiguration"
}

I want to pass name_apptype in main.tf when calling resource group module. So that i don't need to update variable.tf every time.

Any suggestions where i am doing wrong. Plus i am also unable to output the value, i need it so that i could pass resource group name in the next module i want to create.

Thanks

1
code is fine, how are you executing terraform? and where is main.tf? What did you plan show?James Woolfenden

1 Answers

0
votes

You need to do that in the Main.tf

module "resourcegroup" {
  source = "../Modules"
  name_apptype = "AppConfiguration"
}

module "resourcegroup-appservices" {
  source = "../Modules"
  name_apptype = "AppServices"
}

These create a 2 resources groups with the values that you need, additionally you can remove the default value from the name_apptype variable.

If you want to create with the same module both resource groups you need to use count to iterate over an array of names