Home > Enterprise >  Terraform conditional dynamic ebs_block_device while deploying EC2 instances
Terraform conditional dynamic ebs_block_device while deploying EC2 instances

Time:04-07

I am trying to create multiple EC2 instances with a conditional ebs_block_device . Here is input object:

  "Ec2Instances" : [
  {
    "AmiId" : "",
    "VpcId" : "vpc-c2401eaa",
    "KeyName" : "",
    "SubnetId" : "subnet-82e4c4eb",
    "PrivateIp" : "null",
    "VolumeSize" : 100,
    "VolumeType" : "null",
    "Ec2InstanceName" : "test",
    "Ec2InstanceType" : "t2.micro"
  },
  {
    "AmiId" : "",
    "VpcId" : "vpc-c2401eaa",
    "KeyName" : "",
    "SubnetId" : "subnet-82e4c4eb",
    "PrivateIp" : "null",
    "VolumeSize" : 0,
    "VolumeType" : "null",
    "Ec2InstanceName" : "null",
    "Ec2InstanceType" : "t2.micro"
  }]

I am trying to write a logic where the ebs_block_device volume should only be created if the VolumeSize is not 0. The count doesn't work in the block so I am not sure if this can be achieved by terraform.

resource "aws_instance" "virtual_machine" {
  count       = length(var.Ec2Instances)
  depends_on = [
    aws_network_interface.bootstrap_cluster_network_interface
  ]
  ami           = var.Ec2Instances[count.index].AmiId == "" || var.Ec2Instances[count.index].AmiId == null || var.Ec2Instances[count.index].AmiId == "null" ? data.aws_ami.ec2_vm.id : var.Ec2Instances[count.index].AmiId
  instance_type = var.Ec2Instances[count.index].Ec2InstanceType == "" || var.Ec2Instances[count.index].Ec2InstanceType == null || var.Ec2Instances[count.index].Ec2InstanceType == "null" ? "${local.default_instance_type}" : var.Ec2Instances[count.index].Ec2InstanceType
  key_name      = var.Ec2Instances[count.index].KeyName == null || var.Ec2Instances[count.index].KeyName == "" || var.Ec2Instances[count.index].KeyName == "null" ? 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 == "" || var.Ec2Instances[count.index].VolumeType == "null" || var.Ec2Instances[count.index].VolumeType == null ? "${local.default_volume_type}" : 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 == "" || var.Ec2Instances[count.index].VolumeSize == "" ? 0 : var.Ec2Instances[count.index].VolumeSize])
      encrypted             = true
      tags = "${merge(
    tomap({
      Name = var.Ec2Instances[count.index].Ec2InstanceName == "" || var.Ec2Instances[count.index].Ec2InstanceName == null || var.Ec2Instances[count.index].Ec2InstanceName == "null" ?  "${local.VolumeName}-${count.index}" : "${var.Ec2Instances[count.index].Ec2InstanceName}-volume"}), var.Tags
      )}"
    }
    ebs_block_device {
    device_name = "/dev/sda2"
    volume_type = var.Ec2Instances[count.index].VolumeType == "" || var.Ec2Instances[count.index].VolumeType == "null" || var.Ec2Instances[count.index].VolumeType == null ? "${local.default_volume_type}" : var.Ec2Instances[count.index].VolumeType
    volume_size = var.Ec2Instances[count.index].VolumeSize
    encrypted   = true
}
}

CodePudding user response:

You can dynamic blocks for that. Assuming that everything else in your code is correct, you can do:

resource "aws_instance" "virtual_machine" {
  count       = length(var.Ec2Instances)
  depends_on = [
    aws_network_interface.bootstrap_cluster_network_interface
  ]
  ami           = var.Ec2Instances[count.index].AmiId == "" || var.Ec2Instances[count.index].AmiId == null || var.Ec2Instances[count.index].AmiId == "null" ? data.aws_ami.ec2_vm.id : var.Ec2Instances[count.index].AmiId
  instance_type = var.Ec2Instances[count.index].Ec2InstanceType == "" || var.Ec2Instances[count.index].Ec2InstanceType == null || var.Ec2Instances[count.index].Ec2InstanceType == "null" ? "${local.default_instance_type}" : var.Ec2Instances[count.index].Ec2InstanceType
  key_name      = var.Ec2Instances[count.index].KeyName == null || var.Ec2Instances[count.index].KeyName == "" || var.Ec2Instances[count.index].KeyName == "null" ? 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 == "" || var.Ec2Instances[count.index].VolumeType == "null" || var.Ec2Instances[count.index].VolumeType == null ? "${local.default_volume_type}" : 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 == "" || var.Ec2Instances[count.index].VolumeSize == "" ? 0 : var.Ec2Instances[count.index].VolumeSize])
      encrypted             = true
      tags = "${merge(
    tomap({
      Name = var.Ec2Instances[count.index].Ec2InstanceName == "" || var.Ec2Instances[count.index].Ec2InstanceName == null || var.Ec2Instances[count.index].Ec2InstanceName == "null" ?  "${local.VolumeName}-${count.index}" : "${var.Ec2Instances[count.index].Ec2InstanceName}-volume"}), var.Tags
      )}"
    }

    dynamic "ebs_block_device" {
        for_each = var.Ec2Instances[count.index].VolumeSize > 0 ? [1] : []
        content {
            device_name = "/dev/sda2"
            volume_type = var.Ec2Instances[count.index].VolumeType == "" || var.Ec2Instances[count.index].VolumeType == "null" || var.Ec2Instances[count.index].VolumeType == null ? "${local.default_volume_type}" : var.Ec2Instances[count.index].VolumeType
            volume_size = var.Ec2Instances[count.index].VolumeSize
            encrypted   = true
        }
    }

}
  • Related