【发布时间】:2018-10-22 17:37:18
【问题描述】:
我正在尝试开发通用 terraform 模块以支持数据狗监视器,并让模块的用户在侧面通用模块中附加资源和/或覆盖资源。
terraform overrides 功能在没有模块的情况下可以正常工作,但在使用模块时无法正常工作。
如何覆盖模块内的一些资源参数?
要求:
/modules/datadog/monitors.tf 包含资源列表,每个资源代表一个具有默认参数值的通用数据狗监视器。每个单独的应用程序可以选择覆盖每个资源中的一个或多个参数。
/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