Could you please help on this. Below I have elaborated what I’m trying to acheive.
How to have both “aws_ebs_volume” “size” and “aws_volume_attachment” “device_name” in the same map of “node_disks” so that I can call it in the resource.
Example:
I could have a list of data_disks as well it’s corresponding disk_size as variable input as shown below.
variable "dnodes" {
default = ["1", "2", "3"]
}
variable "data_disks" {
description = "device list for EC2 mapping"
default = ["/dev/xvdb", "/dev/xvdc" ]
}
variable "disk_size" {
description = "device list for EC2 mapping"
default = ["10", "30"]
}
I want to be able to have something like this.
node_disks = {
"1:/dev/xvdb" = {
"disk_dev_path" = "/dev/xvdb"
"node_name" = "1"
"disk_size" = "10"
}
"1:/dev/xvdc" = {
"disk_dev_path" = "/dev/xvdc"
"node_name" = "1"
"disk_size" = "30"
}
"2:/dev/xvdb" = {
"disk_dev_path" = "/dev/xvdb"
"node_name" = "2"
"disk_size" = "10"
}
"2:/dev/xvdc" = {
"disk_dev_path" = "/dev/xvdc"
"node_name" = "2"
"disk_size" = "30"
}
"3:/dev/xvdb" = {
"disk_dev_path" = "/dev/xvdb"
"node_name" = "3"
"disk_size" = "10"
}
"3:/dev/xvdc" = {
"disk_dev_path" = "/dev/xvdc"
"node_name" = "3"
"disk_size" = "30"
}
}
I want to be able to use it here as shown below:
resource "aws_ebs_volume" "ebs_volume" {
for_each = local.node_disks
availability_zone = "us-west-1a"
size = each.value.disk_size
I tried to build another map for size as shown below,
locals {
ebs_size = { for pair in setproduct(local.dnodes, var.disk_size) : "${pair[0]}:${pair[1]}" => {
node_name = pair[0]
data_dev_size = pair[1]
} }
}
However when I run plan, it throws below error.
resource "aws_ebs_volume" "data_disk" {
for_each = local.ebs_size
availability_zone = "us-west-1a"
size = each.value.data_dev_size
type = "gp2"
}
resource "aws_volume_attachment" "dnode_data_disk" {
for_each = local.node_disks
device_name = each.value.disk_dev_path
instance_id = aws_instance.this[each.value.node_name].id
volume_id = aws_ebs_volume.data_disk[each.key].id
force_detach = true
}
Error: Invalid index
on ../../main.tf line 87, in resource "aws_volume_attachment" "dnode_data_disk":
87: volume_id = aws_ebs_volume.data_disk[each.key].id
|----------------
| aws_ebs_volume.data_disk is object with 4 attributes
| each.key is "1:/dev/xvdb"