0
votes

My current Terraform is configured to build several AWS volumes which are all of the same size. A couple of our servers now require bigger volumes. Is it possible to create multiple volumes of different size in the same resource block?

Alternatively I'd have to split this block up (1 block per volume size), which would require me to rebuild a lot of my Terraform configuration i.e. in volume attachment section etc. I'd prefer to avoid this if possible.

I need the third and fourth volumes in the count to be of a bigger size. Initially I thought to use some conditional logic such as If/Else however I'm not sure how to implement this in terraform. I'm using terraform 0.12.18.

Pseudo-code:

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"
}
1
Have you thought about for_each? create a key-value variable and then iterate over it - Amit Baranes
You would want to use an iterator over a map containing the key/value pairs for your different sizes. - Matt Schuchard

1 Answers

0
votes

you can do like below.

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

you can also use 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"
}