I have an SNS module that creates an SNS topic.
It outputs a simple string
output "topic" {
value = aws_sns_topic.topic.arn
}
This is then used in my two other modules, the “EC/Redis” and “ECS” modules:
module "ecs" { # and "redis"
sns_topic = module.sns.topic
}
Each of these modules takes a string that can be null:
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:
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
count = var.sns_topic != null ? 1 : 0
[...]
}
HOWEVER, for some reason I get:
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…