1

I have 2 modules A and B. I want a particular resource in module B to be created only after a particular resource in module A is completed completely.

I was trying to achieve just that with this as output in the moduleA

output "alb_arn" {
  value = "${aws_alb.model-server.arn}"
}

Then in the main.tf i.e root i added this,, in the moduleB

  dependency                          = "${module.common.alb_arn}"

Then in the moduleB variable file referenced it like this.

variable "dependency" { }

And in the moduleB main,tf used it as depends_on like this

resource "aws_alb_target_group" "modelServer" {
  name_prefix  = "rqm-"
  port         = "${local.modelServer_container_port}"
  protocol     = "HTTP"
  vpc_id       = "${lookup(var.vpc, var.env)}"
  health_check = "${list(local.health_check)}"
  lifecycle {
    create_before_destroy = true
  }
  depends_on = [ "${var.dependency}" ]
  tags {
    Name = "rqm-modelServer-${var.env}"
  }
}

Error:

Error: aws_alb_target_group.modelServer: depends on value cannot contain interpolations: ${var.dependency}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.