Answers: <p>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.</p>
  
  <p>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):</p>
  
  <pre><code>output "alb_arn" {
    value = aws_alb.model-server.arn
  }
  </code></pre>
  
  <pre><code>module "common" {
    source = "./modules/common"
  }
  
  module "other" {
    source = "./modules/other"
  
    target_group_depends_on = module.common.alb_arn
  }
  </code></pre>
  
  <pre><code>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]
  }
  </code></pre>
  
  <hr>
  
  <p>The main missing feature for the above in Terraform 0.11 is that it only supports resources in <code>depends_on</code>, and not other kinds of object like variables.</p>
  
  <p>However, it's possible to work around that by introducing an additional do-nothing resource that exists only to be a dependency:</p>
  
  <pre><code>output "alb_arn" {
    value = "${aws_alb.model-server.arn}"
  }
  </code></pre>
  
  <pre><code>module "common" {
    source = "./modules/common"
  }
  
  module "other" {
    source = "./modules/other"
  
    target_group_depends_on = "${module.common.alb_arn}"
  }
  </code></pre>
  
  <pre><code>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"]
  }
  </code></pre>
  
  <p>Note that in Terraform 0.11 the <code>depends_on</code> 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 <code>"var.dependency"</code> would not have worked there either because only resource addresses are accepted as explicit dependencies in Terraform 0.11.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-15T02:28:32.280Z
Answers: 2020-01-15T02:28:32.280Z
Answers: Martin Atkins
Body: <p>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.</p>
  
  <p>I was trying to achieve just that with this as output in the moduleA</p>
  
  <pre><code>output "alb_arn" {
    value = "${aws_alb.model-server.arn}"
  }
  </code></pre>
  
  <p>Then in the main.tf i.e root i added this,, in the moduleB</p>
  
  <pre><code>  dependency                          = "${module.common.alb_arn}"
  </code></pre>
  
  <p>Then in the moduleB variable file referenced it like this.</p>
  
  <pre><code>variable "dependency" { }
  </code></pre>
  
  <p>And in the moduleB main,tf used it as depends_on like this</p>
  
  <pre><code>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}"
    }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Error: aws_alb_target_group.modelServer: depends on value cannot contain interpolations: ${var.dependency}
  </code></pre>
  
Tags: <terraform><terraform-module>
Title: How to create dependency between modules in terraform
UserDisplayName: Mohammed Ali
ThreadText: # How to create dependency between modules in terraform
  
  (Asked by: username_0 on 2020-01-14)
  
  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}
  
  ```
  
  **Tags:** terraform, terraform-module
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-15. Score: 3
  
  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.
  
  
  ---
  
  