【发布时间】:2021-12-31 07:35:15
【问题描述】:
我想在 terraform 中创建 google_compute_health_check,我正在考虑如何使它们成为最通用的。我的代码 atm 是这样的
application.hcl
inputs = {
health_checks = {
tcp-health-check = {
name = "tcp-health-check"
desc = "Health check via tcp"
port = 80
timeout_sec = 4
check_interval_sec = 30
}
}
主terragrunt.hcl
include {
path = find_in_parent_folders()
}
terraform { source ...}
locals {
app_vars = read_terragrunt_config(find_in_parent_folders("application.hcl"))
}
inputs = {
# and my idea was that every invocation of the module, picks it's own set
# of health checks that it wants to use
health_checks = [local.app_vars.inputs.health_checks.tcp-health-check]
}
现在模块 main.tf 看起来像这样
locals {
checks = { for check in var.health_checks: check.name => check }
}
resource "google_compute_health_check" "main" {
for_each = local.checks
name = each.value.name
timeout_sec = each.value.timeout_sec
check_interval_sec = each.value.check_interval_sec
dynamic tcp_health_check {
#for_each = each.value.name == "tcp_health_check" ? each.value : []
#for_each = lookup(each.value, "tcp_health_check", [])
for_each = contains(keys(each.value), "tcp_health_check") != null ? each.value : {}
content {
port = 80
# port = each.value.port
# port_name = each.value.name
}
}
我被困在动态块中 - 如何使它工作,以便它仅在我通过 tcphealth_check 时应用,当我通过时,ssh 它创建动态 ssh 块(我知道代码 atm 中没有 ssh 块,但将来我会通过我需要的任何健康检查来扩展模块)
我得到的错误如下contains
Error: List longer than MaxItems
on main.tf line 30, in resource "google_compute_health_check" "main":
30: resource "google_compute_health_check" "main" {
Attribute supports 1 item maximum, config has 7 declared
ERRO[0011] 1 error occurred:
* exit status 1
lookup
Error: ExactlyOne
on main.tf line 30, in resource "google_compute_health_check" "main":
30: resource "google_compute_health_check" "main" {
"ssl_health_check": one of
`grpc_health_check,http2_health_check,http_health_check,https_health_check,ssl_health_check,tcp_health_check`
must be specified
ERRO[0005] 1 error occurred:
* exit status 1
与==比较
Error: Inconsistent conditional result types
on main.tf line 44, in resource "google_compute_health_check" "main":
44: for_each = each.value.name == "tcp_health_check" ? each.value : []
|----------------
| each.value is object with 7 attributes
| each.value.name is "tcp-health-check"
The true and false result expressions must have consistent types. The given
expressions are object and tuple, respectively.
ERRO[0005] 1 error occurred:
* exit status 1
好的,以另一种方式解决了它,但感谢 Marcin 的回答
terragrunt.hcl
inputs = {
name = "nat-health-check"
used_for = "used for NATs"
check_interval_sec = 30
timeout_sec = 5
healthy_threshold = 1
unhealthy_threshold = 5
http_checks = local.app_vars.inputs.health_checks.nat-http
}
和模块main.tf
resource "google_compute_health_check" "main" {
name = var.name
timeout_sec = var.timeout_sec
check_interval_sec = var.check_interval_sec
description = "${var.name} - ${var.used_for}"
dynamic "http_health_check" {
for_each = var.http_checks != null ? [1] : []
content {
port = var.http_checks.port
request_path = var.http_checks.request_path
port_specification = var.http_checks.port_specification
}
}
}
【问题讨论】:
-
您当前的代码有什么问题?有什么错误吗?
-
我已经把它们放在帖子里了
标签: google-cloud-platform terraform hcl terragrunt