Search code examples
azureterraformazure-rm

Terraform count length of variables


we have a standard naming convention within azure, but in order to sometimes be able to make an exception, it must be possible to provide a name yourself when calling the module

How can be indicated within count which variable Var.Log Name or Local.ComponetName should be used and how can we pass this to the name of the resource

resource "azurerm_log_analytics_workspace" "LOG" {
  count               = length(var.LOG_Name) == "" ? length(local.ComponentNames) : null
  name                = var.LOG_Name[count.index] == "" ? local.ComponentNames[count.index] : null
  resource_group_name = element(var.resourcegroup_name[*], count.index)
  location            = var.location
  sku                 = var.LOG_Sku
  retention_in_days   = var.LOG_RetentionPeriod
}

Solution

  • What you are actually looking for are loops. Within loops you can reference the name of the resource and in case there is no such resource available it won't create them, which seems to be what you tried to indicate when mentioning null.

    Here is a great link regarding loops in terraform that thoroughly explains the different types of loops and how to use them: https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9