Terraform→PagerDuty Change Notifications
An important thing when you are setting up PagerDuty is to make sure that that the responder has as much context as possible to recent changes of the system. At PlushCare we use Terraform heavily and making sure that a responder to an incident can attribute to a recent change is extremely important in making sure that our uptime and SLOs are met.
We currently use Terraform Cloud to manage our Terraform state and planning/applying any changes pushed to GitHub. Below I will walk you through how we setup to send webhooks from Terraform Cloud to PagerDuty.
-
Go to your Service Directory
-
Click “+ Add Change Integration”
-
Click “+ New Change Integration”
-
Select “PagerDuty Integrations” and from the drop down select “Custom Change Event Transformer”
-
Enter a name for your Integration. Something like “Terraform Cloud” is a good one.
-
Select the service you will be adding this to
-
Click “Next”
-
Click “View Integration”
-
Click “Edit Integration”
-
Take the code below and replace the boilerplate PagerDuty gave you to start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
var data = PD.inputRequest.body; var workspace_name = data.workspace_name ? data.workspace_name : "None"; var payload = { summary: workspace_name + " — " + data.notifications[0].message, source: data.notification_configuration_id, custom_details: { "notification": data.notifications, "Organization": data.organization_name, "Created At": data.run_created_at, "Created By": data.run_created_by, "Run ID": data.run_id, "Workspace ID": data.workspace_id, "Workspace Name": workspace_name, }, }; var links = data.run_url ? [{ href: data.run_url, text: data.run_message }] : null; var changeEvent = { payload: payload, links: links }; PD.emitChangeEvents([changeEvent]);
-
Click “Save changes”
-
Copy the Integration URL. It should look something like:
https://events.pagerduty.com/integration/RANDOM_TEXT/change/enqueue
-
Go to Terraform Cloud and the Workspace you would like to add the integration to
-
Go to Settings > Notifications
-
Click “Create a Notification”
-
Below is my recommended configuration:
- Name: “PagerDuty Change Events”
- Webhook URL: Paste the URL you copied earlier
- Token: Leave blank
- Triggers: Only certain events
- Completed
- Errored
-
Click “Create a Notification”
-
You should see a Last Response section with a status of 202
-
Go back to PagerDuty service page and you should see the verification event in Recent Changes.
Congratulations you should now receive change notifications from Terraform to your PagerDuty Service. One thing we also do is have this setup on many different PagerDuty services, but because we currently have a single “Production” workspace in Terraform we make sure to add multiple webhooks on the Terraform workspace to each service.