I have the following configuration in vars file which defines an EC2 configuration with ebs volumes.
vars file
SQLEC2s = {
"Machine1" = {
name = "QAIftik1"
subnetId = "subnet-0dc5bca32c0ca73b7"
defaultSecurityGroupId = "sg-0f8a3b8dc370fa481"
additionalSecurityGroupId = ""
regionName = "east-1"
EBSVolumes = [
{
deviceName = "xvdg"
driveLetter = "M"
size = 500
},
{
deviceName = "xvdh"
driveLetter = "N"
size = 100
},
{
deviceName = "xvdi"
driveLetter = "D"
size = 100
},
]
},
"Machine2" = {
name = "QRETSQLWEB1"
subnetId = "subnet-0dc5bca32c0ca73b7"
defaultSecurityGroupId = "sg-0f8a3b8dc370fa481"
additionalSecurityGroupId = ""
regionName = "east-1"
EBSVolumes = [
{
deviceName = "xvdg"
driveLetter = "M"
size = 500
},
{
deviceName = "xvdh"
driveLetter = "N"
size = 100
},
{
deviceName = "xvdi"
driveLetter = "D"
size = 100
},
]
},
#Create EC2 Instance.
resource "aws_instance" "SQLWebEditionEast1" {
for_each = { for key, val in var.SQLEC2s :
key => val if val.regionName == "east-1" }
ami = var.amiSQLServerWebEditionEast1
subnet_id = each.value.subnetId
iam_instance_profile = aws_iam_instance_profile.ReturnsSQLInstanceProfile.id
vpc_security_group_ids = [var.default_security_group_id, aws_security_group.CloudReturnsEC2Sg.id] #[each.value.defaultSecurityGroupId, each.value.additionalSecurityGroupId]
instance_type = var.instance_type
key_name = var.key_name
root_block_device {
delete_on_termination = true
volume_size = 70
volume_type = "gp3"
tags = {
EC2DriveLetter = "C"
Name = each.value.name
}
}
tags = {
"Name" = each.value.name
"Component" = "ReturnsDatabaseServer"
}
}
#Ebs Volume Attachment
locals {
# A list of objects with one object per instance.
flattened_volumesList = flatten([
for key, machine in var.SQLEC2s : [
for vol in machine.EBSVolumes :
{
regionName = machine.regionName
name = machine.name
subnetId = machine.subnetId
driveLetter = vol.driveLetter
size = vol.size
volNameTag = machine.name
key = key
deviceName = vol.deviceName
}
]
])
}
resource "aws_ebs_volume" "EBSVolumesEast1" {
for_each = { for key, val in local.flattened_volumesList :
key => val if val.regionName == "east-1" }
size = each.value.size
type = "gp3"
availability_zone = aws_instance.SQLWebEditionEast1[each.value.key].availability_zone
tags = {
EC2DriveLetter = each.value.driveLetter
Name = aws_instance.SQLWebEditionEast1[each.value.key].tags["Name"]
}
}
resource "aws_volume_attachment" "AttachEast1EBSVolumes" {
for_each = { for key, val in local.flattened_volumesList :
key => val if val.regionName == "east-1" }
device_name = each.value.deviceName
volume_id = aws_ebs_volume.EBSVolumesEast1[index(local.flattened_volumesList, each.value)].id
instance_id = aws_instance.SQLWebEditionEast1[each.value.key].id
skip_destroy = "false"
}
Issue
When I remove “Machine1” from SQLEc2s var file, the terraform plan shows that Volume attached to Machine 2 will also be reassigned. I only want Machine1 and it’s related EBS volumes be removed. I see in the state file ebs volume attachments are stored as indexkey = “0” … I think that why tf is getting confused. What is the best way to handle this situation?