Format strings in Terraform
variable "elbs" {
description = "ELB names on which to create the alarm"
type = list(string)
default = ["elb1", "elb2", "elb3"]
}
resource "aws_cloudwatch_metric_alarm" "healthy_host_count" {
count = length(var.elbs)
alarm_name = format("%s-%s","healthy_host_count", var.elbs[count.index])
alarm_description = format("%s-%s","healthy_host_count", var.elbs[count.index])
...
dimensions = {
LoadBalancerName = var.elbs[count.index]
}
}
This will create 3 AWS CloudWatch alarms with names/descriptions:
healthy_host_count-elb1,
healthy_host_count-elb2 and
healthy_host_count-elb3.