使用 Terraform 创建多个 EBS 卷的快照 [英] Create snapshots of multiple EBS volumes using Terraform

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

问题描述

我正在尝试使用 Terraform 基于特定 AWS 区域中的标签创建某些 EBS 卷的快照.我曾尝试根据标签过滤 EBS 卷.当在过滤器属性中仅指定一个标签值时,我可以获得清晰的 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:

data.aws_ebs_volume.ebs_volume:data.aws_ebs_volume.ebs_volume:您的查询返回多个结果.请尝试更具体的搜索条件,或将 most_recent 属性设置为 true.

data.aws_ebs_volume.ebs_volume: data.aws_ebs_volume.ebs_volume: Your query returned more than one result. Please try a more specific search criteria, or set most_recent attribute to true.

下面是我的 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 卷的快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆