azure - 有没有办法使用 Terraform 中的一个 `random_uuid` 资源生成一组随机 UUID

标签 azure random terraform uuid azure-sentinel

我正在尝试使用 Terraform 为哨兵解决方案部署一些哨兵自动化规则。由于自动化规则的名称需要 UUID,因此我为每个自动化规则使用 random_uuid 资源来生成自动化规则名称的 UUID。代码如下所示。我想知道是否有一种方法可以通过单个 random_uuid 资源或类似的资源为我的自动化规则生成所有随机 UUID 值。

resource "random_uuid" "automation_rule_1" {}

resource "azurerm_sentinel_automation_rule" "automation_rule_1" {
  name                       = random_uuid.automation_rule_1.result
  log_analytics_workspace_id = var.log_analytics_workspace_id
  display_name               = "Automation Rule 1"
  order                      = 1
  triggers_on                = "Incidents"
  triggers_when              = "Created"
  enabled                    = true
  action_playbook {
    logic_app_id = var.logic_app_id
    order        = 1
    tenant_id    = var.tenant_id
  }
}

resource "random_uuid" "automation_rule_2" {}


resource "azurerm_sentinel_automation_rule" "automation_rule_2" {
  name                       = random_uuid.automation_rule_2.result
  log_analytics_workspace_id = var.log_analytics_workspace_id
  display_name               = "Automation Rule 2"
  order                      = 1
  triggers_on                = "Incidents"
  triggers_when              = "Created"
  enabled                    = true
  condition_json = jsonencode(
    [
      {
        conditionProperties = {
          operator     = "Contains"
          propertyName = "IncidentRelatedAnalyticRuleIds"
          propertyValues = [ var.sentinel_alert_rule_scheduled_id ]
        }
        conditionType = "Property"
      }
    ]
  )
  action_incident {
    order                  = 1
    status                 = "Closed"
    classification         = "BenignPositive_SuspiciousButExpected"
    classification_comment = "Sample Comment Goes Here"
    owner_id               = var.automation_rule_owner_id
  }
}

我期望类似以下的内容。有人可以解释一下是否有办法实现这种行为。

resource "random_uuid" "automation_rule_ids" {
  keepers = {
    "rule_1_id" = "dont know what to put here",
    "rule_2_id" = "dont know what to put here"
  }
}

最佳答案

random_uuid 的每个实例仅生成一个 UUID,但您可以使用该资源类型的多个实例来生成多个 UUID。

例如:

variable "rules" {
  type = set(string)
}

resource "random_uuid" "example" {
  for_each = var.rules
}

locals {
  rule_uuids = tomap({
    for rule_name, obj in random_uuid_example :
    rule_name => obj.result
  })
}

这将 local.rule_uuids 定义为从规则名称到 UUID 字符串的映射,使用 for_eachrandom_uuid.example 资源来声明多个该资源的动态实例。

例如,如果将 rules 变量设置为 ["a", "b"],则 local.rule_uuids 将具有值(value)结构如下:

tomap({
  a = "aabbccdd-eeff-0011-2233-445566778899"
  b = "ffffffff-ffff-ffff-ffff-ffffffffffff"
})

...当然,实际的 UUID 值会有所不同,因为它们是随机的。

<小时/>

上面不包括任何“守护者”,因为您的问题没有定义何时需要重新生成 UUID 的任何规则。使用 for_each 意味着向 var.rules 添加一个新元素将导致 Terraform 建议为该规则键生成一个新的 UUID,同样从var.rules 将使 Terraform 建议丢弃其中一个 UUID。

如果您确实需要某种“守护者”,那么另一种方法是将输入变量设为映射,并使用这些值作为每个对象的“守护者”:

variable "rules" {
  type = map(string)
}

resource "random_uuid" "example" {
  for_each = var.rules

  keepers = {
    v = each.value
  }
}

# (locals block unchanged)
resource block 中的

each.value 引用 map 当前元素的值,因此该模块的调用者可能会导致重新生成现有的 UUID通过更改分配给 map 中其元素的值而不更改 map 键。

关于azure - 有没有办法使用 Terraform 中的一个 `random_uuid` 资源生成一组随机 UUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76093736/

相关文章:

javascript - 在两个日期内生成随机日期数组的优雅方法

c++ - 生成n个随机变量,并将它们相乘C++

kubernetes - 如何使用 terraform 限制 kubernetes 集群上的磁盘使用

azure - 当前订阅中不存在该存储帐户

azure - 下载的许可证不足 Azure/Visual Studio Online 和 Visual Studio 2013

azure - 使用 Azure 资源管理器模板创建容器

azure - 远程调试Azure网站的Visual Studio 2017项目

c - 如何在不使用时间函数、静态变量或全局变量的情况下编写随机生成器函数?

intellij-idea - HashiCorp Terraform/HCL 语言支持不起作用

删除数据库后,Terraform 尝试读取 SQL 用户