处理 Terraform AMI 寻找返回一个空列表

Handling Terraform AMI looking returning an empty list

是否有比以下方法更好的方法来处理 Terraform 数据资源aws_ami_idsreturn空列表?

总是希望模块 return 找到最新的 AMI ID。

如果列表为空,我得到的是 "list "data.aws_ami_ids.full_unencrypted_ami.ids" 没有任何元素,因此无法确定类型。"错误,所以这是解决方法。

       data "aws_ami_ids" "full_unencrypted_ami" {
          name_regex  = "${var.ami_unencrypted_regex}"
          owners = ["123456789","self"]
       }

       locals {
         notfound = "${list("AMI Not Found")}"
         unencrypted_ami = "${concat(data.aws_ami_ids.full_unencrypted_ami.ids,local.notfound)}"
       }

       output "full_ami_unencrypted_id" {
         description = "Full Unencrypted AMI ID"
         value       = "${local.full_unencrypted_ami[0]}"
       }

1) 使用 aws_ami_id 而不是 aws_ami_ids,这样如果 AMI 消失,terraform apply 就会失败,迫使您更新 Terraform 解决方案。

2) 创建两个 aws_ami_ids 数据源(第二个是回退),连接结果并获取第一个项目。但是,正如 ydaetskcoR 所暗示的那样,您为什么想要这种隐式(可能未被发现)的回退?