Sourcing for ideas here in case I've overlooked any.
Basically I have multiple AWS instances being created, for each of those instances I'm creating cloudwatch alarms.
I've attempted using templates to iterate through:
resource "template_file" "cloudwatch-ec2" {
count = "${var.instance_count}"
vars {
name = "${element(aws_instance.web.*.tags.Name, count.index)}"
instID = "${element(aws_instance.web.*.id, count.index)}"
snsTopic = "${var.snsTopic}"
}
}
This, while appearing to work, would eventually failed in the apply phase due to
var.name (in the template) not being known.
Exact Error:
* template_file.cloudwatch-ec2: failed to render : 7:20: unknown variable accessed: var.name
I tried using modules, but I'm not sure how to get the count into the module.
An example of the alarm resources:
resource "aws_cloudwatch_metric_alarm" "ec2-statusCheck" {
alarm_name = "${var.name}-StatusCheckSystem" comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "StatusCheckFailed_System"
namespace = "AWS/EC2"
period = "300"
statistic = "Maximum"
threshold = "1"
actions_enabled = true
alarm_actions = ["${var.snsTopic}"]
alarm_description = "${var.name} AWS availability degraded" ok_actions = ["${var.snsTopic}"]
dimensions { InstanceID = "${var.instID}" }
}
I have 3 different modules for instance creation, but they all use the same EC2 alarms. Two of the modules have multiple instances (using count).
The only way I can see to do this is to have all instance configurations use count, and then configure the cloudwatch alarm resources to use count?
Any other ideas?
Please let me know if any additional info/clarity would be helpful.
Thank you
-Noah