-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
385 lines (378 loc) · 15.3 KB
/
template.yml
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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: A collection of utilities that integrate with CloudWatch
Parameters:
CloudWatchCrossAccountSharingRoleName: { Type: String, Default: CloudWatch-CrossAccountSharingRole }
AlarmReminderSearchAccountIds: { Type: CommaDelimitedList }
AlarmReminderSearchRegions: { Type: CommaDelimitedList }
FatalSmsContactListSnsTopicArn: { Type: String }
Resources:
# This is an _organization sink_ custom EventBridge event bus. It's intended
# to exist once within an AWS Organization (i.e., in a single region in a
# single account).
#
# Relevant CloudWatch activity within the organization is expected to be sent
# to this event bus. Generally this is done by creating rules on all default
# event buses within the organization, which forward CloudWatch events from
# all accounts and regions to this bus.
#
# Any tooling that responds to CloudWatch events can subscribe to
# the relevant events on this bus.
#
# This bus should always be called `CloudWatch-org-sink`, as that is what
# the rules on all default buses expect.
OrgSinkEventBus:
Type: AWS::Events::EventBus
Properties:
Name: CloudWatch-org-sink
OrgSinkEventBusPolicy:
# TODO Should have a Condition to limit this to events sent by events.amazonaws.com
# since it's only intended to get events from other event buses, not from
# any arbitrary sender
Type: AWS::Events::EventBusPolicy
Properties:
EventBusName: !Ref OrgSinkEventBus
StatementId: AllowEventsToCloudWatchOrgSink
Statement:
Action: events:PutEvents
Condition:
StringEquals:
aws:ResourceOrgID: ${aws:PrincipalOrgID}
Effect: Allow
Principal: "*"
Resource: !GetAtt OrgSinkEventBus.Arn
# Sends notifications to Slack for nearly all CloudWatch alarm activity
# from all accounts and all regions across the AWS organization by watching
# the custom event bus
AlarmSlackNotificationsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/alarm-slack-notifications
Description: >-
Sends messages to Slack in response to CloudWatch Alarms state changes
from across the organization. All accounts and all regions forward
CloudWatch Alarms state change events to the custom event bus that
has rules to trigger this function.
Environment:
Variables:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
CROSS_ACCOUNT_CLOUDWATCH_ALARM_IAM_ROLE_NAME: !Ref CloudWatchCrossAccountSharingRoleName
Events:
Alarms:
Properties:
EventBusName: !Ref OrgSinkEventBus
Pattern:
detail-type:
- CloudWatch Alarm State Change
source:
- aws.cloudwatch
Type: EventBridgeRule
Handler: index.handler
MemorySize: 192
Policies:
- Statement:
- Action: events:PutEvents
Effect: Allow
Resource: !Sub arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:event-bus/default
Version: "2012-10-17"
- Statement:
- Action: sts:AssumeRole
Effect: Allow
Resource: !Sub arn:aws:iam::*:role/${CloudWatchCrossAccountSharingRoleName}
Version: "2012-10-17"
Runtime: nodejs22.x
Tags:
prx:meta:tagging-version: "2021-04-07"
prx:cloudformation:stack-name: !Ref AWS::StackName
prx:cloudformation:stack-id: !Ref AWS::StackId
prx:ops:environment: Production
prx:dev:application: CloudWatch Toolkit
Timeout: 20
AlarmSlackNotificationsLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${AlarmSlackNotificationsFunction}
RetentionInDays: 14
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
AlarmSlackNotificationsErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: "MINOR [CloudWatch] Alarm Slack Notifications <prod> EXPERIENCING ERRORS"
AlarmDescription: >-
Errors are occurring on the Lambda function that sends Slack messages
for certain CloudWatch alarms. This could mean that some or all
alarm notifications are not reaching Slack. Check alarm state in the
AWS Console.
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: FunctionName
Value: !Ref AlarmSlackNotificationsFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
Threshold: 0
TreatMissingData: notBreaching
# Sends SMS notifications to a list of phone numbers for certain alarm events
# (like Fatal-level alarms) that are seen on the event bus
AlarmSmsNotificationsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/alarm-sms-notifications
Description: >-
Sends messages to SMS phone numbers in response to CloudWatch Alarms
state changes from across the organization. All accounts and all
regions forward CloudWatch Alarms state change events to the custom
event bus that has rules to trigger this function.
Environment:
Variables:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
FATAL_SMS_CONTACT_LIST_SNS_TOPIC_ARN: !Ref FatalSmsContactListSnsTopicArn
Events:
Alarms:
Properties:
EventBusName: !Ref OrgSinkEventBus
Pattern:
detail-type:
- CloudWatch Alarm State Change
source:
- aws.cloudwatch
Type: EventBridgeRule
Handler: index.handler
MemorySize: 128
Policies:
- Statement:
- Action: sns:Publish
Effect: Allow
Resource: !Ref FatalSmsContactListSnsTopicArn
Version: "2012-10-17"
Runtime: nodejs18.x
Tags:
prx:meta:tagging-version: "2021-04-07"
prx:cloudformation:stack-name: !Ref AWS::StackName
prx:cloudformation:stack-id: !Ref AWS::StackId
prx:ops:environment: Production
prx:dev:application: CloudWatch Toolkit
Timeout: 20
AlarmSmsNotificationsLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${AlarmSmsNotificationsFunction}
RetentionInDays: 14
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
AlarmSmsNotificationsErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: "MINOR [CloudWatch] Alarm SMS Notifications <prod> EXPERIENCING ERRORS"
AlarmDescription: >-
Errors are occurring on the Lambda function that sends SMS messages for
certain CloudWatch alarms. Make sure you aware of all outstanding
fatal-level alarms.
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: FunctionName
Value: !Ref AlarmSmsNotificationsFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
Threshold: 0
TreatMissingData: notBreaching
# Scans certain accounts and regions for long-running CloudWatch alarms and
# sends reminder messages to Slack when they are found.
AlarmSlackRemindersFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/alarm-slack-reminders
Description: >-
Scans a set of accounts and regions for long-running CloudWatch alarms,
and sends a summary to Slack
Environment:
Variables:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
CLOUDWATCH_CROSS_ACCOUNT_SHARING_ROLE_NAME: !Ref CloudWatchCrossAccountSharingRoleName
SEARCH_REGIONS: !Join [",", !Ref AlarmReminderSearchRegions]
SEARCH_ACCOUNTS: !Join [",", !Ref AlarmReminderSearchAccountIds]
Events:
WeekdayPoller:
Properties:
Description: >-
Invokes the CloudWatch Alarm reminder function on weekdays
Enabled: true
Schedule: cron(0 1,13,15,17,19,21,23 ? * MON-FRI *)
Type: Schedule
WeekendPoller:
Properties:
Description: >-
Invokes the CloudWatch Alarm reminder function on weekends
Enabled: true
Schedule: cron(0 1,13,17,21 ? * SAT-SUN *)
Type: Schedule
Handler: index.handler
MemorySize: 192
Policies:
- Statement:
- Action: events:PutEvents
Effect: Allow
Resource: !Sub arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:event-bus/default
Version: "2012-10-17"
- Statement:
- Action: sts:AssumeRole
Effect: Allow
Resource: !Sub arn:aws:iam::*:role/${CloudWatchCrossAccountSharingRoleName}
Version: "2012-10-17"
Runtime: nodejs22.x
Tags:
prx:meta:tagging-version: "2021-04-07"
prx:cloudformation:stack-name: !Ref AWS::StackName
prx:cloudformation:stack-id: !Ref AWS::StackId
prx:ops:environment: Production
prx:dev:application: CloudWatch Toolkit
Timeout: 60
AlarmSlackRemindersLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${AlarmSlackRemindersFunction}
RetentionInDays: 14
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
AlarmSlackRemindersErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: "MINOR [CloudWatch] Alarm Slack Reminders <prod> EXPERIENCING ERRORS"
AlarmDescription: >-
Errors are occurring on the CloudWatch Alarm reminders Lambda function,
so there could be some long-running alarms that aren't generating
Slack messages.
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: FunctionName
Value: !Ref AlarmSlackRemindersFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
Threshold: 0
TreatMissingData: notBreaching
# Scans certain accounts and regions for all alarms, and sends a report
# message to Slack with information about any alarms that were active recently
AlarmSlackReportFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/alarm-slack-report
Description: >-
Scans a set of accounts and regions for active alarms,
and sends a summary to Slack
Environment:
Variables:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
CLOUDWATCH_CROSS_ACCOUNT_SHARING_ROLE_NAME: !Ref CloudWatchCrossAccountSharingRoleName
SEARCH_REGIONS: !Join [",", !Ref AlarmReminderSearchRegions]
SEARCH_ACCOUNTS: !Join [",", !Ref AlarmReminderSearchAccountIds]
Events:
WeekdayPoller:
Properties:
Description: >-
Invokes the CloudWatch Alarm report function
Enabled: true
Schedule: cron(10 13 ? * * *)
Type: Schedule
Handler: index.handler
MemorySize: 192
Policies:
- Statement:
- Action: events:PutEvents
Effect: Allow
Resource: !Sub arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:event-bus/default
Version: "2012-10-17"
- Statement:
- Action: sts:AssumeRole
Effect: Allow
Resource: !Sub arn:aws:iam::*:role/${CloudWatchCrossAccountSharingRoleName}
Version: "2012-10-17"
Runtime: nodejs22.x
Tags:
prx:meta:tagging-version: "2021-04-07"
prx:cloudformation:stack-name: !Ref AWS::StackName
prx:cloudformation:stack-id: !Ref AWS::StackId
prx:ops:environment: Production
prx:dev:application: CloudWatch Toolkit
Timeout: 120
AlarmSlackReportLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${AlarmSlackReportFunction}
RetentionInDays: 14
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
AlarmSlackReportErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: "MINOR [CloudWatch] Alarm Slack Report <prod> EXPERIENCING ERRORS"
AlarmDescription: >-
Errors are occurring on the CloudWatch Alarm report Lambda function,
so those reports may not be reaching Slack.
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: FunctionName
Value: !Ref AlarmSlackReportFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudWatch Toolkit }
Threshold: 0
TreatMissingData: notBreaching