简体   繁体   中英

Terraform - creating multiple EBS volumes

How would I go about creating and attaching more than one EBS volume to an instance?

The code below works when attaching a single EBS volume. My main concern is creating a map between the size of the EBS volume and the device name. I've tried a variant of things, creating a list, etc. But no luck.

# Create EBS volume
resource "aws_ebs_volume" "ebs_volume" {
  count                 = "${var.ec2_create_volume == true ? var.ec2_instance_count : 0 }"
  availability_zone     = "${aws_instance.ec2.*.availability_zone[count.index]}"
  size                  = "${var.ec2_ebs_volume_size}"
  type                  = "${var.ec2_ebs_volume_type}"
}

# Attach EBS Volume
resource "aws_volume_attachment" "volume_attachment" {
  count                 = "${var.ec2_create_volume == true ? var.ec2_instance_count : 0 }"
  device_name           = "${var.ec2_device_name}"
  volume_id             = "${aws_ebs_volume.ebs_volume.*.id[count.index]}"
  instance_id           = "${aws_instance.ec2.*.id[count.index]}"
}

You almost there, try using element(list, index) - it will loop over the list. For example, this config will successfully create 2 ec2 instances with 3 additional ebs volumes attached to each:

variable "ec2_device_names" {
  default = [
    "/dev/sdd",
    "/dev/sde",
    "/dev/sdf",
  ]
}

variable "ec2_instance_count" {
  default = 2
}

variable "ec2_ebs_volume_count" {
  default = 3
}

resource "aws_instance" "ec2" {
  count         = "${var.ec2_instance_count}"
  ami           = "${var.aws_ami_id}"
  instance_type = "${var.ec2_instance_type}"
}

resource "aws_ebs_volume" "ebs_volume" {
  count             = "${var.ec2_instance_count * var.ec2_ebs_volume_count}"
  availability_zone = "${element(aws_instance.ec2.*.availability_zone, count.index)}"
  size              = "${var.ec2_ebs_volume_size}"
}

resource "aws_volume_attachment" "volume_attachement" {
  count       = "${var.ec2_instance_count * var.ec2_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.*.id, count.index)}"
}

Incase anyone else is looking for the answer. The solution below works for multiple instances across multiple az. Here device_name is list of string so we need to pass as many names as the number of additional volumes and volume_count is the length of list of number additional_volume_size .

resource "aws_ebs_volume" "ebs_volume" {
      count             = var.instance_count * var.volume_count
      availability_zone = aws_instance.ec2[floor(count.index/var.volume_count)].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, floor(count.index/var.volume_count))
    }

Multiple EC2 instances with multiple EBS volumes of different sizes. This works with odd or even number of volumes.

instance_count = 3
ebs_volume_count        = 2
ec2_ebs_volume_size     = [10, 15]
ec2_device_names        = ["/dev/sdd", "/dev/sde"]

variable "instance_count" {
  type        = number
  default     = 1
}

variable "ebs_volume_count" {
  type        = number
  default     = 0
}

variable "ec2_ebs_volume_size" {
  type        = list(any)
  default = [
    10
  ]
}

variable "ec2_device_names" {
  type = list(any)
  default = [
    "/dev/sdd"
  ]
}

variable "availability_zones" {
  type        = list(any)
}

variable "subnet_ids" {
  type        = list(any)
}

resource "aws_instance" "ec2_instance" {
  count         = var.instance_count 
  ami           = var.aws_ami_id
  availability_zone       = var.availability_zones[count.index]
  subnet_id               = var.subnet_ids[count.index]
  instance_type = var.ec2_instance_type
}

resource "aws_ebs_volume" "ebs_volume" {
  count             = var.instance_count * var.ebs_volume_count
  availability_zone = "${element(aws_instance.ec2_instance.*.availability_zone, floor (count.index/var.ebs_volume_count))}"
  size              = var.ec2_ebs_volume_size[count.index%var.ebs_volume_count]
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM