What you've described here is the recommended way to pass dependencies between modules, but it relies on features introduced in Terraform 0.12. You appear to be using Terraform 0.11, where unfortunately these features are not available.
For Terraform 0.12 it would be idiomatic to write it using the "first-class expressions" syntax, like this (with these examples spanning the three modules just like you did in your opening example):
output "alb_arn" {
value = aws_alb.model-server.arn
}
module "common" {
source = "./modules/common"
}
module "other" {
source = "./modules/other"
target_group_depends_on = module.common.alb_arn
}
variable "target_group_depends_on" {
type = any # only the dependencies matter, not the value
default = null
}
resource "aws_alb_target_group" "modelServer" {
# ...
depends_on = [var.target_group_depends_on]
}
The main missing feature for the above in Terraform 0.11 is that it only supports resources in depends_on
, and not other kinds of object like variables.
However, it's possible to work around that by introducing an additional do-nothing resource that exists only to be a dependency:
output "alb_arn" {
value = "${aws_alb.model-server.arn}"
}
module "common" {
source = "./modules/common"
}
module "other" {
source = "./modules/other"
target_group_depends_on = "${module.common.alb_arn}"
}
variable "target_group_depends_on" {
type = "string" # (because "any" isn't supported in 0.11)
default = ""
}
resource "null_resource" "target_group_depends_on" {
triggers = {
# The reference to the variable here creates an implicit
# dependency on the variable.
dependency = "${var.target_group_depends_on}"
}
}
resource "aws_alb_target_group" "modelServer" {
# ...
# Marking the null_resource as an explicit dependency
# means this indirectly depends on everything the
# null_resource depends on.
depends_on = ["null_resource.target_group_depends_on"]
}
Note that in Terraform 0.11 the depends_on
argument takes a list of literal strings containing resource addresses, not interpolated values. That was the specific reason for the error message you saw when you tried your example, but a reference like "var.dependency"
would not have worked there either because only resource addresses are accepted as explicit dependencies in Terraform 0.11.