Skip to content

Commit 94c0395

Browse files
authored
Merge pull request #5 from Daemon-Solutions/SD-2156
SD-2156. PagerDuty Integration
2 parents 397dbda + 219acba commit 94c0395

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

include/lambda.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import urllib3
77
import traceback
8+
import datetime
89
from string import Template
910

1011

@@ -13,6 +14,12 @@
1314
ttl = int(os.environ['TTL'])
1415
webhook_url = os.environ['SLACK_WEBHOOK']
1516
environment = os.environ['ENVIRONMENT']
17+
pd_key = os.environ['PD_KEY']
18+
dedup_pd_key = os.environ['PD_DEDUP_KEY']
19+
pd_message = os.environ['PD_MESSAGE']
20+
datestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
21+
22+
PD_DATA = ('{"payload": {"summary": "' + pd_message + '","timestamp": "' + datestamp + '","severity": "critical","source": "Laguna Rundeck PROD"},"routing_key": "' + pd_key + '", "dedup_key": "' + dedup_pd_key + '","event_action": "trigger"}')
1623

1724
private_instance_record_template = os.environ['PRIVATE_INSTANCE_RECORD_TEMPLATE']
1825
private_asg_record_template = os.environ['PRIVATE_ASG_RECORD_TEMPLATE']
@@ -191,6 +198,20 @@ def lambda_handler(event, context):
191198
if changes:
192199
change_rrs(changes, zone_id)
193200
slack_notification('Rundeck ' + environment + ' has restarted!!')
201+
202+
#PagerDuty Alert
203+
if environment == "production":
204+
data = json.loads(PD_DATA)
205+
data = json.dumps(data)
206+
data = data.encode()
207+
http = urllib3.PoolManager()
208+
response = http.request('POST',
209+
'https://events.pagerduty.com/v2/enqueue',
210+
body = data,
211+
headers = {'Content-Type': 'application/json'},
212+
retries = False)
213+
content = response.read()
214+
print(content)
194215

195216

196217
# helpers

main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ resource "aws_lambda_function" "manage_dns" {
2727
SERVICE = var.service
2828
SLACK_WEBHOOK = var.slack_webhook
2929
ENVIRONMENT = var.environment
30+
PD_KEY = var.pd_key
31+
PD_DEDUP_KEY = var.dedup_pd_key
32+
PD_MESSAGE = var.pd_message
3033
PRIVATE_INSTANCE_RECORD_TEMPLATE = var.private_instance_record_template
3134
PRIVATE_ASG_RECORD_TEMPLATE = var.private_asg_record_template
3235
PUBLIC_ASG_RECORD_TEMPLATE = var.public_asg_record_template
@@ -40,7 +43,7 @@ resource "aws_lambda_function" "manage_dns" {
4043

4144
resource "null_resource" "notify_sns_topic" {
4245
depends_on = [aws_lambda_function.manage_dns]
43-
count = var.asg_count == "1" && var.enabled == "1" ? 1 : 0
46+
count = var.asg_count == "1" && var.enabled == "1" ? 1 : 0
4447

4548
triggers = {
4649
zone_id = var.zone_id

variables.tf

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,35 @@ variable "ttl" {
6969
default = 60
7070
}
7171

72-
variable runtime {
72+
variable "runtime" {
7373
description = "Runtime binary"
7474
default = "python3.7"
7575
}
7676

77+
#SD-2156
7778
variable "slack_webhook" {
78-
description = "slack webhook for notifications"
79+
type = string
80+
description = "slack webhook for notifications"
7981
}
8082

8183
variable "environment" {
82-
description = "Environment"
84+
type = string
85+
description = "Environment"
86+
}
87+
88+
variable "pd_key" {
89+
type = string
90+
description = "PagerDuty Token Key"
91+
}
92+
93+
variable "dedup_pd_key" {
94+
type = string
95+
description = "PagerDuty Dedup Key"
96+
}
97+
98+
variable "pd_message" {
99+
type = string
100+
description = "PagerDuty Message"
101+
default = "Error!"
83102
}
84103

0 commit comments

Comments
 (0)