如何在 Terraform 中的同一资源块中创建多个不同大小的卷?

How to create multiple Volumes of different sizes in same resource block in Terraform?

我当前的 Terraform 配置为构建多个大小相同的 AWS 卷。我们的一些服务器现在需要更大的容量。是否可以在同一个资源块中创建多个不同大小的卷?

或者我必须将这个块拆分(每个卷大小 1 个块),这将需要我重建很多我的 Terraform 配置,即在卷附件部分等。如果可能。

我需要第三卷和第四卷的尺寸更大一些。最初我想使用一些条件逻辑,例如 If/Else 但是我不确定如何在 terraform 中实现它。我正在使用 terraform 0.12.18.

伪代码:

resource "aws_ebs_volume" "myvolume" {
  provider          = aws.client
  count             = 5
  availability_zone = var.availability_zone
  encrypted         = "true"
  size              = if index aws_ebs_volume.myvolume = 2 or 3 then 300GB else 100GB
  type              = "gp2"
}

你可以像下面那样做。

resource "aws_ebs_volume" "count" {
  count             = 5
  availability_zone = var.availability_zone
  size              = contains([2, 3], count.index) ? 300 : 100
  type              = "gp2"
}

你也可以使用for_each.

resource "aws_ebs_volume" "for_each" {
  for_each = {
    0 = 100
    1 = 100
    2 = 300
    3 = 300
    4 = 100
  }
  availability_zone = var.availability_zone
  size              = each.value
  type              = "gp2"
}