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: !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 =process.env.SNSARN; //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: "nodejs14.x" 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"