Terraform: Using depend_on from a module

Created on 24 May 2019  ยท  3Comments  ยท  Source: hashicorp/terraform

Running Terraform 0.11.13 and trying to pass a depends on into a module. I came up with a nice hack idea:

module "demo_ecs_service" {
    source = "../modules/ecs-service"
    depends_on = [
        "${module.demo_alb.target_group_arn}",
    ]
}
# ../modules/ecs-service/ecs-service.tf
variable "depends_on" {
  type    = "list"
  default = []
}

resource "aws_ecs_service" "service" {
  depends_on = [
    "${var.depends_on}",
  ]
}

But unfortunately this is erroring with:

Error: aws_ecs_service.service: depends on value cannot contain interpolations: ${var.depends_on}

How can I accomplish this?

config enhancement

Most helpful comment

Hi @nodesocket!

Dependencies are not themselves expressions, because Terraform uses the dependencies to decide which order to resolve the expressions. Therefore the names in depends_on must be constants.

However, because variables themselves are graph nodes that can be depended on, you can get a similar effect by depending on the variable itself, rather than the _value_ of the variable.

variable "service_depends_on" {
  type    = any
  default = []
}

resource "aws_ecs_service" "service" {
  depends_on = [
    # This is a dependency on the variable itself, so the variable's _value_ doesn't
    # actually matter at all, but anything that is referenced when generating that
    # value will be a dependency of the variable, so transitively this will depend on
    # those objects.
    var.service_depends_on,
  ]
}

The above is using Terraform v0.12 syntax and I _think_ may not work exactly like that in Terraform v0.11, since the set of valid strings in depends_on was smaller in Terraform v0.11. It may be necessary to introduce an extra resource as an indirection in v0.11 so that there's something that is valid to depends_on:

variable "service_depends_on" {
  type    = "list"
  default = []
}

resource "null_resource" "service_depends_on" {
  triggers = {
    # This reference creates an implicit dependency on the variable, and thus
    # transitively on everything the variable itself depends on.
    deps = "${jsonencode(var.service_depends_on)}"
  }
}

resource "aws_ecs_service" "service" {
  depends_on = [
    # Now we depend on the null_resource above, which in turn depends on the
    # variable, which then in turn depends on all of the objects that were referenced
    # to create its value.
    # (I'd recommend giving just "var.service_depends_on" a try here first though;
    # it might work, if v0.11 was set up to give variables a "dependable name",
    # which I don't recall off the top of my head.)
    "null_resource.service_depends_on",
  ]
}

Note also that I called the variable service_depends_on rather than just depends_on, because in Terraform v0.12 the variable name depends_on is reserved to allow for potential future first-class support for depends_on in module blocks, managed by Terraform itself.

All 3 comments

Hi @nodesocket!

Dependencies are not themselves expressions, because Terraform uses the dependencies to decide which order to resolve the expressions. Therefore the names in depends_on must be constants.

However, because variables themselves are graph nodes that can be depended on, you can get a similar effect by depending on the variable itself, rather than the _value_ of the variable.

variable "service_depends_on" {
  type    = any
  default = []
}

resource "aws_ecs_service" "service" {
  depends_on = [
    # This is a dependency on the variable itself, so the variable's _value_ doesn't
    # actually matter at all, but anything that is referenced when generating that
    # value will be a dependency of the variable, so transitively this will depend on
    # those objects.
    var.service_depends_on,
  ]
}

The above is using Terraform v0.12 syntax and I _think_ may not work exactly like that in Terraform v0.11, since the set of valid strings in depends_on was smaller in Terraform v0.11. It may be necessary to introduce an extra resource as an indirection in v0.11 so that there's something that is valid to depends_on:

variable "service_depends_on" {
  type    = "list"
  default = []
}

resource "null_resource" "service_depends_on" {
  triggers = {
    # This reference creates an implicit dependency on the variable, and thus
    # transitively on everything the variable itself depends on.
    deps = "${jsonencode(var.service_depends_on)}"
  }
}

resource "aws_ecs_service" "service" {
  depends_on = [
    # Now we depend on the null_resource above, which in turn depends on the
    # variable, which then in turn depends on all of the objects that were referenced
    # to create its value.
    # (I'd recommend giving just "var.service_depends_on" a try here first though;
    # it might work, if v0.11 was set up to give variables a "dependable name",
    # which I don't recall off the top of my head.)
    "null_resource.service_depends_on",
  ]
}

Note also that I called the variable service_depends_on rather than just depends_on, because in Terraform v0.12 the variable name depends_on is reserved to allow for potential future first-class support for depends_on in module blocks, managed by Terraform itself.

Closing this because we shipped depends_on for modules in 0.13.x even though this isn't exactly the same thing. :)

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rjinski picture rjinski  ยท  3Comments

shanmugakarna picture shanmugakarna  ยท  3Comments

zeninfinity picture zeninfinity  ยท  3Comments

ketzacoatl picture ketzacoatl  ยท  3Comments

darron picture darron  ยท  3Comments