title: Looping over local json using for-each in Terraform
body: <p>I am trying to create multiple cloudwatch alarms using input from a json file or json locally declared. Either is fine. I've removed the list of</p>
  <p>The problem is that this is within a module, so I cannot use <code>file.auto.tfvars.json</code> which is how I previously did this.</p>
  <p>File structure:</p>
  <pre><code>.
  ├── cloudwatch.tf
  ├── cloudwatchalarms.json
  └── locals.tf
  </code></pre>
  <p>What I've tried so far:</p>
  <ol>
  <li>Use <code>jsondecode</code> to bring in the <code>cloudwatchalarms.json</code> and loop over it in the following way:</li>
  </ol>
  <p>cloudwatch.tf</p>
  <pre><code># creates alarms based on what exists within cloudwatchalarms.auto.tfvars.json
  resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;alarms&quot; {
    for_each            = { for alarm in local.all_alarms : alarm =&gt; alarm }
    alarm_name          = &quot;${var.awsResourceName}-${each.value.Identifier}&quot;
    namespace           = each.value.Namespace
    comparison_operator = each.value.ComparisonOperator
    evaluation_periods  = each.value.EvaluationPeriods
    statistic           = each.value.Statistic
    treat_missing_data  = each.value.TreatMissingData
    threshold           = each.value.Threshold
    period              = each.value.Period
    metric_name         = each.value.MetricName
  
    dimensions = {
      EcsServiceName = var.awsResourceName
      EcsClusterName = var.ecs_cluster_name
    }
  }
  </code></pre>
  <p>locals.tf</p>
  <pre><code># create locals to pull in cloudwatchalarms.json
  locals {
    cloudwatchAlarms = jsondecode(file(&quot;${path.module}/cloudwatchalarms.json&quot;))
    # loop over the cloudwatchalarms json structure
    all_alarms = [for alarms in local.cloudwatchAlarms.cloudwatchAlarms : alarms.Identifier]
  }
  </code></pre>
  <p>cloudwatchalarms.json</p>
  <pre><code>{
          
          &quot;cloudwatchAlarms&quot;: [
              {
                  &quot;Identifier&quot;: &quot;ServiceMemoryUtilisation&quot;,
                  &quot;Namespace&quot;: &quot;AWS/ECS&quot;,
                  &quot;MetricName&quot;: &quot;MemoryUtilization&quot;,
                  &quot;Statistic&quot;: &quot;Average&quot;,
                  &quot;Threshold&quot;: 90,
                  &quot;Period&quot;: 60,
                  &quot;EvaluationPeriods&quot;: 5,
                  &quot;ComparisonOperator&quot;: &quot;GreaterThanThreshold&quot;,
                  &quot;TreatMissingData&quot;: &quot;missing&quot;
              },
              {
                  &quot;Identifier&quot;: &quot;ServiceCPUUtilisation&quot;,
                  &quot;Namespace&quot;: &quot;AWS/ECS&quot;,
                  &quot;MetricName&quot;: &quot;CPUUtilization&quot;,
                  &quot;Statistic&quot;: &quot;Average&quot;,
                  &quot;Threshold&quot;: 90,
                  &quot;Period&quot;: 60,
                  &quot;EvaluationPeriods&quot;: 5,
                  &quot;ComparisonOperator&quot;: &quot;GreaterThanThreshold&quot;,
                  &quot;TreatMissingData&quot;: &quot;missing&quot;
              }
          ]      
      }
  }
  </code></pre>
  <p>When using this method I get the following errors for every attribute:</p>
  <pre><code>
  Error: Unsupported attribute
  
  
  
    on .terraform/modules/terraform-ecs-monitoring/cloudwatch.tf line 4, in resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;alarms&quot;:
  
     4:   alarm_name          = &quot;${var.awsResourceName}-${each.value.Identifier}&quot;
  
      |----------------
  
      | each.value is &quot;TaskStability&quot;
  
  
  
  This value does not have any attributes.
  </code></pre>
  <ol start="2">
  <li>The second method I tried using was declaring the json structure in <code>locals</code></li>
  </ol>
  <p>cloudwatch.tf</p>
  <pre><code># creates alarms based on what exists within cloudwatchalarms.auto.tfvars.json
  resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;alarms&quot; {
    for_each            = { for alarm in local.cloudwatchAlarms : alarm.Identifier =&gt; alarm }
    alarm_name          = &quot;${var.awsResourceName}-${each.value.Identifier}&quot;
    namespace           = each.value.Namespace
    comparison_operator = each.value.ComparisonOperator
    evaluation_periods  = each.value.EvaluationPeriods
    statistic           = each.value.Statistic
    treat_missing_data  = each.value.TreatMissingData
    threshold           = each.value.Threshold
    period              = each.value.Period
    metric_name         = each.value.MetricName
  
    dimensions = {
      EcsServiceName = var.awsResourceName
      EcsClusterName = var.ecs_cluster_name
    }
  }
  </code></pre>
  <p>locals.tf</p>
  <pre><code>locals {
  
      cloudwatchAlarms = {
          
          &quot;cloudwatchAlarms&quot;: [
              {
                  &quot;Identifier&quot;: &quot;ServiceMemoryUtilisation&quot;,
                  &quot;Namespace&quot;: &quot;AWS/ECS&quot;,
                  &quot;MetricName&quot;: &quot;MemoryUtilization&quot;,
                  &quot;Statistic&quot;: &quot;Average&quot;,
                  &quot;Threshold&quot;: 90,
                  &quot;Period&quot;: 60,
                  &quot;EvaluationPeriods&quot;: 5,
                  &quot;ComparisonOperator&quot;: &quot;GreaterThanThreshold&quot;,
                  &quot;TreatMissingData&quot;: &quot;missing&quot;
              },
              {
                  &quot;Identifier&quot;: &quot;ServiceCPUUtilisation&quot;,
                  &quot;Namespace&quot;: &quot;AWS/ECS&quot;,
                  &quot;MetricName&quot;: &quot;CPUUtilization&quot;,
                  &quot;Statistic&quot;: &quot;Average&quot;,
                  &quot;Threshold&quot;: 90,
                  &quot;Period&quot;: 60,
                  &quot;EvaluationPeriods&quot;: 5,
                  &quot;ComparisonOperator&quot;: &quot;GreaterThanThreshold&quot;,
                  &quot;TreatMissingData&quot;: &quot;missing&quot;
              }
          ]      
      }
  }
  </code></pre>
  <p>I then get this error when trying this method:</p>
  <pre><code>
  Error: Unsupported attribute
  
  
  
    on .terraform/modules/terraform-ecs-monitoring/cloudwatch.tf line 3, in resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;alarms&quot;:
  
     3:   for_each            = { for alarm in local.cloudwatchAlarms : alarm.Identifier =&gt; alarm }
  
  
  
  This value does not have any attributes.
  
  
  
  Throw to stop pipeline
  </code></pre>
  <p>Any help or pointing in the right direction would be great. Thanks.</p>