Hello. This is really hard to explain, but we currently have 3 instances of one type. I was tasked to add 3 more. However the problem is whenever I add more instances, the loop wants to change the order of my volume attachments. Since I used to have 3 types of one node and now I’ve increased it to 6 it sequentially is looping and moving volumes around. Any help?
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "dnode" {
count = "${var.dnodes}"
ami = "${var.ami}"
instance_type = "${var.instance_type_dnode}"
subnet_id = "${var.subnet}"
key_name = "${var.key}"
vpc_security_group_ids = ["${var.security_group}"]
iam_instance_profile = "${var.company}-${lower(var.service)}"
user_data = "${file("./scripts/dnodes.sh")}"
disable_api_termination = false
ebs_optimized = true
monitoring = false
lifecycle {
ignore_changes = ["user_data", "ami"]
}
tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${count.index + 1}"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
"c7n:DoNotPatch" = "True"
"c7n:OffHour" = "off=[(M-H,20),(S,8)];tz=mt"
"c7n:OnHour" = "on=(M-F,00);tz=mt"
}
volume_tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${count.index + 1}_ROOT"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_instance" "enode" {
count = "${var.enodes}"
ami = "${var.ami}"
instance_type = "${var.instance_type_enode}"
subnet_id = "${var.subnet}"
key_name = "${var.key}"
vpc_security_group_ids = ["${var.security_group}"]
iam_instance_profile = "${var.company}-${lower(var.service)}"
user_data = "${file("./scripts/enodes.sh")}"
disable_api_termination = false
lifecycle {
ignore_changes = ["user_data", "ami"]
}
tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${count.index + var.dnodes + 1}"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
"c7n:DoNotPatch" = "True"
"c7n:OffHour" = "off=[(M-H,20),(S,8)];tz=mt"
"c7n:OnHour" = "on=(M-F,00);tz=mt"
}
volume_tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${count.index + var.dnodes + 1}_ROOT"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_ebs_volume" "varopt-dnode" {
count = "${var.dnodes}"
availability_zone = "${var.availability_zone}"
size = 100
type = "gp2"
tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${element(var.dnode_list, count.index)}_VAROPT"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_ebs_volume" "varopt-enode" {
count = "${var.enodes}"
availability_zone = "${var.availability_zone}"
size = 100
type = "gp2"
tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${element(var.enode_list, count.index)}_VAROPT"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_ebs_volume" "data-disk" {
count = "${var.dnodes * 5}"
availability_zone = "${var.availability_zone}"
size = 600
type = "gp2"
tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${element(var.dnode_list, count.index)}_VOL_${element(var.data_volume_device_list, count.index)}"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_ebs_volume" "opt2-enode" {
count = "${var.enodes}"
availability_zone = "${var.availability_zone}"
size = 150
type = "gp2"
tags = {
Name = "${var.service}_${var.environment}_Energy_Node_${element(var.enode_list, count.index)}_OPT2"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_volume_attachment" "varopt-dnode-volume-attachment" {
count = "${var.dnodes}"
device_name = "/dev/sdf"
instance_id = "${element(aws_instance.dnode.*.id, count.index)}"
volume_id = "${element(aws_ebs_volume.varopt-dnode.*.id, count.index)}"
force_detach = true
skip_destroy = true
}
resource "aws_volume_attachment" "varopt-enode-volume-attachment" {
count = "${var.enodes}"
device_name = "/dev/sdf"
instance_id = "${element(aws_instance.enode.*.id, count.index)}"
volume_id = "${element(aws_ebs_volume.varopt-enode.*.id, count.index)}"
force_detach = true
}
resource "aws_volume_attachment" "opt2-enode-volume-attachment" {
count = "${var.enodes}"
device_name = "/dev/sdg"
instance_id = "${element(aws_instance.enode.*.id, count.index)}"
volume_id = "${element(aws_ebs_volume.opt2-enode.*.id, count.index)}"
force_detach = true
}
resource "aws_volume_attachment" "data-volume-attachment" {
count = "${var.dnodes * 5}"
device_name = "${element(var.data_volume_device_list, count.index)}"
instance_id = "${element(aws_instance.dnode.*.id, count.index)}"
volume_id = "${element(aws_ebs_volume.data-disk.*.id, count.index)}"
force_detach = true
}
I think the problem is the way I’m doing my volume attachment using a list. Here’s that variable
variable "data_volume_device_list" {
description = "device list for EC2 mapping"
type = "list"
default = ["/dev/sdg", "/dev/sdh", "/dev/sdi", "/dev/sdj", "/dev/sdk"]
}
Here’s a bit of my plan output:
# aws_volume_attachment.data-volume-attachment[3] must be replaced
-/+ resource "aws_volume_attachment" "data-volume-attachment" {
device_name = "/dev/sdj"
force_detach = true
~ id = "vai-1957865839" -> (known after apply)
~ instance_id = "i-08a576eac17cbc00b" -> (known after apply) # forces replacement
volume_id = "vol-0b70a54ea5a81c718"
}
# aws_volume_attachment.data-volume-attachment[4] must be replaced
-/+ resource "aws_volume_attachment" "data-volume-attachment" {
device_name = "/dev/sdk"
force_detach = true
~ id = "vai-2929341630" -> (known after apply)
~ instance_id = "i-0b9ba0db464e8f0cb" -> (known after apply) # forces replacement
volume_id = "vol-05f4671c1f23853c6"
}
# aws_volume_attachment.data-volume-attachment[5] must be replaced
-/+ resource "aws_volume_attachment" "data-volume-attachment" {
device_name = "/dev/sdg"
force_detach = true
~ id = "vai-1849870220" -> (known after apply)
~ instance_id = "i-061c8a0463e9aefe4" -> (known after apply) # forces replacement
volume_id = "vol-0c1dc11f9c344990d"
}
# aws_volume_attachment.data-volume-attachment[9] must be replaced
-/+ resource "aws_volume_attachment" "data-volume-attachment" {
device_name = "/dev/sdk"
force_detach = true
~ id = "vai-2507744327" -> (known after apply)
~ instance_id = "i-08a576eac17cbc00b" -> (known after apply) # forces replacement
volume_id = "vol-0336e19c00bb07b65"
}
# aws_volume_attachment.data-volume-attachment[10] must be replaced
-/+ resource "aws_volume_attachment" "data-volume-attachment" {
device_name = "/dev/sdg"
force_detach = true
~ id = "vai-1666224816" -> (known after apply)
~ instance_id = "i-0b9ba0db464e8f0cb" -> (known after apply) # forces replacement
volume_id = "vol-0534b6cdd346945a2"
}
# aws_volume_attachment.data-volume-attachment[11] must be replaced
-/+ resource "aws_volume_attachment" "data-volume-attachment" {
device_name = "/dev/sdh"
force_detach = true
~ id = "vai-124253228" -> (known after apply)
~ instance_id = "i-061c8a0463e9aefe4" -> (known after apply) # forces replacement
volume_id = "vol-097031d6d2ef2cc22"
}
# aws_volume_attachment.data-volume-attachment[15] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdg"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[16] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdh"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[17] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdi"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[18] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdj"
+ force_detach = true
+ id = (known after apply)
+ instance_id = "i-08a576eac17cbc00b"
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[19] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdk"
+ force_detach = true
+ id = (known after apply)
+ instance_id = "i-0b9ba0db464e8f0cb"
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[20] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdg"
+ force_detach = true
+ id = (known after apply)
+ instance_id = "i-061c8a0463e9aefe4"
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[21] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdh"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[22] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdi"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[23] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdj"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[24] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdk"
+ force_detach = true
+ id = (known after apply)
+ instance_id = "i-08a576eac17cbc00b"
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[25] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdg"
+ force_detach = true
+ id = (known after apply)
+ instance_id = "i-0b9ba0db464e8f0cb"
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[26] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdh"
+ force_detach = true
+ id = (known after apply)
+ instance_id = "i-061c8a0463e9aefe4"
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[27] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdi"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[28] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdj"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.data-volume-attachment[29] will be created
+ resource "aws_volume_attachment" "data-volume-attachment" {
+ device_name = "/dev/sdk"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ volume_id = (known after apply)
}
# aws_volume_attachment.varopt-dnode-volume-attachment[3] will be created
+ resource "aws_volume_attachment" "varopt-dnode-volume-attachment" {
+ device_name = "/dev/sdf"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ skip_destroy = true
+ volume_id = (known after apply)
}
# aws_volume_attachment.varopt-dnode-volume-attachment[4] will be created
+ resource "aws_volume_attachment" "varopt-dnode-volume-attachment" {
+ device_name = "/dev/sdf"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ skip_destroy = true
+ volume_id = (known after apply)
}
# aws_volume_attachment.varopt-dnode-volume-attachment[5] will be created
+ resource "aws_volume_attachment" "varopt-dnode-volume-attachment" {
+ device_name = "/dev/sdf"
+ force_detach = true
+ id = (known after apply)
+ instance_id = (known after apply)
+ skip_destroy = true
+ volume_id = (known after apply)
}
Plan: 45 to add, 27 to change, 6 to destroy.