[英]attaching multiple ebs volumes to each ec2 instance
I am trying to attach multiple ebs volumes to each instance.我正在尝试将多个 ebs 卷附加到每个实例。 In this particular scenario, I am trying to attach two ebs volumes (/dev/sde, /dev/sdf) to each of the two ec2 instances I am creating.
在这个特定场景中,我试图将两个 ebs 卷(/dev/sde、/dev/sdf)附加到我正在创建的两个 ec2 实例中的每一个。 The number of ebs volumes and the number of ec2 instances are variables (meaning they can change. 3 ebs volumes to each ec2 for total of 3 ec2s etc so they are not constant).
ebs 卷的数量和 ec2 实例的数量是变量(这意味着它们可以改变。每个 ec2 有 3 个 ebs 卷,总共 3 个 ec2 等,所以它们不是常数)。
But some reason, I am getting this error但出于某种原因,我收到了这个错误
Error: Error attaching volume (vol-0f1ace71d7af68b36) to instance
(i-029671a0d4d152761), message: "Invalid value '/dev/sde' for unixDevice. Attachment
point/dev/sde is already in use", code: "InvalidParameterValue"
so here is the block for that attachment work所以这是附件工作的障碍
resource "aws_instance" "data_node" {
count= var.ec2_count
instance_type = var.instance_type
availability_zone = "us-east-1b"
ami = "ami-0747bdcabd34c712a"
key_name = "test-ec2"
tags = {
Name = "ec2-attr"
}
}
resource "aws_ebs_volume" "test_volume" {
count = var.ec2_count * var.test_ebs_volume_count
size= 16000
type = "gp2"
availability_zone = "us-east-1b"
tags = {
Name = "1e-volume-${count.index + 1}"
}
}
resource "aws_volume_attachment" "volume_attachement" {
count = var.ec2_count * var.test_ebs_volume_count
volume_id = aws_ebs_volume.test_volume.*.id[count.index]
device_name = element(var.ec2_device_names, (count.index))
instance_id = element(aws_instance.data_node.*.id, ((count.index+1)%2))
}
variable "ec2_device_names" {
description = "multiple devices for each ec2 instance"
default = [
"/dev/sde",
"/dev/sdf"
]
}
variable "ec2_count" {
default = 2
}
variable "test_ebs_volume_count" {
default = 2
}
variable "test_ebs_volume_size" {
default = 16000
}
I am able to see all 4 ebs volumes being created, and 2 ec2 instances being created.我能够看到正在创建的所有 4 个 ebs 卷,以及正在创建的 2 个 ec2 实例。 But one ec2 instance has /dev/sde attached, and the other one is attached to on the other ec2 to /dev/sdf
但是一个 ec2 实例附加了 /dev/sde,另一个附加到另一个 ec2 到 /dev/sdf
I wanted to see both /dev/sde, and /dev/sdf attached to first ec2 instance and second ec2 instance.我想看到 /dev/sde 和 /dev/sdf 都附加到第一个 ec2 实例和第二个 ec2 实例。
first ec2:
/dev/sde
/dev/sdf
second ec2:
/dev/sde
/dev/sdf
I know there is something I am missing in "aws_volume_attachment" block code.我知道“aws_volume_attachment”块代码中缺少某些内容。 Any suggestions would be greatly appreciated.
任何建议将不胜感激。
I am able to make it work if I change this如果我改变这个,我就能让它工作
variable "ec2_device_names" {
description = "multiple devices for each ec2 instance"
default = [
"/dev/sde",
"/dev/sdf"
]
}
TO到
variable "ec2_device_names" {
description = "multiple devices for each ec2 instance"
default = [
"/dev/sde",
"/dev/sde",
"/dev/sdf",
"/dev/sdf"
]
}
Because I am working with only 2 ec2 instances, the above solution works, but if I have to have 3 ec2 instances that becomes因为我只使用 2 个 ec2 实例,所以上面的解决方案有效,但是如果我必须有 3 个 ec2 实例,它就变成了
variable "ec2_device_names" {
description = "multiple devices for each ec2 instance"
default = [
"/dev/sde",
"/dev/sde",
"/dev/sde",
"/dev/sdf",
"/dev/sdf",
"/dev/sdf"
]
}
see this is not quite an elegant solution.看到这不是一个优雅的解决方案。
I would re-organize your code , and mostly use for_each
, instead of count.我会重新组织您的代码,主要使用
for_each
,而不是计数。 Below is working example of what you can do:以下是您可以做什么的工作示例:
variable "ec2_count" {
default = 2
}
variable "test_ebs_volume_count" {
default = 2
}
variable "test_ebs_volume_size" {
default = 8
}
variable "ec2_device_names" {
description = "multiple devices for each ec2 instance"
default = [
"/dev/sde",
"/dev/sdf"
]
}
resource "aws_instance" "data_node" {
count= var.ec2_count
instance_type = "t2.micro"
availability_zone = "us-east-1b"
ami = "ami-0747bdcabd34c712a"
#key_name = "test-ec2"
tags = {
Name = "ec2-attr"
}
}
locals {
# local map to keep track of relationships between
# ec2 instances and volume devices
instance_device_map = merge([
for idx1, instance in aws_instance.data_node:
{ for idx2, ec2_device_name in var.ec2_device_names:
"${instance.id}-${ec2_device_name}" => {
instance_id = instance.id
ec2_device_name = ec2_device_name
index = idx1*length(var.ec2_device_names) + idx2
}
}
]...)
}
resource "aws_ebs_volume" "test_volume" {
for_each = local.instance_device_map
size= 8
type = "gp2"
availability_zone = "us-east-1b"
tags = {
Name = "1e-volume-${each.value.index}"
}
}
resource "aws_volume_attachment" "volume_attachement" {
for_each = local.instance_device_map
volume_id = aws_ebs_volume.test_volume[each.key].id
device_name = each.value.ec2_device_name
instance_id = each.value.instance_id
}
output "tgest" {
value = local.instance_device_map
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.