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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With