Azure 存储(Blob、队列、表)使用 for_each 和 locals 登录 Terraform

标签 azure terraform azure-storage terraform-provider-azure

我正在编写 Terraform 代码以启用 Azure 存储 Blob、队列和表类型的日志记录。使用我当前的代码,我需要获取每种存储类型的数据(例如 Blob),并使用它来获取其日志和指标详细信息。

有什么方法可以使用 for_each 和 locals 来避免为每种存储类型重复相同的代码块。下面是 Blob 类型的代码现在的样子,

data "azurerm_monitor_diagnostic_categories" "storage_blob" {
  resource_id = "${azurerm_storage_account.stamp.id}/blobServices/default/"
}

resource "azurerm_monitor_diagnostic_setting" "storage_blob" {
  name                       = "storageblobladiagnostics"
  target_resource_id         = "${azurerm_storage_account.stamp.id}/blobServices/default/"
  log_analytics_workspace_id = azurerm_log_analytics_workspace.stamp.id

  dynamic "log" {
    iterator = entry
    for_each = data.azurerm_monitor_diagnostic_categories.storage_blob.logs

    content {
      category = entry.value
      enabled  = true

      retention_policy {
        enabled = true
        days    = 30
      }
    }
  }

  dynamic "metric" {
    iterator = entry
    for_each = data.azurerm_monitor_diagnostic_categories.storage_blob.metrics

    content {
      category = entry.value
      enabled  = true

      retention_policy {
        enabled = true
        days    = 30
      }
    }
  }
}

下面的实现似乎不起作用,因为数据 block 无法处理动态 block 中的 for_each 表达式

locals {
storage = ["blobServices", "tableServices", "queueServices"]
}

data "azurerm_monitor_diagnostic_categories" "storage_blob" {
  resource_id = "${azurerm_storage_account.stamp.id}/${each.key}/default/"
}



resource "azurerm_monitor_diagnostic_setting" "storage_blob" {
  for_each                   = toset(local.storage)
  name                       = "storageblobladiagnostics"
  target_resource_id         = "${azurerm_storage_account.stamp.id}/${each.key}/default/"
  log_analytics_workspace_id = azurerm_log_analytics_workspace.stamp.id

  dynamic "log" {
    iterator = entry
    for_each = data.azurerm_monitor_diagnostic_categories.storage_blob.logs

    content {
      category = entry.value
      enabled  = true

      retention_policy {
        enabled = true
        days    = 30
      }
    }
  }

  dynamic "metric" {
    iterator = entry
    for_each = data.azurerm_monitor_diagnostic_categories.storage_blob.metrics

    content {
      category = entry.value
      enabled  = true

      retention_policy {
        enabled = true
        days    = 30
      }
    }
  }
}

最佳答案

为了使其正常工作,您必须稍微调整代码。在您的示例中,数据源未使用 for_each,因此无法按照您想要的方式使用它。调整如下:

locals {
storage = ["blobServices", "tableServices", "queueServices"]
}

data "azurerm_monitor_diagnostic_categories" "storage_blob" {
  for_each    = toset(local.storage)
  resource_id = "${azurerm_storage_account.stamp.id}/${each.key}/default/"
}

resource "azurerm_monitor_diagnostic_setting" "storage_blob" {
  for_each                   = toset(local.storage)
  name                       = "storageblobladiagnostics"
  target_resource_id         = "${azurerm_storage_account.stamp.id}/${each.key}/default/"
  log_analytics_workspace_id = azurerm_log_analytics_workspace.stamp.id

  dynamic "log" {
    iterator = entry
    for_each = "${data.azurerm_monitor_diagnostic_categories.storage_blob[each.key].logs}"
    content {
      category = entry.value
      enabled  = true

      retention_policy {
        enabled = true
        days    = 30
      }
    }
  }

  dynamic "metric" {
    iterator = entry
    for_each = "${data.azurerm_monitor_diagnostic_categories.storage_blob[each.key].metrics}"

    content {
      category = entry.value
      enabled  = true

      retention_policy {
        enabled = true
        days    = 30
      }
    }
  }
}

关于Azure 存储(Blob、队列、表)使用 for_each 和 locals 登录 Terraform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75117586/

相关文章:

amazon-web-services - terraform中aws_iam_user_login_profile的PGP key

amazon-web-services - Terraform 模板 jsonencoding 与 iterate

amazon-web-services - Terraform:如果目标是 aws_sfn_state_machine,则 aws_api_gateway_integration 的 uri 参数是什么

php - 在 Blob 存储 Azure 中创建容器 - PHP

azure - 无法为 Event-Grid 创建 Webhook 订阅

azure - 如何设置具有 Azure 计算 API 权限的应用程序

azure - 找不到具有指定模式 : d:\a\r1\a\**\*. zip 的包<br/>检查任务中提到的包是否作为工件发布

azure - 如何在本地计算机上模拟Azure文件存储?

azure - 更改文件名 从 Azure 下载

azure - 需要在 azure 逻辑应用程序中使用表达式才能获取 JSON 元素