0%

Terraform - Add dependency for modules

Error

Error creating application autoscaling target: ValidationException: ECS service doesn’t exist: service/AAA

Encountered this type of dependency issue before, but re-run terraform plan/apply usually can fix it, but not lucky this time :-)

After having a quick look, and found out the module depends_on (dependency between terraform modules) only officially supported for Terraform version 0.13 - hashicorp/terraform#10462 - but we’re on version 0.12 unfortunately.

Workaround

Found the workaround here:

The key insight here is that variables are nodes in the dependency graph too, and so can use them as a “hub” for passing dependencies across the module boundary.

Example

e.g.

In module A (e.g. with resource aws_appautoscaling_target)

  1. Add the following in variables.yml:
1
2
3
4
variable "mod_depends_on" {
type = any
default = null
}
  1. Add depends_on for resource A aws_appautoscaling_target (as depends_on can refer directly to variables in Terraform version 0.12)
1
2
3
4
resource "aws_appautoscaling_target" "target" {
...
depends_on = [var.mod_depends_on]
}

In module B (e.g. with resource aws_ecs_service):

Just need to add the following:

1
mod_depends_on = [module.A]

Reference:

https://discuss.hashicorp.com/t/tips-howto-implement-module-depends-on-emulation/2305

Welcome to my other publishing channels