From 322160db4a5c0676f0b49c1ef66750f499c39244 Mon Sep 17 00:00:00 2001 From: sssalim Date: Fri, 3 Feb 2017 11:33:25 +1100 Subject: [PATCH 1/3] Added CloudFormation Templates for sns-topic-publisher --- .../cfn-templates/sns-topic-publisher.json | 175 ++++++++++++++++++ .../cfn-templates/sns-topic-publisher.yml | 113 +++++++++++ 2 files changed, 288 insertions(+) create mode 100644 sns-topic-publisher/cfn-templates/sns-topic-publisher.json create mode 100644 sns-topic-publisher/cfn-templates/sns-topic-publisher.yml diff --git a/sns-topic-publisher/cfn-templates/sns-topic-publisher.json b/sns-topic-publisher/cfn-templates/sns-topic-publisher.json new file mode 100644 index 0000000..20c15a9 --- /dev/null +++ b/sns-topic-publisher/cfn-templates/sns-topic-publisher.json @@ -0,0 +1,175 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Parameters": { + "SNSTopicName": { + "Type": "String", + "Description": "Please enter your SNS Topic Name" + } + }, + "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": "*" + } + ] + }, + "Roles": [ + { + "Ref": "LambdaFunctionRole" + } + ] + } + }, + "SNSPublishFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "LambdaFunctionRole", + "Arn" + ] + }, + "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 ='arn:aws:sns:", + { + "Ref": "AWS::Region" + }, + ":", + { + "Ref": "AWS::AccountId" + }, + ":", + { + "Ref": "SNSTopicName" + }, + "'; //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": "nodejs4.3", + "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" + ] + }, + "State": "ENABLED", + "Targets": [ + { + "Arn": { + "Fn::GetAtt": [ + "SNSPublishFunction", + "Arn" + ] + }, + "Id": "SNSPublishFunction" + } + ] + } + } + } +} \ No newline at end of file diff --git a/sns-topic-publisher/cfn-templates/sns-topic-publisher.yml b/sns-topic-publisher/cfn-templates/sns-topic-publisher.yml new file mode 100644 index 0000000..af32f47 --- /dev/null +++ b/sns-topic-publisher/cfn-templates/sns-topic-publisher.yml @@ -0,0 +1,113 @@ +AWSTemplateFormatVersion: "2010-09-09" +Parameters: + SNSTopicName: + Type: String + Description: Please enter your SNS Topic Name +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: "*" + Roles: + - + Ref: "LambdaFunctionRole" + SNSPublishFunction: + Type: "AWS::Lambda::Function" + Properties: + Handler: "index.handler" + Role: + Fn::GetAtt: + - "LambdaFunctionRole" + - "Arn" + 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 + + //main function which gets AWS Health data from Cloudwatch event + exports.handler = (event, context, callback) => { + //extract details from Cloudwatch event + 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'; + eventName = event.detail.eventTypeCode + //prepare message for SNS to publish + var snsPublishParams = { + Message: healthMessage, + Subject: eventName, + TopicArn: snsTopic + }; + sns.publish(snsPublishParams, function(err, data) { + if (err) { + const snsPublishErrorMessage = `Error publishing AWS Health event to SNS`; + console.log(snsPublishErrorMessage, err); + callback(snsPublishErrorMessage); + } + else { + const snsPublishSuccessMessage = `Successfully got details from AWS Health event, ${!eventName} and published to SNS topic.`; + console.log(snsPublishSuccessMessage, data); + callback(null, snsPublishSuccessMessage); //return success + } + }); + }; + Runtime: "nodejs4.3" + Timeout: "25" + LambdaInvokePermission: + Type: "AWS::Lambda::Permission" + Properties: + FunctionName: + Fn::GetAtt: + - "SNSPublishFunction" + - "Arn" + Action: "lambda:InvokeFunction" + Principal: "events.amazonaws.com" + SourceArn: + !GetAtt CloudWatchEventRule.Arn + CloudWatchEventRule: + Type: "AWS::Events::Rule" + Properties: + Description: "EventRule" + EventPattern: + source: + - "aws.health" + State: "ENABLED" + Targets: + - + Arn: + Fn::GetAtt: + - "SNSPublishFunction" + - "Arn" + Id: "SNSPublishFunction" From eb32c4b805f182e5971592247cae908b387668cf Mon Sep 17 00:00:00 2001 From: sssalim Date: Sat, 4 Feb 2017 19:45:59 +1100 Subject: [PATCH 2/3] Updated With suggestions https://github.com/aws/aws-health-tools/pull/11#pullrequestreview-20142947 --- sms-notifier.yml | 126 ++++++++++++++++++ .../cfn-templates/sns-topic-publisher.json | 59 +++++--- .../cfn-templates/sns-topic-publisher.yml | 25 +++- 3 files changed, 192 insertions(+), 18 deletions(-) create mode 100644 sms-notifier.yml diff --git a/sms-notifier.yml b/sms-notifier.yml new file mode 100644 index 0000000..4e3bd87 --- /dev/null +++ b/sms-notifier.yml @@ -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 diff --git a/sns-topic-publisher/cfn-templates/sns-topic-publisher.json b/sns-topic-publisher/cfn-templates/sns-topic-publisher.json index 20c15a9..73762f0 100644 --- a/sns-topic-publisher/cfn-templates/sns-topic-publisher.json +++ b/sns-topic-publisher/cfn-templates/sns-topic-publisher.json @@ -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": { @@ -52,7 +52,25 @@ "sns:Publish" ], "Effect": "Allow", - "Resource": "*" + "Resource": { + "Fn::Join": [ + "", + [ + "arn:aws:sns:", + { + "Ref": "AWS::Region" + }, + ":", + { + "Ref": "AWS::AccountId" + }, + ":", + { + "Ref": "SNSTopicName" + } + ] + ] + } } ] }, @@ -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": [ @@ -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", diff --git a/sns-topic-publisher/cfn-templates/sns-topic-publisher.yml b/sns-topic-publisher/cfn-templates/sns-topic-publisher.yml index af32f47..783488d 100644 --- a/sns-topic-publisher/cfn-templates/sns-topic-publisher.yml +++ b/sns-topic-publisher/cfn-templates/sns-topic-publisher.yml @@ -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" @@ -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" @@ -50,6 +58,17 @@ 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 @@ -57,7 +76,7 @@ Resources: 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) => { From d8faf933647e5cd94d86a0485466a6aa73b9b1e5 Mon Sep 17 00:00:00 2001 From: sssalim Date: Sun, 5 Feb 2017 00:37:04 +1100 Subject: [PATCH 3/3] removed sms-notifier.yml --- sms-notifier.yml | 126 ----------------------------------------------- 1 file changed, 126 deletions(-) delete mode 100644 sms-notifier.yml diff --git a/sms-notifier.yml b/sms-notifier.yml deleted file mode 100644 index 4e3bd87..0000000 --- a/sms-notifier.yml +++ /dev/null @@ -1,126 +0,0 @@ -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