Ravi kant Gautam
Ravi kant Gautam

Reputation: 348

How to create the different size of EBS volume and attach to single EC2 instance using Terraform?

I am creating multiple volumes with different sizes like below:

resource "aws_ebs_volume" "ebs_volume" {
  for_each = {
    0 = 100
    1 = 50
    2 = 20
    3 = 20
    4 = 50
  }
  availability_zone = var.availability_zone
  size              = each.value
  type              = "gp2"
}

I will create multiple volumes like the above. Now, I want to attach it to a single EC2 instance. In below, resource ec2_production I am defining the configuration for root volume i.e. size is 40GB and volume type is gp2. Now I want to create 5 five more volume like mentioned in the above resource ebs_volume and attach them to my below-defined ec2_production instance

resource "aws_instance" "ec2_production" {
  count         = "${var.ec2_instance_count}"
  ami           = "${var.ami}"
  availability_zone = var.availability_zone
  instance_type = "${var.ec2_instance_type}"
  subnet_id = aws_subnet.subnet.id
  associate_public_ip_address = true
  vpc_security_group_ids = [aws_security_group.security_group_access_internet.id]
  key_name = "key-pair"
  ebs_block_device {
    device_name = "/dev/sda1"
    volume_type = "gp2"
    volume_size = 40
  }
  tags = {
    Name = var.tag
  }
}

This is the resource for the EBS_volume_attachment:

I am not sure how to pass the devices name and how to attach all the EBS volume to my AWS instance:

resource "aws_volume_attachment" "volume_attachement" {
  count       = "${var.ec2_instance_count * var.ebs_volume_count}"
  volume_id   = "${aws_ebs_volume.ebs_volume.*.id[count.index]}"
  device_name = "${element(var.ec2_device_names, count.index)}"
  instance_id = "${element(aws_instance.ec2_production.*.id, count.index)}"

}

My variable.tf file

variable "ec2_device_names" {
    default = [
    "/dev/sdf",
    "/dev/sdg",
    "/dev/sdh",
    "/dev/sdi",
    "/dev/sdj"
  ]
}

variable "ec2_instance_count" {
  default = 1
}

variable "ec2_instance_type" {
  default = "t2.micro"
}

variable "ami" {
    default = "ami-0d382e80be7ffdae5"
}

Is there any way I can attach all the volume to each device I have created above and attach to my EC2 instance?

Upvotes: 0

Views: 2701

Answers (3)

Bishal Paudel
Bishal Paudel

Reputation: 46

resource "aws_ebs_volume" "ebs_volume" {
  count             = var.instance_count * var.volume_count
  availability_zone = aws_instance.ec2[count.index < var.volume_count ? 0 : 1 ].availability_zone
  size              = var.additional_volume_size[count.index%var.volume_count]
}

resource "aws_volume_attachment" "volume_attachement" {
  count       = var.instance_count * var.volume_count
  volume_id   = element(aws_ebs_volume.ebs_volume.*.id, count.index)
  device_name = element(var.device_name, count.index)
  instance_id = element(aws_instance.ec2.*.id, count.index < var.volume_count ? 0 : 1)
  force_detach = true
}

Upvotes: 2

Adib
Adib

Reputation: 49

You need to define the attachment of EBS to instance/instances. After that, you need to mount ones. I have a small cheatsheet for one Instance-EBS you may use.

Upvotes: -1

yvesonline
yvesonline

Reputation: 4857

First I think your aws_ebs_volume is wrongly named in the question but this might be just a copy & paste error, it should be resource "aws_ebs_volume" "ebs_volume" not resource "aws_ebs_volume" "for_each".

Second in your aws_volume_attachment you're accessing the ebs_volume index incorrectly, you want to do volume_id = aws_ebs_volume.ebs_volume[count.index].id, i.e. access the list of ebs_volume at the count.index position and then get the attribute id.

Thirdly your count in the aws_volume_attachment is iterating over too much and the instance_id can be fixed.

So your updated code for the aws_volume_attachment should look like:

resource "aws_volume_attachment" "volume_attachement" {
  count       = var.ebs_volume_count
  volume_id   = aws_ebs_volume.ebs_volume[count.index].id
  device_name = "${element(var.ec2_device_names, count.index)}"
  instance_id = aws_instance.ec2_production.id
}

Upvotes: 2

Related Questions