Skip to content

Commit

Permalink
Updated With suggestions #11 (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
sssalim-aws committed Feb 4, 2017
1 parent 322160d commit eb32c4b
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 18 deletions.
126 changes: 126 additions & 0 deletions sms-notifier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,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: nodejs4.3

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
59 changes: 44 additions & 15 deletions sns-topic-publisher/cfn-templates/sns-topic-publisher.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Parameters": {
"SNSTopicName": {
"Type": "String",
"Description": "Please enter your SNS Topic Name"
"Description": "Please enter your SNS Topic Name. (SNS Topic must exist in the same region where this stack is launched)."
}
},
"Resources": {
Expand Down Expand Up @@ -52,7 +52,25 @@
"sns:Publish"
],
"Effect": "Allow",
"Resource": "*"
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:sns:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "SNSTopicName"
}
]
]
}
}
]
},
Expand All @@ -73,6 +91,29 @@
"Arn"
]
},
"Environment": {
"Variables": {
"SNSARN": {
"Fn::Join": [
"",
[
"arn:aws:sns:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "SNSTopicName"
}
]
]
}
}
},
"Code": {
"ZipFile": {
"Fn::Join": [
Expand All @@ -83,19 +124,7 @@
"var sns = new AWS.SNS();\n",
"\n",
"// define configuration\n",
"const snsTopic ='arn:aws:sns:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "SNSTopicName"
},
"'; //use ARN",
"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",
Expand Down
25 changes: 22 additions & 3 deletions sns-topic-publisher/cfn-templates/sns-topic-publisher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AWSTemplateFormatVersion: "2010-09-09"
Parameters:
SNSTopicName:
Type: String
Description: Please enter your SNS Topic Name
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"
Expand Down Expand Up @@ -38,7 +38,15 @@ Resources:
Action:
- sns:Publish
Effect: Allow
Resource: "*"
Resource:
Fn::Join:
- ""
- - "arn:aws:sns:"
- !Ref "AWS::Region"
- ":"
- !Ref "AWS::AccountId"
- ":"
- !Ref "SNSTopicName"
Roles:
-
Ref: "LambdaFunctionRole"
Expand All @@ -50,14 +58,25 @@ Resources:
Fn::GetAtt:
- "LambdaFunctionRole"
- "Arn"
Environment:
Variables:
SNSARN:
Fn::Join:
- ""
- - "arn:aws:sns:"
- !Ref "AWS::Region"
- ":"
- !Ref "AWS::AccountId"
- ":"
- !Ref "SNSTopicName"
Code:
ZipFile: !Sub |
// Sample Lambda Function to send notifications to a SNS topic when an AWS Health event happens
var AWS = require('aws-sdk');
var sns = new AWS.SNS();
// define configuration
const snsTopic ='arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${SNSTopicName}'; //use ARN
const snsTopic =process.env.SNSARN; //use ARN
//main function which gets AWS Health data from Cloudwatch event
exports.handler = (event, context, callback) => {
Expand Down

0 comments on commit eb32c4b

Please sign in to comment.