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"