I was having issues with a module (the AWS ECS module) where when I deleted something, it would always end up with a dependency loop. It didn’t make much sense to me until I started diving into the state file, and I discovered that the state file doesn’t list dependencies the way I expected:
When you have two related resources that depend on eachother, for example an AWS launch template and an AWS autoscaling group, and you want to make a set of them, it’s natural to have something like this:
resource "aws_launch_template" "my_lt" {
for_each = local.node_groups
...
}
resource "aws_autoscaling_group" "mt_asg" {
for_each = local.node_groups
launch_template = aws_launch_template.my_lt[each.key].id
}
The goal would be for each ASG to depend on its matching launch template, but not any other LTs. However this is not what happens: each instance of the ASG says that it depends on “aws_launch.template.lt” - and in fact it does seem to depend on all the launch templates, not just the one it should be matched up with.
This makes it very difficult to tear down some of the resource pairs (by removing elements from local.node_groups) because the dependency chain seems to always result in a dependency loop.
Is this something that terraform 13 will fix? Or are there other ways to get the dependencies to be more specific?