skip to Main Content

I’ll try to explain the issue that I’m facing with an example.

I had following terraform code for creating multiple EBS volumes in loop.

main.tf

resource "aws_ebs_volume" "ebs_volume" {
  count             = var.count_drive
  type              = element(var.ebs_drive_type, count.index)
  size              = element(var.ebs_devices_size, count.index)
  iops              = element(var.ebs_iops, count.index)
}

variables.tfvars

ebs_devices_names     = ["/dev/xvdd", "/dev/xvdi", "/dev/xvdg"]
ebs_devices_size      = ["250", "6000", "2000"]
ebs_drive_type        = ["gp3", "io2", "gp3"]
ebs_iops              = ["3000", "5000", "3000"]

Above code is working fine. Now the issue is that I also want to specify throughput. I can add one more variable of list type like others but throughput can only be specified for gp3. Hence I’ll get an error for other EBS types like gp2, io1, io2.

So to summarize what changes need to be done in code so that we can skip throughput assignment for other than gp3 types?

2

Answers


  1. Set it to null if you want to skip it:

    resource "aws_ebs_volume" "ebs_volume" {
      count             = var.count_drive
      type              = element(var.ebs_drive_type, count.index)
      size              = element(var.ebs_devices_size, count.index)
      iops              = element(var.ebs_iops, count.index)
      throughput        = var.ebs_drive_type[count.index] == "gp3" ? var.throughput : null
    }
    
    Login or Signup to reply.
  2. Here is what you could do:

    resource "aws_ebs_volume" "ebs_volume" {
      count             = var.count_drive
      type              = element(var.ebs_drive_type, count.index)
      size              = element(var.ebs_devices_size, count.index)
      iops              = element(var.ebs_drive_type, count.index) == "gp2" ? null : element(var.ebs_iops, count.index)
      throughput        = element(var.ebs_drive_type, count.index) == "gp3" ? var.throughput : null
      availability_zone = var.az
    }
    

    You don’t have to use the variable throughput, you might as well set it to a number you want that falls between the lowest and highest possible throughput. Also, take note that availability_zone is a required parameter. Additionally, the iops parameter is not available for the gp2 type of volume.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search