Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform ebs volume

Tags:

terraform

I am new to terraform, have created 3 ec2 instances, and i have created 6 ebs volume. How do we attach 2 ebs volumes to each of the three instances?

#Create 6 EBS volumes and attach 2 per instance.
resource "aws_ebs_volume" "vertica_ebs" {
        count                           = "6"
        availability_zone               = "${var.availability_zone}"
        size                            = "500"
        type                            = "st1"

       }
    }

#Attach ebs volume
resource "aws_volume_attachment" "ebs_att" {
     count = "6"
volume {
     device_name = "/dev/sdf"
     volume_id = "[${element(aws_ebs_volume.vertica_ebs.*.id, count.index)}]"
}
volume{
     device_name = "/dev/sdg"
     volume_id = "[${element(aws_ebs_volume.vertica_ebs.*.id, count.index)}]"
}
     instance_id = "[${element(aws_instance.vertica1.*.id,count.index)}]"
}

Errors:

  • aws_volume_attachment.ebs_att #2: "device_name": required field is not set
  • aws_volume_attachment.ebs_att #2: "volume_id": required field is not set
  • aws_volume_attachment.ebs_att #2: : invalid or unknown key: volume
  • aws_volume_attachment.ebs_att #4: "device_name": required field is not set
  • aws_volume_attachment.ebs_att #4: "volume_id": required field is not set
  • aws_volume_attachment.ebs_att #4: : invalid or unknown key: volume
  • aws_volume_attachment.ebs_att #3: "device_name": required field is not set
  • aws_volume_attachment.ebs_att #3: "volume_id": required field is not set
  • aws_volume_attachment.ebs_att #3: : invalid or unknown key: volume
  • aws_volume_attachment.ebs_att #0: "volume_id": required field is not set
  • aws_volume_attachment.ebs_att #0: "device_name": required field is not set
  • aws_volume_attachment.ebs_att #0: : invalid or unknown key: volume
  • aws_volume_attachment.ebs_att #1: "device_name": required field is not set
  • aws_volume_attachment.ebs_att #1: "volume_id": required field is not set
  • aws_volume_attachment.ebs_att #1: : invalid or unknown key: volume
  • aws_volume_attachment.ebs_att #5: "volume_id": required field is not set
  • aws_volume_attachment.ebs_att #5: "device_name": required field is not set
  • aws_volume_attachment.ebs_att #5: : invalid or unknown key: volume
like image 516
user6826691 Avatar asked Jan 05 '23 09:01

user6826691


1 Answers

Note: this method will destroy and recreate the servers. Use 'aws volume attachment' method if this is unacceptable to you.

One way to approach this problem, and how I would solve it, is to attach the ebs volumes directly to the instance resource.

You can do this by adding an 'ebs_block_device' element to each server configuration, then running terraform apply. For example, each server resource you wished to add 2 ebs block devices to, would look like:

resource "aws_instance""example_instance"{
#INSTANCE CONFIGURATION VALUES  
    ebs_block_device{
      device_name = "/dev/sdf"
      volume_size = 500
      volume_type = "st1"
    }
    ebs_block_device{
      device_name = "/dev/sdg"
      volume_size = 500
      volume_type = "st1"
    }
}

Then run terraform plan, see that the block devices will be added to the servers and the servers. Using this method, the servers WILL BE DESTROYED AND RECREATED. If this is acceptable, run terraform apply to rebuild the servers with the additional volumes.

Check out the documentation around ebs_block_device and aws_instance here.

like image 127
Mr.Budris Avatar answered Mar 24 '23 01:03

Mr.Budris