使用Terraform创建多个EBS卷的快照

使用Terraform创建多个EBS卷的快照

本文介绍了使用Terraform创建多个EBS卷的快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Terraform基于特定AWS区域中的标签创建某些EBS卷的快照.我曾尝试根据标签过滤EBS卷.当在filter属性中仅指定一个标记值但对于多个值时,我可以获得清晰的EBS卷id输出,我得到以下错误:

I am trying to create snapshots of certain EBS volumes based on tags in a particular AWS region using Terraform.I have tried filtering EBS volumes based on Tags. I can get a clear output of EBS volume id when only one tag value is specified in the filter attribute but for more than one values, i get the following error:

下面是我的Terraform模板:

Below is my terraform template:

data "aws_ebs_volume" "ebs_volume" {
  filter {
    name   = "tag:Name"
    values = ["EBS1","EBS2","EBS3"]
  }
}
output "ebs_volume_id" {
  value = "${data.aws_ebs_volume.ebs_volume.id}"
}

resource "aws_ebs_snapshot" "ebs_volume" {
  volume_id = "${data.aws_ebs_volume.ebs_volume.id}"
}

有没有一种清晰的方法可以使用terraform中的任何一种循环语句来创建多个EBS卷的快照?

Is there a clear way to create snapshots of multiple EBS volumes using any kind of looping statement in terraform?

推荐答案

您可以使用 count元参数遍历列表,创建多个资源或数据源.

You can use the count meta parameter to loop over lists, creating multiple resources or data sources.

在您的情况下,您可以执行以下操作:

In your case you could do something like this:

variable "ebs_volumes" {
  default = [
    "EBS1",
    "EBS2",
    "EBS3",
  ]
}

data "aws_ebs_volume" "ebs_volume" {
  count = "${length(var.ebs_volumes)}"

  filter {
    name   = "tag:Name"
    values = ["${var.ebs_volumes[count.index]}"]
  }
}

output "ebs_volume_ids" {
  value = ["${data.aws_ebs_volume.ebs_volume.*.id}"]
}

resource "aws_ebs_snapshot" "ebs_volume" {
  count     = "${length(var.ebs_volumes)}"
  volume_id = "${data.aws_ebs_volume.ebs_volume.*.id[count.index]}"
}

这篇关于使用Terraform创建多个EBS卷的快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:38