First time asking a question so if I've left anything important out let me know!

Terraform v0.11.1 provider.aws v0.1.4

I'm trying to create multiple cloudwatch alarms for multiple instances. I was wondering if there was a way to have the config iterating though a list. I have tried using the following code, but I get "Error: variable "instances": default may not contain interpolations".

variable "instances" {
  description = "Run the EC2 Instances in these Availability Zones"
  type = "list"
  default = ["${aws_instance.my_instance1.id}", "${aws_instance.my_instance2.id}", "${aws_instance.my_instance3.id}", "${aws_instance.my_instance4.id}", "${aws_instance.my_instance5.id}"]

############ Cloudwatch monitoring 
resource "aws_cloudwatch_metric_alarm" "cpu" {
  count                     = 5
  alarm_name                = "terraform-cpu-high-test"
  comparison_operator       = "GreaterThanOrEqualToThreshold"
  evaluation_periods        = "2"
  metric_name               = "CPUUtilization"
  namespace                 = "AWS/EC2"
  period                    = "180"
  statistic                 = "Average"
  threshold                 = "80"
  alarm_description         = "This metric monitors ec2 cpu utilization"
  insufficient_data_actions = []
  dimensions {
    InstanceId = "${element(var.instances, count.index)}"

Is something like this even possible with terraform and if so what's the best way to do it?



Your issue is

default = ["${aws_instance.my_instance1.id}", "${aws_instance.my_instance2.id}", "${aws_instance.my_instance3.id}", "${aws_instance.my_instance4.id}", "${aws_instance.my_instance5.id}"]

That either has to be passed in or set statically.

If my_instance1-5 are the same and can be set with a count in them like above. Then its better to do

InstanceId = "${element(aws_instance.my_instance.*.id, count.index)}"
  • Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values? – Matthew Slater Dec 12 '17 at 7:44

It would help if you show us the code on how you're creating the EC2 instances. If they are created with a count then this should be easier. If they are created in the same template as this Cloudwatch code, then again even easier.

If it's not in the same template, then you will need to use outputs. https://www.terraform.io/docs/configuration/outputs.html This allows a module to output information for other modules to use. To gain information from an output, you need a data source that will allow you to look inside the state file and abstract the EC2 instance ID's. This helps if you're using a remote backend, such as S3.

If it's in the same module and it's using a count to build the instances, then as Mike said, you can use

InstanceId = "${element(aws_instance.my_instance.*.id, count.index)}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.