简体   繁体   English

Terraform - 创建多个 EBS 卷

[英]Terraform - creating multiple EBS volumes

How would I go about creating and attaching more than one EBS volume to an instance?我 go 如何创建多个 EBS 卷并将其附加到一个实例?

The code below works when attaching a single EBS volume.以下代码在附加单个 EBS 卷时有效。 My main concern is creating a map between the size of the EBS volume and the device name.我主要关心的是在 EBS 卷的大小和设备名称之间创建一个 map。 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. 您快到了,尝试使用element(list, index) -它会遍历整个列表。 For example, this config will successfully create 2 ec2 instances with 3 additional ebs volumes attached to each: 例如,此配置将成功创建2个ec2实例,每个实例附加3个附加ebs卷:

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.下面的解决方案适用于跨多个 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 .这里device_name是字符串列表,所以我们需要通过尽可能多的名额外的体积和volume_count的数量是数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.具有多个不同大小的 EBS 卷的多个 EC2 实例。 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))}"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 terraform 中的 map 创建多个不同大小的 ebs 卷? - How can I create several ebs volumes each of a different size using a map in terraform? 使用 Terraform 通过 EC2 创建时如何向 AWS EBS 添加标签? - How can I add a tag to AWS EBS when creating through EC2 with Terraform? 在不丢失数据的情况下删除未使用的 EBS 卷 - Delete EBS volumes not in use without losing data Terraform 启动模板为 AWS EKS Cluster Autoscaling Group 创建两个卷 - Terraform launch template creating two volumes for AWS EKS Cluster Autoscaling Group 如何使用 terraform 从 Ami(具有多个卷 -3+)旋转 ec2 - How to spin the ec2 from Ami(with multiple volumes -3+) using terraform Terraform:创建并验证多个 ACM 证书 - Terraform: Creating and validating multiple ACM certificates 优化大 EBS 卷初始化(预热) - Optimize Big EBS Volumes Initialization (Warm-up) 如何在区域范围内启用“始终加密新 EBS 卷”设置? - How to enable the "Always encrypt new EBS volumes" regionwide setting? 使用 terraform 创建多个实例和一个网络接口时出错 - Error when creating multiple instances and one network interface using terraform API 网关资源在不考虑 state 的情况下使用 terraform 创建多次 - API Gateway resources are creating multiple times with terraform without considering state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM