forked from aws/aws-health-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms-notifier.yml
126 lines (110 loc) · 3.79 KB
/
sms-notifier.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
Description: >
This template sets up AWS Health Tool to send custom text or SMS notifications via Amazon SNS when an AWS Health event happens by using AWS Lambda and Amazon CloudWatch Events.
Parameters:
PhoneNumber:
Type: String
Default: +1XXX5550100
Description: The phone number to send notifications to.
Metadata:
AWS::CloudFormation::Interface:
ParameterLabels:
PhoneNumber:
default: "Phone number"
ParameterGroups:
- Label:
default: AWS Health Tool Configuration
Parameters:
- PhoneNumber
Resources:
LambdaExecutionRole:
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: sms-notifier
PolicyDocument:
Version: '2012-10-17'
Statement:
-
Effect: Allow
Action: sns:Publish
Resource: '*'
-
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
Roles:
-
Ref: LambdaExecutionRole
SmsNotifierFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Environment:
Variables:
PHONE_NUMBER: !Ref PhoneNumber
Code:
ZipFile: >
// Sample Lambda Function to send notifications via text when an AWS Health event happens
'use strict';
let AWS = require('aws-sdk');
let sns = new AWS.SNS();
//main function which gets AWS Health data from Cloudwatch event
exports.handler = (event, context, callback) => {
//get phone number from Env Variable
let phoneNumber = process.env.PHONE_NUMBER;
//extract details from Cloudwatch event
let eventName = event.detail.eventTypeCode
let healthMessage = `The following AWS Health event type has occured: ${eventName} For more details, please see https://phd.aws.amazon.com/phd/home?region=us-east-1#/dashboard/open-issues`;
//prepare message for SNS to publish
let snsPublishParams = {
Message: healthMessage,
PhoneNumber: phoneNumber,
};
sns.publish(snsPublishParams,(err,data) => {
if (err) {
const snsPublishErrorMessage = `Error publishing AWS Health event to SNS`;
console.log(snsPublishErrorMessage, err, err.stack); // adding the err.stack
callback(snsPublishErrorMessage);
}
const snsPublishSuccessMessage = `Successfully got details from AWS Health event, ${eventName} and sent SMS via SNS.`;
console.log(snsPublishSuccessMessage, data);
callback(null, snsPublishSuccessMessage); //return success
});
};
Runtime: nodejs12.x
AwsHealthEventRule:
Type: AWS::Events::Rule
Properties:
Description: AWSHealthEventRule
EventPattern:
source:
- aws.health
State: ENABLED
Targets:
-
Arn: !GetAtt SmsNotifierFunction.Arn
Id: SmsNotifierLambdaFunction
PermissionForEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref SmsNotifierFunction
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt AwsHealthEventRule.Arn