简体   繁体   English

使用Terraform和count()创建EBS快照

[英]Create EBS snapshot using Terraform and count()

I want to create snapshots for all gp2 type EBS volumes. 我想为所有gp2类型的EBS卷创建快照。 I have the following code: 我有以下代码:

data "aws_ebs_volume" "ebs_volumes" {
  filter {
    name   = "volume-type"
    values = ["gp2"]
  }
}

resource "aws_ebs_snapshot" "ebs_snapshot" {
  count = "${length(data.aws_ebs_volume.ebs_volumes.ids)}"
  volume_id = "${element(data.ebs_volume.ebs_volumes.ids, count.index)}"
}

Instead I got the following error: 相反,我得到了以下错误:

terraform plan

Error: resource 'aws_ebs_snapshot.ebs_snapshot' config: unknown resource 'data.ebs_volume.ebs_volumes' referenced in variable data.ebs_volume.ebs_volumes.ids

Since I am using data source, the list returns by data.aws_ebs_volume is dynamic - not a static variable as discussed in Create snapshots of multiple EBS volumes using Terraform . 由于我使用的是数据源,因此列表按data.aws_ebs_volume返回是动态的-不是静态variable使用Terraform创建多个EBS卷的快照中所述

Using terraform show : 使用terraform show

data.aws_ebs_volume.ebs_volumes:
  id = vol-00b3eaaf04b9377cb
  arn = arn:aws:ec2:us-east-1:ACCOUNT_ID:volume/vol-00b3eaaf04b9377cb
  availability_zone = us-east-1c
  encrypted = false
  filter.# = 1
  filter.3737401200.name = volume-type
  filter.3737401200.values.# = 1
  filter.3737401200.values.0 = gp2
  iops = 100
  kms_key_id =
  most_recent = false
  size = 8
  snapshot_id = snap-01d81204beb02804b
  tags.% = 0
  volume_id = vol-00b3eaaf04b9377cb
  volume_type = gp2

There are normally 2 types of data source in Terraform providers, singular ones and plural ones such as aws_ami vs aws_ami_ids with the plural one normally just returning a list of IDs of resources and the singular one offering more information about each specific resource. Terraform提供程序中通常有两种类型的数据源,单数形式和复数形式,例如aws_amiaws_ami_ids ,其中复数形式通常仅返回资源ID的列表,而单数形式则提供有关每个特定资源的更多信息。

Unfortunately the AWS provider has not yet implemented a plural data source for EBS volumes so you are limited to just the singular aws_ebs_volume data source and can't just dynamically return all EBS volumes matching some criteria (such as them being GP2). 不幸的是,AWS提供程序尚未为EBS卷实现多个数据源,因此您仅限于单个aws_ebs_volume数据源 ,并且不能仅动态返回符合某些条件的所有EBS卷(例如它们为GP2)。

As a short term fix, and if you really want to use Terraform to manage creating snapshots directly, you could use an external data source to get the list of EBS volumes outside of Terraform. 作为短期修复,如果您确实要使用Terraform直接管理创建快照,则可以使用外部数据源来获取Terraform之外的EBS卷的列表。

A quick example of this might look something like this (untested): 一个简单的例子可能看起来像这样(未经测试):

data "external" "all_gp2_ebs_volumes" {
  # Should run something like:
  # `aws ec2 describe-volumes --query 'Volumes[].VolumeId' --filters Name=volume-type,Values=gp2`
  # but return the result in a way that the data source needs it.
  program = ["python", "${path.module}/get-all-gp2-ebs-volumes.py"]
}

resource "aws_ebs_snapshot" "ebs_snapshot" {
  count     = "${length(data.external.all_gp2_ebs_volumes.result.ids)}
  volume_id = "${data.external.all_gp2_ebs_volumes.result.ids[count.index]}"
}

Unless the 2 hour period restriction on AWS DLM or AWS Backup is a real blocker for you for some reason I would use those. 除非出于某种原因对您的AWS DLMAWS Backup的2小时限制是真正的阻止者,否则我将使用这些限制。 There is already an aws_dlm_lifecycle_policy resource to configure this via Terraform and then this issue is tracking the creation of the necessary AWS Backup resources. 已经有aws_dlm_lifecycle_policy资源可通过Terraform进行配置,然后此问题将跟踪必要的AWS Backup资源的创建。

Another alternative would be to raise a feature request on the issue tracker for a aws_ebs_volume_ids data source (I couldn't find an existing feature request from a quick look). 另一种替代方法是在问题跟踪器上针对aws_ebs_volume_ids数据源提出功能请求(我无法通过快速查找找到现有的功能请求)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM