【问题标题】:How to "override" some of the resource parameters when using terraform modules?使用 terraform 模块时如何“覆盖”某些资源参数?
【发布时间】:2018-10-22 17:37:18
【问题描述】:

我正在尝试开发通用 terraform 模块以支持数据狗监视器,并让模块的用户在侧面通用模块中附加资源和/或覆盖资源。

terraform overrides 功能在没有模块的情况下可以正常工作,但在使用模块时无法正常工作。

如何覆盖模块内的一些资源参数?

要求:

  1. /modules/datadog/monitors.tf 包含资源列表,每个资源代表一个具有默认参数值的通用数据狗监视器。每个单独的应用程序可以选择覆盖每个资源中的一个或多个参数

  2. /application-1/monitors.tf 包含源为 /modules/datadog/ 的模块,还有一些通用监视器未涵盖的监视器和一些变量。

/application-1/monitors.tf

module "datadog" {
  source  = "/modules/datadog/"
}

/modules/datadog/monitors.tf

# Generic Datadog monitor to monitor cpu 
resource "datadog_monitor" "foo" {
  name               = "Name for monitor foo"
  type               = "metric alert"
  message            = "Monitor triggered. Notify: @hipchat-channel"
  escalation_message = "Escalation message @pagerduty"

  query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 4"

  thresholds {
    ok                = 0
    warning           = 2
    warning_recovery  = 1
    critical          = 4
    critical_recovery = 3
  }

  notify_no_data    = false
  renotify_interval = 60

  notify_audit = false
  timeout_h    = 60
  include_tags = true

  silenced {
    "*" = 0
  }

  tags = ["foo:bar", "baz"]
}

解决方案 1:将 overrides.tf 添加到 /modules/datadog 目录。 terraform override feature 将 overrides.tf 中的内容合并到 在 monitor.tf 中定义的配置。
但是这个解决方案的问题是每个应用程序特定的 overrides.tf 需要在运行 apply 命令之前复制到 /modules/datadog 目录。

覆盖.tf

resource "datadog_monitor" "foo" {
      escalation_message = "Escalation message @pagerduty1"

      thresholds {
        ok                = 0
        warning           = 20
        warning_recovery  = 10
        critical          = 40
        critical_recovery = 35
      }

      notify_no_data    = false

    }

解决方案 2:我可以对模块使用覆盖吗?我试图通过将 overrides.tf 复制到 /application-1/ 目录来覆盖资源参数,但 terraform 并没有覆盖资源,而是将两者视为不同的资源。

【问题讨论】:

  • 顺便说一句,我熟悉模块变量,但我正在尝试找到如何覆盖资源
  • 虽然您的问题格式适用于 StackOverflow(您实际上提出了一个我认为在 [我观看 Terraform] 之前我没有想到的问题),但我不得不说这实际上是谷歌的第一个结果:terraform.io/docs/configuration/override.html——你读过吗?
  • @ChaimEliyah 感谢您的回复!我探索了 terraform 覆盖 terraform.io/docs/configuration/override.html,它适用于 out 模块。但我正在尝试同时利用 terraform 覆盖和模块。具体来说,我想覆盖一个或多个在模块中定义的资源参数。
  • 我有理由确定导入模块中的资源是不可变的。您必须定义一个新资源,然后根据@Tim Theratt 的答案中指定的条件使用“计数”标志在两者之间切换。

标签: terraform


【解决方案1】:

如果要创建资源,您可以使用带有计数的条件来覆盖。下面的示例将仅在变量 environment 不 = 生产时创建资源。如果 Count = 0 则不会创建资源,

问候,

resource "azurerm_network_security_rule" "web_server_nsg_rule_rdp" {
  name                        = "RDP Inbound"
  priority                    = 100
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "3389"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = "${azurerm_resource_group.web_server_rg.name}"
  network_security_group_name = "${azurerm_network_security_group.web_server_nsg.name}"
  count                       = "${var.environment == "production" ? 0 : 1}"  
}

【讨论】:

  • 感谢 Tim 提供有条件地创建资源的解决方案。我正在尝试查找在使用 terraform 模块时是否可以覆盖某些资源参数。示例:在问题中添加示例
猜你喜欢
  • 1970-01-01
  • 2019-08-26
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
  • 2022-09-22
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多