gpt4 book ai didi

Terraform 13,根据另一个值验证变量

转载 作者:行者123 更新时间:2023-12-04 12:33:12 28 4
gpt4 key购买 nike

有没有办法实现以下逻辑

variable "environment" {
description = "The environment this will be run in can only be set to [preprod|test|prod]"
type = string
default = "test"
validation {
condition = can(regex("^(prod|preprod|test)$", var.environment))
error_message = "The environment variable can only be set to [prod|preprod|test]."
}
}

variable "fet_code" {
description = "Set the feature code"
type = string
default = ""
validation {
condition = var.environment == "test" && length(var.fet_code) != 3
error_message = "The environment has been set to 'test' but the fet_code has not be defined."
}
}
目前我收到以下错误:
Error: Invalid reference in variable validation

on variable.tf line 17, in variable "fet_code":
17: condition = var.environment == "fet" && length(var.fet_code) == 3

The condition for variable "fet_code" can only refer to the variable itself,
using var.fet_code.
我明白代码的问题是什么,我只是想知道是否有办法绕过限制?

最佳答案

虽然有一个 Github issue要将其作为一项功能实现,验证多个变量的唯一方法是使用局部变量在运行时抛出错误:

variable "environment" {
description = "The environment this will be run in can only be set to [preprod|test|prod]"
type = string
default = "test"
validation {
condition = can(regex("^(prod|preprod|test)$", var.environment))
error_message = "The environment variable can only be set to [prod|preprod|test]."
}
}

variable "fet_code" {
description = "Set the feature code"
type = string
default = ""
}

locals {
validate_fet_code_cnd = var.environment == "test" && length(var.fet_code) != 3
validate_fet_code_msg = "The environment has been set to 'test' but the fet_code has not been defined."
validate_fet_code_chk = regex(
"^${local.validate_fet_code_msg}$",
( !local.validate_fet_code_cnd
? local.validate_fet_code_msg
: "" ) )
}
这是一个凌乱、粗略的 hack,但它应该可以防止应用无效值。

关于Terraform 13,根据另一个值验证变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63629916/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com