4
votes

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
2
Can you clarify what you're asking? But the below code is creating only 2 ebs volumes. This sentence seems unfinished, what was the expected outcome of the code snippet you pasted? Is it not applying the different types correctly? Are you asking for help with additional code to associate it with an EC2 instance?Anthony Neace
@AnthonyNeace i want to know the additional code to attach 2 volumes to each instanceuser6826691

2 Answers

6
votes

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.

3
votes

I will do something in this format:

resource "aws_instance" "example" {
  ami           = "${lookup(var.AMIS, var.AWS_REGION)}"
  instance_type = "t2.micro"

}

resource "aws_ebs_volume" "ebs-volume-1" {
    availability_zone = "eu-west-1a"
    size = 500
    type = "st1"
    tags {
        Name = "more volume"
    }
}

resource "aws_ebs_volume" "ebs-volume-2" {
    availability_zone = "eu-west-1a"
    size = 500
    type = "st1"
    tags {
        Name = "more volume"
    }
}

resource "aws_volume_attachment" "ebs-volume-1-attachment" {
  device_name = "/dev/sdf"
  volume_id = "${aws_ebs_volume.ebs-volume-1.id}"
  instance_id = "${aws_instance.example.id}"
}

resource "aws_volume_attachment" "ebs-volume-2-attachment" {
  device_name = "/dev/sdg"
  volume_id = "${aws_ebs_volume.ebs-volume-2.id}"
  instance_id = "${aws_instance.example.id}"
}

I hope that helps