当索引[0]超出计数时,如何防止Terraform删除资源

发布于 2025-02-07 07:45:29 字数 759 浏览 2 评论 0 原文

我以前曾创建了一个类型的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]的数量不超出计数)

是否有另一种忽略此资源的方法?还是代码错误?

I have previously created A type cloudflare record and would like to keep it while adding another one. So I defined a flag to just skip it.

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
}

But Terraform deleted this resource with the following message:

cloudflare_record.vault-cloudflare-a-record[0] will be destroyed
(because index [0] is out of range for count)

Is there another way to ignore this resource? Or is the code wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2025-02-14 07:45:29

在这种情况下,您不能将相同的标志用于两个不同的资源,因为A和CNAME资源的变量值将保持不变。我看到的方式在当前代码中有两个可能的选项,因为您使用了不同的条件(在您使用 == 中,在CNAME中,您使用!= ):

var.flag == false ? 1 : 0 # A record
var.flag != false ? 1 : 0 # CNAME record

这意味着,如果 flag = false 将创建记录(因为计数将为 1 ),并且不会创建CNAME记录(因为计数将为<代码> 0 )。现在,如果 flag 的值更改为 true ,则A记录将被删除(因为 true == false 将返回 False )和CNAME记录将创建(因为 true!= false 将为 true )。这意味着相同的 flag 不应用于两个不同的资源。您可以将两个资源的条件使用相同的条件使用,这意味着两者都会共同创建/删除(不确定这是您想要的)。更好的方法是定义两个变量,一个用于A,一个用于CNAME记录:

variable "create_a_record" {
  type = bool
}

variable "create_cname_record" {
  type = bool
}

然后,在您的代码中,您将更改使用 count meta-argument的行,

resource "cloudflare_record" "cloudflare-a-record" {
  count   = var.create_a_record ? 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.create_cname_record ? 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
}

以下方式您可以控制如果您想同时创建或仅一个。另请注意以下内容:

count = var.create_a_record ? 1 : 0
count = var.create_cname_record ? 1 : 0

当变量类型 bool true false )时,当在条件中使用它们时,您不必检查他们对另一个布尔值的平等性,因为最左端的值无论如何<代码> true 或 false 。因此,例如,如果您设置 create_a_record = true ,那将使上述表达式:

count = true ? 1 : 0

这将评估为 count = 1 。您还可以设置变量的默认值,例如,如果要确保记录始终存在,则可以做到这一点:

variable "create_a_record" {
  type    = bool
  default = true
}

[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 !=):

var.flag == false ? 1 : 0 # A record
var.flag != false ? 1 : 0 # CNAME record

This means if the flag = false the A record will be created (as the count will be 1) and the CNAME record will not be created (as the count will be 0). Now, if the flag's value changes to true, then the A record will be deleted (as true == false will return false) and the CNAME record will be created (as true != false will be true). This means that the same flag 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:

variable "create_a_record" {
  type = bool
}

variable "create_cname_record" {
  type = bool
}

Then, in your code you would change the lines that use the count meta-argument to:

resource "cloudflare_record" "cloudflare-a-record" {
  count   = var.create_a_record ? 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.create_cname_record ? 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
}

This way you can control if you want to have both created or only one. Also note the following:

count = var.create_a_record ? 1 : 0
count = var.create_cname_record ? 1 : 0

When variables are of type bool (true or false), when using them in conditionals, you do not have to check their equality against another boolean, as the left-most value will anyway be either true or false. So for example, if you set create_a_record = true, that would make the above expression:

count = true ? 1 : 0

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:

variable "create_a_record" {
  type    = bool
  default = true
}

[1] https://www.terraform.io/language/expressions/conditionals

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文