display name
display name

Reputation: 328

Terraform Nested Block Not Being Applied As Expected

Using Terraform 1.1.4 and Terragrunt 0.36.0, I'm refining a PagerDuty module and am seeing some unexpected behaviour with some nested blocks returning invalid index errors. Below is the resource I'm working with. This resource creates except when specific nested block inputs are used or not used inside of incident_urgency_rule.

The Resource:

resource "pagerduty_service" "this" {
  name                    = var.name
  description             = var.description
  auto_resolve_timeout    = var.resolve_timeout
  acknowledgement_timeout = var.ack_timeout
  escalation_policy       = data.pagerduty_escalation_policy.this.id
  alert_creation          = var.alert_creation
  dynamic "incident_urgency_rule" {
    for_each = var.incident_urgency_rule
    content {
      type    = incident_urgency_rule.value["type"]
      urgency = incident_urgency_rule.value["urgency"]
      dynamic "during_support_hours" {
        for_each = incident_urgency_rule.value.during_support_hours
        content {
          type    = during_support_hours.value["type"]
          urgency = during_support_hours.value["urgency"]
        }
      }
      dynamic "outside_support_hours" {
        for_each = incident_urgency_rule.value.outside_support_hours
        content {
          type    = outside_support_hours.value["type"]
          urgency = outside_support_hours.value["urgency"]
        }
      }
    }
  }
  dynamic "support_hours" {
    for_each = var.support_hours
    content {
      type         = support_hours.value["type"]
      start_time   = support_hours.value["start_time"]
      end_time     = support_hours.value["end_time"]
      time_zone    = support_hours.value["time_zone"]
      days_of_week = support_hours.value["days_of_week"]
    }
  }
  dynamic "scheduled_actions" {
    for_each = var.scheduled_actions
    content {
      to_urgency = scheduled_actions.value["to_urgency"]
      type       = scheduled_actions.value["type"]
      dynamic "at" {
        for_each = scheduled_actions.value.at
        content {
          name = at.value["name"]
          type = at.value["type"]
        }
      }
    }
  }
}

The Desired Input:

inputs = {
  // PagerDuty Service
  name              = "My Important Service"
  description       = "Service for all prod services."
  escalation_policy = "My Team"
  alert_creation    = "create_alerts_and_incidents"
  resolve_timeout   = 14400
  ack_timeout       = 600
  token             = local.pagerduty_key.key

  // Incident Urgency Rules
  incident_urgency_rule = [{
    type    = "use_support_hours"

    during_support_hours = [
      {
      type    = "constant"
      urgency = "high"
    }]
    outside_support_hours = [
      {
      type    = "constant"
      urgency = "low"
    }]
  }]

  // Support Hours
  support_hours = [
    {
      type = "fixed_time_per_day"
      time_zone = "America/Lima"
      days_of_week = [1, 2, 3, 4, 5]
      start_time  = "05:00:00"
      end_time    = "16:00:00"
    }
  ]
  // Scheduled Actions
  scheduled_actions = [{
    type       = "urgency_change"
    to_urgency = "high"
    at = [{
      type = "named_time"
      name = "support_hours_start"
    }]
  }]
...
...
...
}

The Variables:

variable "incident_urgency_rule" {
  type    = any
  default = []
}

variable "support_hours" {
  type    = any
  default = []
}

variable "scheduled_actions" {
  type    = any
  default = []
}

However, I see an issue:

  1. When urgency is removed from incident_urgency_rule and type is set to use_support hours, an index error is returned. This configuration is however supported by all appearances here.

The error returned is

│ Error: Invalid index
│ 
│   on main.tf line 27, in resource "pagerduty_service" "this":
│   27:       urgency = incident_urgency_rule.value["urgency"]
│     ├────────────────
│     │ incident_urgency_rule.value is object with 3 attributes
│ 
│ The given key does not identify an element in this collection value.

Lastly, on every apply, even when there is no change, Terraform detects one for the incident_urgency_rule

      ~ incident_urgency_rule {
            # (2 unchanged attributes hidden)

          + during_support_hours {
              + type    = "constant"
              + urgency = "high"
            }

          + outside_support_hours {
              + type    = "constant"
              + urgency = "low"
            }
        }

        # (1 unchanged block hidden)
    }

Plan: 0 to add, 2 to change, 0 to destroy.

My question is, it is hard to tell if this is an issue with the PagerDuty API or if there is something I could be doing differently to handle these nested blocks. Thank you for reading.

Cheers.

Upvotes: 0

Views: 213

Answers (0)

Related Questions