Terraform: adding depends_on to your custom modules

Bogdan Sulima
1 min readMay 19, 2017

--

Association between some resources is deferred and takes some time. If you don’t explicitly pass the association resource as a variable to your custom module, terraform might fail to provision. Terraform’s modules do not support depends_on out of the box, but it is not difficult to emulate it.

# File fancy-app-module/variables.tfvariable depends_on { default = [], type = "list"}
# File my-app.tf
module "app" {
source = "modules/fancy-app-module"
# Wait for resources and associations to be created
depends_on = [
"${aws_alb_target_group.app.arn}"
]
}
resource "aws_alb_target_group" "app" {
name = "app-group"
}
resource "aws_alb_listener" "front_end" {
# Association of default_action takes some time and
# if this action is required by you module, it's creation
# might fail due to async provisioning of the
# resources by terraform
default_action {
target_group_arn = "${aws_alb_target_group.app.id}"
type = "forward"
}
}

--

--