Skip to content

Instantly share code, notes, and snippets.

@josh-padnick josh-padnick/tf-bar.tf
Last active May 31, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.