# How to iterate over all resources of same type?

**URL:** <https://discuss.hashicorp.com/t/how-to-iterate-over-all-resources-of-same-type/31423>\
**Category:** Boundary\
**Created:** [November 2, 2021, 5:01pm UTC](https://discuss.hashicorp.com/t/how-to-iterate-over-all-resources-of-same-type/31423 "2021-11-02T17:01:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtremesay-sereema](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jtremesay-sereema/32/23440_2.png) [@jtremesay-sereema](https://discuss.hashicorp.com/u/jtremesay-sereema)\
**Post date:** [November 2, 2021, 5:01pm UTC](https://discuss.hashicorp.com/t/how-to-iterate-over-all-resources-of-same-type/31423/1 "2021-11-02T17:01:46Z")

</div>

Hi,

I have declared some AWS EC2 instances with terraform

```auto
resource aws_instance "server1" {
    # ...
}

resource aws_instance "server2" {
    # ...
}

resource aws_instance "server3" {
    # ...
}

```

Now, I want to declare some monitoring rules. But since I’m kind of lazy, I don’t want to manually declare the rules for each servers. I have found the `for_each` keyword, but I’m not sure how to use it.

```auto
resource "aws_cloudwatch_metric_alarm" "alarm_cpu" {
  for_each = aws_instance # <= obviously, doesn’t work

  alarm_name = "High CPU"
  namespace = "AWS/EC2"
  metric_name = "CPUUtilization"
  dimensions = {
    InstanceId = each.key.id
  }
  period = "60"
  evaluation_periods = "3"
  datapoints_to_alarm = "3"
  statistic = "Average"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  threshold = "95"
  alarm_actions = [
    aws_sns_topic.alarms.arn]
  ok_actions = [
    aws_sns_topic.alarms.arn]
}

```

---

<div class="post-metadata">

**Author:** ![grantorchard1](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/grantorchard1/32/17626_2.png) [@grantorchard1](https://discuss.hashicorp.com/u/grantorchard1)\
**Post date:** [November 2, 2021, 8:33pm UTC](https://discuss.hashicorp.com/t/how-to-iterate-over-all-resources-of-same-type/31423/2 "2021-11-02T20:33:25Z")

</div>

The `for_each` loop likes to know the values you intend to iterate over ahead of time, so you may be better off using `count` here. That said, it doesn’t massively alter what you’re trying to do.

In your case, I would consider making a middle man with a local variables block so you can easily adjust.

```auto
locals {
  instance_ids = [
    aws_instance.server1.id, 
    aws_instance.server2.id,
    aws_instance.server3.id,
  ]
}

```

Now you can go:

```auto
resource "aws_cloudwatch_metric_alarm" "alarm_cpu" {
  count = length(local.instance_ids)

  alarm_name = "High CPU"
  namespace = "AWS/EC2"
  metric_name = "CPUUtilization"
  dimensions = {
    InstanceId = local.instance_ids.count_index
  }
  period = "60"
  evaluation_periods = "3"
  datapoints_to_alarm = "3"
  statistic = "Average"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  threshold = "95"
  alarm_actions = [
    aws_sns_topic.alarms.arn]
  ok_actions = [
    aws_sns_topic.alarms.arn]
}

```

Hope that helps!

---

<div class="post-metadata">

**Author:** ![jtremesay-sereema](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jtremesay-sereema/32/23440_2.png) [@jtremesay-sereema](https://discuss.hashicorp.com/u/jtremesay-sereema)\
**Post date:** [November 3, 2021, 7:54am UTC](https://discuss.hashicorp.com/t/how-to-iterate-over-all-resources-of-same-type/31423/3 "2021-11-03T07:54:53Z")

</div>

It helps. Thank you for your response!
