forked from aws/aws-health-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dos-report-notifier.json
218 lines (218 loc) · 6.28 KB
/
dos-report-notifier.json
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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"SNSTopicName": {
"Type": "String",
"Description": "Please enter your SNS Topic Name. (SNS Topic must exist in the same region where this stack is launched)."
}
},
"Resources": {
"LambdaFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"Path": "/"
}
},
"LambdaRolePolicies": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": "LambdaPolicy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1477516473539",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": "arn:aws:logs:*:*:*"
},
{
"Sid": "Stmt1484080345748",
"Action": [
"sns:Publish"
],
"Effect": "Allow",
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:sns:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "SNSTopicName"
}
]
]
}
}
]
},
"Roles": [
{
"Ref": "LambdaFunctionRole"
}
]
}
},
"SNSPublishFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"LambdaFunctionRole",
"Arn"
]
},
"Environment": {
"Variables": {
"SNSARN": {
"Fn::Join": [
"",
[
"arn:aws:sns:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "SNSTopicName"
}
]
]
}
}
},
"Code": {
"ZipFile": {
"Fn::Join": [
"",
[
"// Sample Lambda Function to send notifications to a SNS topic when an AWS Health event happens\n",
"var AWS = require('aws-sdk');\n",
"var sns = new AWS.SNS();\n",
"\n",
"// define configuration\n",
"const snsTopic =process.env.SNSARN; //use ARN",
"\n",
"//main function which gets AWS Health data from Cloudwatch event\n",
"exports.handler = (event, context, callback) => {\n",
" //extract details from Cloudwatch event\n",
" healthMessage = event.detail.eventDescription[0].latestDescription + ' For more details, please see https://phd.aws.amazon.com/phd/home?region=us-east-1#/dashboard/open-issues';\n",
" eventName = event.detail.eventTypeCode\n",
" //prepare message for SNS to publish\n",
" var snsPublishParams = {\n",
" Message: healthMessage, \n",
" Subject: eventName,\n",
" TopicArn: snsTopic\n",
" };\n",
" sns.publish(snsPublishParams, function(err, data) {\n",
" if (err) {\n",
" const snsPublishErrorMessage = `Error publishing AWS Health event to SNS`;\n",
" console.log(snsPublishErrorMessage, err);\n",
" callback(snsPublishErrorMessage);\n",
" } \n",
" else {\n",
" const snsPublishSuccessMessage = `Successfully got details from AWS Health event, ${eventName} and published to SNS topic.`;\n",
" console.log(snsPublishSuccessMessage, data);\n",
" callback(null, snsPublishSuccessMessage); //return success\n",
" }\n",
" });\n",
"};"
]
]
}
},
"Runtime": "nodejs6.1",
"Timeout": "25"
}
},
"LambdaInvokePermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"FunctionName": {
"Fn::GetAtt": [
"SNSPublishFunction",
"Arn"
]
},
"Action": "lambda:InvokeFunction",
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"CloudWatchEventRule",
"Arn"
]
}
}
},
"CloudWatchEventRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "EventRule",
"EventPattern": {
"source": [
"aws.health"
],
"detail-type": [
"AWS Health Abuse Event"
],
"detail": {
"service": [
"ABUSE"
],
"eventTypeCategory": [
"issue"
],
"eventTypeCode": [
"AWS_ABUSE_DOS_REPORT"
]
}
},
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"SNSPublishFunction",
"Arn"
]
},
"Id": "SNSPublishFunction"
}
]
}
}
}
}