Terraform v0.12.x,AWS提供商
我正在尝试编写一个通用模块,该模块将返回传入的EBS卷名的现有EBS快照,或者获取传入EBS卷名的快照。无论哪种方式,它都应该返回快照id。
下面是获取现有快照的代码。
data "aws_ebs_snapshot" "snapshot" {
most_recent = true
filter {
name = "tag:Name"
values = [var.ebs_id]
}
filter {
name = "status"
values = ["completed"]
}
}
output "snapshot_id" {
value = data.aws_ebs_snapshot.snapshot.id
description = "Jenkins master snapshot id"
}这是拍摄快照的代码。
data "aws_ebs_volume" "ebs" {
most_recent = true
filter {
name = "tag:Name"
values = [var.ebs_id]
}
}
// Take a snapshot of the green EBS resource
resource "aws_ebs_snapshot" "snapshot" {
volume_id = data.aws_ebs_volume.ebs.id
}
output "snapshot_id" {
value = aws_ebs_snapshot.snapshot.id
description = "Jenkins master snapshot id"
}有可能这样做吗?如果是的话,怎么做?我知道我可以把它们分成两个不同的模块,但是很幽默。
# try/catch block is of course pseudo-code
try {
# Get an existing snapshot id
data "aws_ebs_snapshot" "snapshot" {
most_recent = true
filter {
name = "tag:Name"
values = [var.ebs_name]
}
filter {
name = "status"
values = ["completed"]
}
}
output "snapshot_id" {
value = data.aws_ebs_snapshot.snapshot.id
description = "Jenkins master snapshot id"
}
}
catch() {
# Get the volume id and take a snapshot
data "aws_ebs_volume" "ebs" {
most_recent = true
filter {
name = "tag:Name"
values = [var.ebs_id]
}
}
// Take a snapshot of the green EBS resource
resource "aws_ebs_snapshot" "snapshot" {
volume_id = data.aws_ebs_volume.ebs.id
}
output "snapshot_id" {
value = aws_ebs_snapshot.snapshot.id
description = "Jenkins master snapshot id"
}
}我知道try/catch块不是以这种方式在Terraform中使用的,那么我如何才能实现我想要的呢?
发布于 2020-11-23 22:56:21
您所描述的情况似乎不像需要基于远程系统进行动态决策的情况,因为您可以从输入变量中完全判断调用方是指定快照id还是指定卷id:
variable "ebs_name" {
type = string
default = null
}
variable "ebs_id" {
type = string
default = null
}
data "aws_ebs_snapshot" "snapshot" {
count = var.ebs_name != null ? 1 : 0
most_recent = true
filter {
name = "tag:Name"
values = [var.ebs_name]
}
filter {
name = "status"
values = ["completed"]
}
}
data "aws_ebs_volume" "ebs" {
count = var.ebs_id != null ? 1 : 0
most_recent = true
filter {
name = "tag:Name"
values = [var.ebs_id]
}
}
// Take a snapshot of the green EBS resource
resource "aws_ebs_snapshot" "snapshot" {
count = var.ebs_id != null ? 1 : 0
volume_id = data.aws_ebs_volume.ebs[count.index].id
}
output "snapshot_id" {
# Return either the generated snapshot or the given
# snapshot. If the caller specified both for some
# reason then the generated snapshot takes priority.
# This will produce an error if neither var.ebs_name
# nor var.ebs_id is set, because the result will have
# no elements.
value = concat(
aws_ebs_snapshot.snapshot[*].id,
data.aws_ebs_snapshot.snapshot[*].id,
)[0]
description = "Jenkins master snapshot id"
}为了完整,如果其他人将来找到了这个答案,我想指出的是,模块组成指南建议直接为每一种情况编写简单的读取或创建代码,而不是在这样的情况下做出动态决定,但是我展示了上面的动态示例,因为您建议(通过引用使用两个模块来解决这个问题的可能性)您已经考虑并决定不使用组合样式。
发布于 2020-11-20 20:12:11
乍一看,您可能认为可以通过数据源检查快照是否存在,如果数据源没有返回任何内容,则可以在资源上使用类似于count的内容创建快照。不幸的是,Terraform不是这样工作的,因为数据源如果找不到匹配项,就会抛出一个错误,从而导致Terraform退出。
请参阅HashiCorp 这里的官方响应,当您被问到所要寻找的功能类型时。
这里所要求的动态决策与Terraform的设计目标背道而驰,因为它使配置成为对可能是什么而不是什么的描述。
通常,通过AWS脚本,或者类似Python/boto3 3脚本,而不是Terraform来更好地处理这类事情。
https://stackoverflow.com/questions/64935659
复制相似问题