The Imperative of Automated Incident Response

In the fast-paced world of modern web and mobile solutions, incidents are inevitable. For organizations like SoftCrafter, which specializes in building robust e-commerce, web, and mobile platforms, ensuring high availability and rapid recovery is paramount. Site Reliability Engineering (SRE) principles advocate for minimizing human intervention in repetitive tasks, and incident response is a prime candidate for automation. Manual processes introduce delays, human error, and increased stress for on-call teams. Automating aspects of incident response, particularly the setup and management of alerting and escalation, is a game-changer.

This article explores how to integrate PagerDuty, a leading incident management platform, with Terraform, the infrastructure-as-code tool, to automate the creation and management of SRE playbooks. This powerful combination allows teams to define their incident response infrastructure declaratively, ensuring consistency, repeatability, and faster recovery times.

Why PagerDuty and Terraform?

PagerDuty excels at aggregating alerts, managing on-call schedules, and orchestrating escalations. It provides a centralized hub for incident communication and resolution. However, manually configuring PagerDuty services, escalation policies, and users can be tedious and error-prone, especially for growing teams and complex systems. This is where Terraform comes in.

Terraform allows you to define your infrastructure in HCL (HashiCorp Configuration Language) files. By treating your PagerDuty configuration as code, you gain several significant advantages:

  • Version Control: Track changes to your incident response setup in Git.
  • Repeatability: Spin up identical PagerDuty configurations across different environments or projects.
  • Consistency: Enforce standardized alerting and escalation policies.
  • Auditability: Easily see who changed what and when.
  • Reduced Toil: Automate the creation and modification of PagerDuty resources.

At SoftCrafter, we leverage these principles across our services to ensure our clients receive robust and resilient solutions, from web development to mobile app development.

Setting Up Your PagerDuty Provider in Terraform

Before you can start defining PagerDuty resources, you need to configure the PagerDuty Terraform provider. First, ensure you have a PagerDuty API token with appropriate permissions (e.g., read/write access to services, escalation policies, and schedules).

Your versions.tf file might look something like this:

terraform {
  bindependencies {
    pagerduty = {
      source  = "PagerDuty/pagerduty"
      version = "~> 2.0"
    }
  }
}

Next, configure the provider in your provider.tf:

provider "pagerduty" {  token = var.pagerduty_api_token}

It’s best practice to pass the API token via an environment variable (TF_VAR_pagerduty_api_token) or a Terraform variable, rather than hardcoding it.

Defining SRE Playbooks with Terraform Resources

Let’s walk through an example of defining common PagerDuty components that form an SRE playbook: users, teams, escalation policies, schedules, and services.

1. PagerDuty Users and Teams

First, define your SRE team members as PagerDuty users and organize them into a team.

resource "pagerduty_user" "sre_engineer_john" {
  name  = "John Doe"
  email = "[email protected]"
  role  = "user"
}
resource "pagerduty_user" "sre_engineer_jane" {
  name  = "Jane Smith"
  email = "[email protected]"
  role  = "user"
}
resource "pagerduty_team" "sre_team" {
  name        = "SoftCrafter SRE Team"
  description = "Team responsible for critical infrastructure and incident response."
  members = [
    pagerduty_user.sre_engineer_john.id,
    pagerduty_user.sre_engineer_jane.id
  ]
}

2. On-Call Schedules

Schedules define who is on call at specific times. This example creates a simple 24/7 rotation.

resource "pagerduty_schedule" "sre_oncall_schedule" {
  name      = "SoftCrafter SRE On-Call Schedule"
  time_zone = "America/New_York"
  layer {
    name  = "Primary On-Call Layer"
    start = "2023-01-01T09:00:00-05:00"
    rotation_turn_length_seconds = 604800 # 1 week
    rotation_virtual_start       = "2023-01-01T09:00:00-05:00"
    users = [
      pagerduty_user.sre_engineer_john.id,
      pagerduty_user.sre_engineer_jane.id
    ]
  }
}

3. Escalation Policies

Escalation policies dictate the order in which users or schedules are notified during an incident.

resource "pagerduty_escalation_policy" "critical_service_escalation" {
  name      = "Critical Service Escalation Policy"
  num_loops = 2 # Escalate twice before stopping
  rule {
    escalation_delay_in_minutes = 15
    target {
      type = "schedule_reference"
      id   = pagerduty_schedule.sre_oncall_schedule.id
    }
  }
  rule {
    escalation_delay_in_minutes = 30
    target {
      type = "team_reference"
      id   = pagerduty_team.sre_team.id
    }
  }
}

4. Services

Finally, tie everything together with a PagerDuty service, which represents a component of your system that can generate incidents.

resource "pagerduty_service" "api_service" {
  name                    = "SoftCrafter API Service"
  auto_resolve_timeout    = 14400 # 4 hours
  acknowledgement_timeout = 600   # 10 minutes
  escalation_policy       = pagerduty_escalation_policy.critical_service_escalation.id
  incident_urgency_rule {
    type    = "constant"
    urgency = "high"
  }
  addons = [
    "https://example.com/api-docs" # Example of adding an addon
  ]
}
resource "pagerduty_service_integration" "api_service_generic_events" {
  name    = "Generic Events Integration"
  type    = "generic_events_api_inbound_integration"
  service = pagerduty_service.api_service.id
}

With these resources defined, a simple terraform apply will provision your entire PagerDuty incident response setup, ready to receive alerts for your critical API service.

Benefits and Best Practices for SRE Teams

Adopting this approach offers significant benefits for SRE teams and organizations focused on operational excellence. For SoftCrafter’s corporate services, this level of automation ensures robust support infrastructure.

  • Standardization: Enforce consistent incident handling across all services.
  • Faster Onboarding: New services can quickly be integrated into the incident response framework.
  • Error Reduction: Eliminate manual configuration mistakes.
  • Disaster Recovery: Recreate your PagerDuty setup rapidly if needed.
  • Collaboration: All changes are reviewed and approved through standard code review processes.

Best Practices:

  • Organize your Terraform code logically, perhaps by team or service.
  • Use modules to encapsulate reusable PagerDuty configurations.
  • Integrate Terraform into your CI/CD pipeline to automatically apply changes.
  • Regularly review your PagerDuty configurations as code to ensure they align with operational needs.

By integrating PagerDuty with Terraform, SRE teams can move beyond reactive incident management to a proactive, automated approach. This not only reduces operational burden but also significantly improves the reliability and resilience of your systems, a core value at SoftCrafter. If you’re looking to enhance your operational capabilities, feel free to contact us to discuss how we can help optimize your infrastructure and incident response strategies.

#SRE #PagerDuty #Terraform #IncidentResponse #Automation #DevOps #InfrastructureAsCode #Cloud

Categorized in:

DevOps & CI/CD,

Last Update: November 27, 2026