title: Terraform conditional dynamic ebs_block_device while deploying EC2 instances
body: <p>I am trying to create multiple EC2 instances with a conditional <code>ebs_block_device</code> . Here is input object:</p>
  <pre><code>  &quot;Ec2Instances&quot; : [
    {
      &quot;AmiId&quot; : &quot;&quot;,
      &quot;VpcId&quot; : &quot;vpc-c2401eaa&quot;,
      &quot;KeyName&quot; : &quot;&quot;,
      &quot;SubnetId&quot; : &quot;subnet-82e4c4eb&quot;,
      &quot;PrivateIp&quot; : &quot;null&quot;,
      &quot;VolumeSize&quot; : 100,
      &quot;VolumeType&quot; : &quot;null&quot;,
      &quot;Ec2InstanceName&quot; : &quot;test&quot;,
      &quot;Ec2InstanceType&quot; : &quot;t2.micro&quot;
    },
    {
      &quot;AmiId&quot; : &quot;&quot;,
      &quot;VpcId&quot; : &quot;vpc-c2401eaa&quot;,
      &quot;KeyName&quot; : &quot;&quot;,
      &quot;SubnetId&quot; : &quot;subnet-82e4c4eb&quot;,
      &quot;PrivateIp&quot; : &quot;null&quot;,
      &quot;VolumeSize&quot; : 0,
      &quot;VolumeType&quot; : &quot;null&quot;,
      &quot;Ec2InstanceName&quot; : &quot;null&quot;,
      &quot;Ec2InstanceType&quot; : &quot;t2.micro&quot;
    }]
  </code></pre>
  <p>I am trying to write a logic where the <code>ebs_block_device</code> volume should only be created if the <code>VolumeSize</code> is not 0. The count doesn't work in the block so I am not sure if this can be achieved by terraform.</p>
  <pre><code>resource &quot;aws_instance&quot; &quot;virtual_machine&quot; {
    count       = length(var.Ec2Instances)
    depends_on = [
      aws_network_interface.bootstrap_cluster_network_interface
    ]
    ami           = var.Ec2Instances[count.index].AmiId == &quot;&quot; || var.Ec2Instances[count.index].AmiId == null || var.Ec2Instances[count.index].AmiId == &quot;null&quot; ? data.aws_ami.ec2_vm.id : var.Ec2Instances[count.index].AmiId
    instance_type = var.Ec2Instances[count.index].Ec2InstanceType == &quot;&quot; || var.Ec2Instances[count.index].Ec2InstanceType == null || var.Ec2Instances[count.index].Ec2InstanceType == &quot;null&quot; ? &quot;${local.default_instance_type}&quot; : var.Ec2Instances[count.index].Ec2InstanceType
    key_name      = var.Ec2Instances[count.index].KeyName == null || var.Ec2Instances[count.index].KeyName == &quot;&quot; || var.Ec2Instances[count.index].KeyName == &quot;null&quot; ? local.key_name : var.Ec2Instances[count.index].KeyName
    network_interface {
      network_interface_id = aws_network_interface.bootstrap_cluster_network_interface[count.index].id
      device_index         = 0
    }
    root_block_device {
        volume_type = var.Ec2Instances[count.index].VolumeType == &quot;&quot; || var.Ec2Instances[count.index].VolumeType == &quot;null&quot; || var.Ec2Instances[count.index].VolumeType == null ? &quot;${local.default_volume_type}&quot; : var.Ec2Instances[count.index].VolumeType
        volume_size = sum([lookup(local.custom_amis, var.Ec2Instances[count.index].AmiId, 40), var.Ec2Instances[count.index].VolumeSize == null || var.Ec2Instances[count.index].VolumeSize == &quot;&quot; || var.Ec2Instances[count.index].VolumeSize == &quot;&quot; ? 0 : var.Ec2Instances[count.index].VolumeSize])
        encrypted             = true
        tags = &quot;${merge(
      tomap({
        Name = var.Ec2Instances[count.index].Ec2InstanceName == &quot;&quot; || var.Ec2Instances[count.index].Ec2InstanceName == null || var.Ec2Instances[count.index].Ec2InstanceName == &quot;null&quot; ?  &quot;${local.VolumeName}-${count.index}&quot; : &quot;${var.Ec2Instances[count.index].Ec2InstanceName}-volume&quot;}), var.Tags
        )}&quot;
      }
      ebs_block_device {
      device_name = &quot;/dev/sda2&quot;
      volume_type = var.Ec2Instances[count.index].VolumeType == &quot;&quot; || var.Ec2Instances[count.index].VolumeType == &quot;null&quot; || var.Ec2Instances[count.index].VolumeType == null ? &quot;${local.default_volume_type}&quot; : var.Ec2Instances[count.index].VolumeType
      volume_size = var.Ec2Instances[count.index].VolumeSize
      encrypted   = true
  }
  }
  </code></pre>