Private
Private

Reputation: 1771

SNS Topic is not sending the pipeline event change emails when configured through Cloudformation

I am creating a cloudwatch event which will trigger when there is state change in my code pipeline and assigned to a SNS topic. I am doing all the above configuration through Cloudformation Stack. Below is my stack template

{
    "Description": "Triggering CodePipeline notifications.",
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "EmailAddress": {
            "Description": "Email Address",
            "Type": "String"
        }
    },
    "Resources": {
        "EventRule": {
            "Type": "AWS::Events::Rule",
            "Properties": {
                "Description": "EventRule",
                "EventPattern": {
                    "source": [
                        "aws.codepipeline"
                    ],
                    "detail-type": [
                        "CodePipeline Pipeline Execution State Change"
                    ],
                    "detail": {
                        "state": [
                            "FAILED"
                        ],
                        "pipeline": [
                           "mypipelinename"
                        ]
                    }
                },
                "State": "ENABLED",
                "Targets": [
                    {
                        "Arn": {
                            "Ref": "MySNSTopic"
                        },
                        "Id": "PipelineNotificationTopic",
                        "InputTransformer": {
                            "InputTemplate": "\"The Pipeline <pipeline> has failed.\" ",
                            "InputPathsMap": {
                                "pipeline": "$.detail.pipeline"
                            }
                        }
                    }
                ]
            }
        },
        "MySNSTopic": {
            "Type": "AWS::SNS::Topic",
            "Properties": {
             "TopicName": "Huaaa",
                "Subscription": [
                    {
                        "Endpoint": {
                            "Ref": "EmailAddress"
                        },
                        "Protocol": "email"
                    }
                ]
            }
        }
    }
}

This is creating all the necessary resources like cloudwatch event and SNS subscription and sending a notification for my email address for confirmation. But when there is a state change in mypipeline this is not triggering any email notification.

But when i created a new SNSTopic and created a subscription using aws console and attached it to the cloudwatch event that was created by cloudformation stack as a target it worked fine. I can able to receive notifications when there is a change.

Am i missing anything in my Template?

Thanks Any help is apprecicated

Upvotes: 2

Views: 1826

Answers (1)

MaiKaY
MaiKaY

Reputation: 4482

You are missing to set the AWS::SNS::TopicPolicy which allows to publish.

For example (written in .yml)

MySNSTopicPolicy:
  Type: AWS::SNS::TopicPolicy
  Properties:
    Topics:
      - !Ref MySNSTopic
    PolicyDocument:
      Version: 2012-10-17
      Statement:
        - Effect: Allow
          Principal:
            AWS: '*'
          Action: sns:Publish
          Resource: !Ref MySNSTopic

Upvotes: 3

Related Questions