当索引[0]超出计数时,如何防止Terraform删除资源
我以前曾创建了一个类型的CloudFlare记录,并希望在添加另一个类型。因此,我定义了一个旗帜以跳过它。
resource "cloudflare_record" "cloudflare-a-record" {
count = var.flag != false ? 1 : 0
zone_id = var.zone_id
name = var.sub_domain
type = "A"
value = aws_eip.my_eip.public_ip
ttl = 1
proxied = false
}
resource "cloudflare_record" "vault-cloudflare-cname-record" {
count = var.flag == false ? 1 : 0
zone_id = var.zone_id
name = cloudflare_record.cloudflare-a-record.hostname
type = "CNAME"
value = aws_eip.my_eip.public_dns
ttl = 1
proxied = false
}
但是Terraform通过以下消息删除了此资源:
cloudflare_record.vault-cloudflare-a record [0]将被销毁 (因为索引[0]的数量不超出计数)
是否有另一种忽略此资源的方法?还是代码错误?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您不能将相同的标志用于两个不同的资源,因为A和CNAME资源的变量值将保持不变。我看到的方式在当前代码中有两个可能的选项,因为您使用了不同的条件(在您使用
==
中,在CNAME中,您使用!=
):这意味着,如果
flag = false
将创建记录(因为计数将为1
),并且不会创建CNAME记录(因为计数将为<代码> 0 )。现在,如果flag
的值更改为true
,则A记录将被删除(因为true == false
将返回False
)和CNAME记录将创建(因为true!= false
将为true
)。这意味着相同的flag
不应用于两个不同的资源。您可以将两个资源的条件使用相同的条件使用,这意味着两者都会共同创建/删除(不确定这是您想要的)。更好的方法是定义两个变量,一个用于A,一个用于CNAME记录:然后,在您的代码中,您将更改使用
count
meta-argument的行,以下方式您可以控制如果您想同时创建或仅一个。另请注意以下内容:
当变量类型
bool
(true
或false
)时,当在条件中使用它们时,您不必检查他们对另一个布尔值的平等性,因为最左端的值无论如何<代码> true 或false
。因此,例如,如果您设置create_a_record = true
,那将使上述表达式:这将评估为
count = 1
。您还可以设置变量的默认值,例如,如果要确保记录始终存在,则可以做到这一点:[1] https://www.terraform.io/language/language/expressions/conditionals
In this case, you cannot use the same flag for two different resources as the variable value will remain the same for both A and CNAME resources. The way I see it there are two possible options with the current code since you are using different conditionals (in A you use
==
and in CNAME you use!=
):This means if the
flag = false
the A record will be created (as the count will be1
) and the CNAME record will not be created (as the count will be0
). Now, if theflag
's value changes totrue
, then the A record will be deleted (astrue == false
will returnfalse
) and the CNAME record will be created (astrue != false
will betrue
). This means that the sameflag
should not be used for two different resources. You could use the same conditional for both resources, which means that both would be created/deleted together (not sure if that is what you want). A better way would be to define two variables, one for A and one for CNAME record:Then, in your code you would change the lines that use the
count
meta-argument to:This way you can control if you want to have both created or only one. Also note the following:
When variables are of type
bool
(true
orfalse
), when using them in conditionals, you do not have to check their equality against another boolean, as the left-most value will anyway be eithertrue
orfalse
. So for example, if you setcreate_a_record = true
, that would make the above expression:and that would evaluate to
count = 1
. You could also set default values for variables, e.g., if you want to make sure the A record is always there, you can just do this:[1] https://www.terraform.io/language/expressions/conditionals