# Module dependencies doesn't work

**URL:** <https://discuss.hashicorp.com/t/module-dependencies-doesnt-work/53860>\
**Category:** AWS\
**Created:** [May 17, 2023, 7:19am UTC](https://discuss.hashicorp.com/t/module-dependencies-doesnt-work/53860 "2023-05-17T07:19:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![FransUrbo](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/fransurbo/32/31351_2.png) [@FransUrbo](https://discuss.hashicorp.com/u/FransUrbo)\
**Post date:** [May 17, 2023, 7:19am UTC](https://discuss.hashicorp.com/t/module-dependencies-doesnt-work/53860/1 "2023-05-17T07:19:15Z")

</div>

I have an SNS module that creates an SNS topic.

It outputs a simple string

```auto
output "topic" {
    value = aws_sns_topic.topic.arn
}

```

This is then used in my two other modules, the “EC/Redis” and “ECS” modules:

```auto
module "ecs" { # and "redis"
  sns_topic = module.sns.topic
}

```

Each of these modules takes a string that can be null:

```auto
variable "sns_topic" {
  description = "ARN of SNS topic"
  type = string
  default = null
}

```

Then in each of these two modules, I create a `aws_cloudwatch_metric_alarm` within a count, checking for this:

```auto
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
  count = var.sns_topic != null ? 1 : 0

  [...]
}

```

HOWEVER, for some reason I get:

```auto
Error: Invalid count argument

  on .terraform/modules/<redis_module>/alarms.tf line 2, in resource "aws_cloudwatch_metric_alarm" "cpu_high":
   2: count = var.sns_topic != null ? 1 : 0

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

```

Looking at the `plan`, I see that the **sns** module is (supposed to be) created _after_ both the ECS and REDIS modules. If the order of the plan is anything to go by, that is…

I’ve even tried to set a `depends_on = [module.sns]` in both the ECS and REDIS module, but that didn’t help…

Any ideas? Any workaround? Other than going through the modules, one at a time and do an `apply` on them? That’s currently the only way we can get it all built at this time, but I’d like to fix that…

NOTE: We have use-cases where we don’t want/need the extra cost or complexity of alarms and topics, so I’d like to have this as a null-able variable…

---

<div class="post-metadata">

**Author:** ![FransUrbo](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/fransurbo/32/31351_2.png) [@FransUrbo](https://discuss.hashicorp.com/u/FransUrbo)\
**Post date:** [May 19, 2023, 8:06am UTC](https://discuss.hashicorp.com/t/module-dependencies-doesnt-work/53860/2 "2023-05-19T08:06:49Z")

</div>

Turns out converting the output to an `object` and checking for that object to be null worked!

Full explanation here: [Can't get Terraform module dependencies to work - Stack Overflow](https://stackoverflow.com/questions/76278644/cant-get-terraform-module-dependencies-to-work/76285539#76285539)
