Skip to content

Instantly share code, notes, and snippets.

@josh-padnick
Last active December 15, 2021 17:55
Show Gist options
  • Save josh-padnick/0db0befe2129f7fd2163455d9957e619 to your computer and use it in GitHub Desktop.
Save josh-padnick/0db0befe2129f7fd2163455d9957e619 to your computer and use it in GitHub Desktop.
Hack for Terraform Module depends_on
# This file would need to be in its own folder like /tf-bar/main.tf
variable "bar" {
default = "foo"
}
resource "null_resource" "bar" {
triggers {
bar = "${var.bar}"
}
provisioner "local-exec" {
command = "echo 'tf-2.bar sleeping for 5 seconds' && sleep 5"
}
}
output "bar" {
value = "${null_resource.bar.triggers.bar}"
}
# This file would need to be in its own folder like /tf-foo/main.tf
variable "foo" {
default = "foo"
}
resource "null_resource" "foo" {
triggers {
foo = "${var.foo}"
}
provisioner "local-exec" {
command = "echo 'tf-1.foo sleeping for 2 seconds' && sleep 2"
}
}
variable "foo2" {
default = "foo"
}
resource "null_resource" "foo2" {
triggers {
foo2 = "${var.foo2}"
}
provisioner "local-exec" {
command = "echo 'tf-1.foo2 sleeping for 5 seconds' && sleep 5"
}
}
# You must list all resources in the module here, or a resource that depends on an unlisted resource will begin creating
# before the rest of the module's resources have been created.
output "foo" {
value = <<-EOF
${null_resource.foo.triggers.foo}
${null_resource.foo2.triggers.foo2}
EOF
}
# This file would need to be in its own folder like /tf-modules/main.tf
module "foo" {
source = "../tf-foo"
}
module "bar" {
source = "../tf-bar"
bar = "${module.foo.foo}"
}
@josh-padnick
Copy link
Author

josh-padnick commented May 31, 2018

Unfortunately, Terraform doesn't yet support depends_on with modules, however you can approximate this dependency by making clever use of the module's outputs, or the variables passed into it. This shows a (somewhat hacky) example of how to make sure all the resources in module.bar depend on module.foo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment