如果无法找到要从中还原的快照,如何在 terraform 中创建 ebs 卷

How do I create an ebs volume in terraform if it could not find a snapshot to restore from

所以我正在阅读此处的文档:https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ebs_volume

它说我们可以使用 SnapshotId= 或 size= 这样快照 id 将从给定 id 的快照创建卷,而 size= 将创建一个全新的卷。

我有这个数据源来访问我的快照:

data "aws_ebs_snapshot" "p4_ebs_snapshot" {
  most_recent = true
  owners = ["self"]

  filter {
    name = "tag:Name"
    values = ["PerforceDepots"]
  }
}

我想知道是否可以这样做:

if data.foundSnapshot?
    snapshotId=data.id;
else
    size=100
end

上面显然只是伪代码,但我想知道如何在 terraform 中做这样的事情。可能吗?

遗憾的是,没有 external data source 就不可能。 TF 没有检查某物是否存在的概念。如果任何数据源不存在,它就会出错。解决这个问题的唯一方法是开发您自己的数据源,它可以提供这样的功能。