-
Notifications
You must be signed in to change notification settings - Fork 145
/
messagegenerator.py
651 lines (602 loc) · 36.1 KB
/
messagegenerator.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import json
from datetime import datetime
import sys
import logging
logger = logging.getLogger()
def get_message_for_slack(event_details, event_type, affected_accounts, affected_entities, slack_webhook):
message = ""
summary = ""
if slack_webhook == "services": #Handle "Incoming Webhook" webhooks
if len(affected_entities) >= 1:
affected_entities = "\n".join(affected_entities)
if affected_entities == "UNKNOWN":
affected_entities = "All resources\nin region"
else:
affected_entities = "All resources\nin region"
if len(affected_accounts) >= 1:
affected_accounts = "\n".join(affected_accounts)
else:
affected_accounts = "All accounts\nin region"
if event_type == "create":
summary += (
f":rotating_light:*[NEW] AWS Health reported an issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region.*"
)
message = {
"text": summary,
"attachments": [
{
"color": "danger",
"fields": [
{ "title": "Account(s)", "value": affected_accounts, "short": True },
{ "title": "Resource(s)", "value": affected_entities, "short": True },
{ "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True },
{ "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True },
{ "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True },
{ "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True },
{ "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False },
{ "title": "Updates", "value": get_last_aws_update(event_details), "short": False }
],
}
]
}
elif event_type == "resolve":
summary += (
f":heavy_check_mark:*[RESOLVED] The AWS Health issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region is now resolved.*"
)
message = {
"text": summary,
"attachments": [
{
"color": "00ff00",
"fields": [
{ "title": "Account(s)", "value": affected_accounts, "short": True },
{ "title": "Resource(s)", "value": affected_entities, "short": True },
{ "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True },
{ "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True },
{ "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True },
{ "title": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event'].get('endTime')), "short": True },
{ "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True },
{ "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False },
{ "title": "Updates", "value": get_last_aws_update(event_details), "short": False }
],
}
]
}
else: #Handle 'workflows' or 'triggers' webhooks
if len(affected_entities) >= 1:
affected_entities = "\n".join(affected_entities)
if affected_entities == "UNKNOWN":
affected_entities = "All resources\nin region"
else:
affected_entities = "All resources in region"
if len(affected_accounts) >= 1:
affected_accounts = "\n".join(affected_accounts)
else:
affected_accounts = "All accounts in region"
if event_type == "create":
summary += (
f":rotating_light:*[NEW] AWS Health reported an issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region.*"
)
message = {
"text": summary,
"accounts": affected_accounts,
"resources": affected_entities,
"service": event_details['successfulSet'][0]['event']['service'],
"region": event_details['successfulSet'][0]['event']['region'],
"start_time": cleanup_time(event_details['successfulSet'][0]['event']['startTime']),
"status": event_details['successfulSet'][0]['event']['statusCode'],
"event_arn": event_details['successfulSet'][0]['event']['arn'],
"updates": get_last_aws_update(event_details)
}
elif event_type == "resolve":
summary += (
f":heavy_check_mark:*[RESOLVED] The AWS Health issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region is now resolved.*"
)
message = {
"text": summary,
"accounts": affected_accounts,
"resources": affected_entities,
"service": event_details['successfulSet'][0]['event']['service'],
"region": event_details['successfulSet'][0]['event']['region'],
"start_time": cleanup_time(event_details['successfulSet'][0]['event']['startTime']),
"status": event_details['successfulSet'][0]['event']['statusCode'],
"event_arn": event_details['successfulSet'][0]['event']['arn'],
"updates": get_last_aws_update(event_details)
}
print("Message sent to Slack: ", message)
return message
# COMMON compose the event detail field for org and non-org
def get_detail_for_eventbridge(event_details, affected_entities):
message = {}
#replace the key "arn" with eventArn to match event format from aws.health
message["eventArn"] = ""
message.update(event_details['successfulSet'][0]['event'])
message["eventArn"] = message.pop("arn")
#message = event_details['successfulSet'][0]['event']
message["eventDescription"] = event_details["successfulSet"][0]["eventDescription"]
message["affectedEntities"] = affected_entities
# Log length of json message for debugging if eventbridge may reject the message as messages
# are limited in size to 256KB
json_message = json.dumps(message)
print("PHD/SHD Message generated for EventBridge with estimated size ", str(sys.getsizeof(json_message) / 1024), "KB: ", message)
return message
def get_org_message_for_slack(event_details, event_type, affected_org_accounts, affected_org_entities, slack_webhook):
message = ""
summary = ""
if slack_webhook == "webhook":
if len(affected_org_entities) >= 1:
affected_org_entities = "\n".join(affected_org_entities)
else:
affected_org_entities = "All resources\nin region"
if len(affected_org_accounts) >= 1:
affected_org_accounts = "\n".join(affected_org_accounts)
else:
affected_org_accounts = "All accounts\nin region"
if event_type == "create":
summary += (
f":rotating_light:*[NEW] AWS Health reported an issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region.*"
)
message = {
"text": summary,
"attachments": [
{
"color": "danger",
"fields": [
{ "title": "Account(s)", "value": affected_org_accounts, "short": True },
{ "title": "Resource(s)", "value": affected_org_entities, "short": True },
{ "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True },
{ "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True },
{ "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True },
{ "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True },
{ "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False },
{ "title": "Updates", "value": get_last_aws_update(event_details), "short": False }
],
}
]
}
elif event_type == "resolve":
summary += (
f":heavy_check_mark:*[RESOLVED] The AWS Health issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region is now resolved.*"
)
message = {
"text": summary,
"attachments": [
{
"color": "00ff00",
"fields": [
{ "title": "Account(s)", "value": affected_org_accounts, "short": True },
{ "title": "Resource(s)", "value": affected_org_entities, "short": True },
{ "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True },
{ "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True },
{ "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True },
{ "title": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event'].get('endTime')), "short": True },
{ "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True },
{ "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False },
{ "title": "Updates", "value": get_last_aws_update(event_details), "short": False }
],
}
]
}
else:
if len(affected_org_entities) >= 1:
affected_org_entities = "\n".join(affected_org_entities)
else:
affected_org_entities = "All resources in region"
if len(affected_org_accounts) >= 1:
affected_org_accounts = "\n".join(affected_org_accounts)
else:
affected_org_accounts = "All accounts in region"
if event_type == "create":
summary += (
f":rotating_light:*[NEW] AWS Health reported an issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region.*"
)
message = {
"text": summary,
"accounts": affected_org_accounts,
"resources": affected_org_entities,
"service": event_details['successfulSet'][0]['event']['service'],
"region": event_details['successfulSet'][0]['event']['region'],
"start_time": cleanup_time(event_details['successfulSet'][0]['event']['startTime']),
"status": event_details['successfulSet'][0]['event']['statusCode'],
"event_arn": event_details['successfulSet'][0]['event']['arn'],
"updates": get_last_aws_update(event_details)
}
elif event_type == "resolve":
summary += (
f":heavy_check_mark:*[RESOLVED] The AWS Health issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in "
f"the {event_details['successfulSet'][0]['event']['region'].upper()} region is now resolved.*"
)
message = {
"text": summary,
"accounts": affected_org_accounts,
"resources": affected_org_entities,
"service": event_details['successfulSet'][0]['event']['service'],
"region": event_details['successfulSet'][0]['event']['region'],
"start_time": cleanup_time(event_details['successfulSet'][0]['event']['startTime']),
"status": event_details['successfulSet'][0]['event']['statusCode'],
"event_arn": event_details['successfulSet'][0]['event']['arn'],
"updates": get_last_aws_update(event_details)
}
json.dumps(message)
print("Message sent to Slack: ", message)
return message
def get_message_for_chime(event_details, event_type, affected_accounts, affected_entities):
message = ""
if len(affected_entities) >= 1:
affected_entities = "\n".join(affected_entities)
if affected_entities == "UNKNOWN":
affected_entities = "All resources\nin region"
else:
affected_entities = "All resources\nin region"
if len(affected_accounts) >= 1:
affected_accounts = "\n".join(affected_accounts)
else:
affected_accounts = "All accounts\nin region"
summary = ""
if event_type == "create":
message = str("/md" + "\n" + "**:rotating_light:\[NEW\] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event']['service'].upper() + " service in " + event_details['successfulSet'][0]['event']['region'].upper() + " region.**" + "\n"
"---" + "\n"
"**Account(s)**: " + affected_accounts + "\n"
"**Resource(s)**: " + affected_entities + "\n"
"**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n"
"**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n"
"**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n"
"**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n"
"**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n"
"**Updates:**" + "\n" + get_last_aws_update(event_details)
)
elif event_type == "resolve":
message = str("/md" + "\n" + "**:heavy_check_mark:\[RESOLVED\] The AWS Health issue with the " + event_details['successfulSet'][0]['event']['service'].upper() + " service in " + event_details['successfulSet'][0]['event']['region'].upper() + " region is now resolved.**" + "\n"
"---" + "\n"
"**Account(s)**: " + affected_accounts + "\n"
"**Resource(s)**: " + affected_entities + "\n"
"**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n"
"**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n"
"**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n"
"**End Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event'].get('endTime')) + "\n"
"**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n"
"**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n"
"**Updates:**" + "\n" + get_last_aws_update(event_details)
)
message = truncate_message_if_needed(message, 4096)
print("Message sent to Chime: ", message)
return message
def get_org_message_for_chime(event_details, event_type, affected_org_accounts, affected_org_entities):
message = ""
summary = ""
if len(affected_org_entities) >= 1:
affected_org_entities = "\n".join(affected_org_entities)
else:
affected_org_entities = "All resources in region"
if len(affected_org_accounts) >= 1:
affected_org_accounts = "\n".join(affected_org_accounts)
else:
affected_org_accounts = "All accounts in region"
if event_type == "create":
message = str("/md" + "\n" + "**:rotating_light:\[NEW\] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event']['service'].upper()) + " service in " + str(event_details['successfulSet'][0]['event']['region'].upper() + " region**" + "\n"
"---" + "\n"
"**Account(s)**: " + affected_org_accounts + "\n"
"**Resource(s)**: " + affected_org_entities + "\n"
"**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n"
"**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n"
"**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n"
"**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n"
"**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n"
"**Updates:**" + "\n" + get_last_aws_update(event_details)
)
elif event_type == "resolve":
message = str("/md" + "\n" + "**:heavy_check_mark:\[RESOLVED\] The AWS Health issue with the " + event_details['successfulSet'][0]['event']['service'].upper()) + " service in " + str(event_details['successfulSet'][0]['event']['region'].upper() + " region is now resolved.**" + "\n"
"---" + "\n"
"**Account(s)**: " + affected_org_accounts + "\n"
"**Resource(s)**: " + affected_org_entities + "\n"
"**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n"
"**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n"
"**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n"
"**End Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event'].get('endTime')) + "\n"
"**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n"
"**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n"
"**Updates:**" + "\n" + get_last_aws_update(event_details)
)
message = truncate_message_if_needed(message, 4096)
print("Message sent to Chime: ", message)
return message
def get_message_for_teams(event_details, event_type, affected_accounts, affected_entities):
message = ""
if len(affected_entities) >= 1:
affected_entities = "\n".join(affected_entities)
if affected_entities == "UNKNOWN":
affected_entities = "All resources\nin region"
else:
affected_entities = "All resources\nin region"
if len(affected_accounts) >= 1:
affected_accounts = "\n".join(affected_accounts)
else:
affected_accounts = "All accounts\nin region"
summary = ""
if event_type == "create":
title = "🚨 [NEW] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event'][
'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][
'region'].upper() + " region."
message = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "FF0000",
"summary": "AWS Health Aware Alert",
"sections": [
{
"activityTitle": str(title),
"markdown": False,
"facts": [
{"name": "Account(s)", "value": affected_accounts},
{"name": "Resource(s)", "value": affected_entities},
{"name": "Service", "value": event_details['successfulSet'][0]['event']['service']},
{"name": "Region", "value": event_details['successfulSet'][0]['event']['region']},
{"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])},
{"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']},
{"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']},
{"name": "Updates", "value": get_last_aws_update(event_details)}
],
}
]
}
elif event_type == "resolve":
title = "✅ [RESOLVED] The AWS Health issue with the " + event_details['successfulSet'][0]['event'][
'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][
'region'].upper() + " region is now resolved."
message = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "00ff00",
"summary": "AWS Health Aware Alert",
"sections": [
{
"activityTitle": str(title),
"markdown": False,
"facts": [
{"name": "Account(s)", "value": affected_accounts},
{"name": "Resource(s)", "value": affected_entities},
{"name": "Service", "value": event_details['successfulSet'][0]['event']['service']},
{"name": "Region", "value": event_details['successfulSet'][0]['event']['region']},
{"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])},
{"name": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event'].get('endTime'))},
{"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']},
{"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']},
{"name": "Updates", "value": get_last_aws_update(event_details)}
],
}
]
}
print("Message sent to Teams: ", message)
return message
def get_org_message_for_teams(event_details, event_type, affected_org_accounts, affected_org_entities):
message = ""
summary = ""
if len(affected_org_entities) >= 1:
affected_org_entities = "\n".join(affected_org_entities)
else:
affected_org_entities = "All resources in region"
if len(affected_org_accounts) >= 1:
affected_org_accounts = "\n".join(affected_org_accounts)
else:
affected_org_accounts = "All accounts in region"
if event_type == "create":
title = "🚨 [NEW] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event'][
'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][
'region'].upper() + " region."
message = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "FF0000",
"summary": "AWS Health Aware Alert",
"sections": [
{
"activityTitle": title,
"markdown": False,
"facts": [
{"name": "Account(s)", "value": affected_org_accounts},
{"name": "Resource(s)", "value": affected_org_entities},
{"name": "Service", "value": event_details['successfulSet'][0]['event']['service']},
{"name": "Region", "value": event_details['successfulSet'][0]['event']['region']},
{"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])},
{"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']},
{"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']},
{"name": "Updates", "value": event_details['successfulSet'][0]['eventDescription']['latestDescription']}
],
}
]
}
elif event_type == "resolve":
title = "✅ [RESOLVED] The AWS Health issue with the " + event_details['successfulSet'][0]['event'][
'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][
'region'].upper() + " region is now resolved."
message = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "00ff00",
"summary": "AWS Health Aware Alert",
"sections": [
{
"activityTitle": title,
"markdown": False,
"facts": [
{"name": "Account(s)", "value": affected_org_accounts},
{"name": "Resource(s)", "value": affected_org_entities},
{"name": "Service", "value": event_details['successfulSet'][0]['event']['service']},
{"name": "Region", "value": event_details['successfulSet'][0]['event']['region']},
{"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])},
{"name": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event'].get('endTime'))},
{"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']},
{"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']},
{"name": "Updates", "value": event_details['successfulSet'][0]['eventDescription']['latestDescription']}
],
}
]
}
return message
print("Message sent to Teams: ", message)
def get_message_for_email(event_details, event_type, affected_accounts, affected_entities):
# Not srue why we have the new line in the affected entities code here
if len(affected_entities) >= 1:
affected_entities = "\n".join(affected_entities)
if affected_entities == "UNKNOWN":
affected_entities = "All resources\nin region"
else:
affected_entities = "All resources\nin region"
if len(affected_accounts) >= 1:
affected_accounts = "\n".join(affected_accounts)
else:
affected_accounts = "All accounts\nin region"
if event_type == "create":
BODY_HTML = f"""
<html>
<body>
<h>Greetings from AWS Health Aware,</h><br>
<p>There is an AWS incident that is in effect which may likely impact your resources. Here are the details:<br><br>
<b>Account(s):</b> {affected_accounts}<br>
<b>Resource(s):</b> {affected_entities}<br>
<b>Service:</b> {event_details['successfulSet'][0]['event']['service']}<br>
<b>Region:</b> {event_details['successfulSet'][0]['event']['region']}<br>
<b>Start Time (UTC):</b> {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}<br>
<b>Status:</b> {event_details['successfulSet'][0]['event']['statusCode']}<br>
<b>Event ARN:</b> {event_details['successfulSet'][0]['event']['arn']}<br>
<b>Updates:</b> {event_details['successfulSet'][0]['eventDescription']['latestDescription']}<br><br>
For updates, please visit the <a href=https://status.aws.amazon.com>AWS Service Health Dashboard</a><br>
If you are experiencing issues related to this event, please open an <a href=https://console.aws.amazon.com/support/home>AWS Support</a> case within your account.<br><br>
Thanks, <br><br>AHA: AWS Health Aware
</p>
</body>
</html>
"""
else:
BODY_HTML = f"""
<html>
<body>
<h>Greetings again from AWS Health Aware,</h><br>
<p>Good news! The AWS Health incident from earlier has now been marked as resolved.<br><br>
<b>Account(s):</b> {affected_accounts}<br>
<b>Resource(s):</b> {affected_entities}<br>
<b>Service:</b> {event_details['successfulSet'][0]['event']['service']}<br>
<b>Region:</b> {event_details['successfulSet'][0]['event']['region']}<br>
<b>Start Time (UTC):</b> {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}<br>
<b>End Time (UTC):</b> {cleanup_time(event_details['successfulSet'][0]['event'].get('endTime'))}<br>
<b>Status:</b> {event_details['successfulSet'][0]['event']['statusCode']}<br>
<b>Event ARN:</b> {event_details['successfulSet'][0]['event']['arn']}<br>
<b>Updates:</b> {event_details['successfulSet'][0]['eventDescription']['latestDescription']}<br><br>
If you are still experiencing issues related to this event, please open an <a href=https://console.aws.amazon.com/support/home>AWS Support</a> case within your account.<br><br>
<br><br>
Thanks, <br><br>AHA: AWS Health Aware
</p>
</body>
</html>
"""
print("Message sent to Email: ", BODY_HTML)
return BODY_HTML
def get_org_message_for_email(event_details, event_type, affected_org_accounts, affected_org_entities):
if len(affected_org_entities) >= 1:
affected_org_entities = "\n".join(affected_org_entities)
else:
affected_org_entities = "All services related resources in region"
if len(affected_org_accounts) >= 1:
affected_org_accounts = "\n".join(affected_org_accounts)
else:
affected_org_accounts = "All accounts in region"
if event_type == "create":
BODY_HTML = f"""
<html>
<body>
<h>Greetings from AWS Health Aware,</h><br>
<p>There is an AWS incident that is in effect which may likely impact your resources. Here are the details:<br><br>
<b>Account(s):</b> {affected_org_accounts}<br>
<b>Resource(s):</b> {affected_org_entities}<br>
<b>Service:</b> {event_details['successfulSet'][0]['event']['service']}<br>
<b>Region:</b> {event_details['successfulSet'][0]['event']['region']}<br>
<b>Start Time (UTC):</b> {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}<br>
<b>Status:</b> {event_details['successfulSet'][0]['event']['statusCode']}<br>
<b>Event ARN:</b> {event_details['successfulSet'][0]['event']['arn']}<br>
<b>Updates:</b> {event_details['successfulSet'][0]['eventDescription']['latestDescription']}<br><br>
For updates, please visit the <a href=https://status.aws.amazon.com>AWS Service Health Dashboard</a><br>
If you are experiencing issues related to this event, please open an <a href=https://console.aws.amazon.com/support/home>AWS Support</a> case within your account.<br><br>
Thanks, <br><br>AHA: AWS Health Aware
</p>
</body>
</html>
"""
else:
BODY_HTML = f"""
<html>
<body>
<h>Greetings again from AWS Health Aware,</h><br>
<p>Good news! The AWS Health incident from earlier has now been marked as resolved.<br><br>
<b>Account(s):</b> {affected_org_accounts}<br>
<b>Resource(s):</b> {affected_org_entities}<br>
<b>Service:</b> {event_details['successfulSet'][0]['event']['service']}<br>
<b>Region:</b> {event_details['successfulSet'][0]['event']['region']}<br>
<b>Start Time (UTC):</b> {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}<br>
<b>End Time (UTC):</b> {cleanup_time(event_details['successfulSet'][0]['event'].get('endTime'))}<br>
<b>Status:</b> {event_details['successfulSet'][0]['event']['statusCode']}<br>
<b>Event ARN:</b> {event_details['successfulSet'][0]['event']['arn']}<br>
<b>Updates:</b> {event_details['successfulSet'][0]['eventDescription']['latestDescription']}<br><br>
If you are still experiencing issues related to this event, please open an <a href=https://console.aws.amazon.com/support/home>AWS Support</a> case within your account.<br><br>
Thanks, <br><br>AHA: AWS Health Aware
</p>
</body>
</html>
"""
print("Message sent to Email: ", BODY_HTML)
return BODY_HTML
def cleanup_time(event_time):
"""
Takes as input a datetime string as received from The AWS Health event_detail call. It converts this string to a
datetime object, changes the timezone to EST and then formats it into a readable string to display in Slack.
:param event_time: datetime string
:type event_time: str
:return: A formatted string that includes the month, date, year and 12-hour time.
:rtype: str
"""
if not event_time:
return "Unknown"
event_time = datetime.strptime(event_time[:16], '%Y-%m-%d %H:%M')
return event_time.strftime("%Y-%m-%d %H:%M:%S")
def get_last_aws_update(event_details):
"""
Takes as input the event_details and returns the last update from AWS (instead of the entire timeline)
:param event_details: Detailed information about a specific AWS health event.
:type event_details: dict
:return: the last update message from AWS
:rtype: str
"""
aws_message = event_details['successfulSet'][0]['eventDescription']['latestDescription']
return aws_message
def format_date(event_time):
"""
Takes as input a datetime string as received from The AWS Health event_detail call. It converts this string to a
datetime object, changes the timezone to EST and then formats it into a readable string to display in Slack.
:param event_time: datetime string
:type event_time: str
:return: A formatted string that includes the month, date, year and 12-hour time.
:rtype: str
"""
event_time = datetime.strptime(event_time[:16], '%Y-%m-%d %H:%M')
return event_time.strftime('%B %d, %Y at %I:%M %p')
def truncate_message_if_needed(message, max_length):
"""
Truncates the message if it exceeds the specified maximum length.
:param message: Message you want to truncate.
:type message: str
:param max_length: Length at which to truncate the message.
:type max_length: int
:return: Possibly truncated message.
:rtype: str
"""
message_length = len(message)
if message_length > max_length:
print(f"Message length of {message_length} is too long, truncating to {max_length}.")
message = message[:(max_length - 3)] + "..."
return message