Search code examples
amazon-web-servicesnotificationsaws-cloudformationsubscription

Subscription in SNS is present and need to use 1 subscription using cloudformation


I have a cloudformation.yaml file and I have added more than 1 subscription using the below script. If I provide all the 3 mail id's it is working as expected and if I add only 1 or 2 mail id's then stack creation is failing. I have 2 questions here as follows:

  1. How to make my template work without issue even though if I provide 1 email id only ?

  2. Since I have implemented only for email notification, I need to remove "Subscription protocol" parameter from UI while creation of stack and let the email is set as a default parameter.

Could someone help me with inputs in this pls ?

AWSTemplateFormatVersion: '2010-09-09'
Description: Creates SNS topic, SNS subscription and Cloudwatch rule for Codebuild Notification
Parameters:
  EmailID1:
    Type: String
    Description: Enter Email ID to receive notifications.
  EmailID2:
    Type: String
    Description: Enter Email ID to receive notifications.   
  EmailID3:
    Type: String
    Description: Enter Email ID to receive notifications.
  SubscriptionProtocol:
    Type: String
    Description: The subscription protocol to send notification (Ex: email)
    AllowedValues:
    - email
    Default: email
Mappings: {}
Conditions: {}
Resources:
  SNSTopicCodebuildFailNotify:
    Type: AWS::SNS::Topic
    Properties: {}
  SNSSubscription1:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol:
        Ref: SubscriptionProtocol
      Endpoint:
        Ref: EmailID1      
      TopicArn:
        Ref: SNSTopicCodebuildFailNotify
  SNSSubscription2:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol:
        Ref: SubscriptionProtocol
      Endpoint:
        Ref: EmailID2      
      TopicArn:
        Ref: SNSTopicCodebuildFailNotify      
  SNSSubscription3:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol:
        Ref: SubscriptionProtocol
      Endpoint:
        Ref: EmailID3     
      TopicArn:
        Ref: SNSTopicCodebuildFailNotify

Solution

  • You can setup Conditions for each email and conditionally create your subscriptions:

    AWSTemplateFormatVersion: '2010-09-09'
    Description: Creates SNS topic, SNS subscription and Cloudwatch rule for Codebuild Notification
    Parameters:
      EmailID1:
        Type: String
        Default: ""
        Description: Enter Email ID to receive notifications.
      EmailID2:
        Type: String
        Default: ""
        Description: Enter Email ID to receive notifications.
      EmailID3:
        Type: String
        Default: ""
        Description: Enter Email ID to receive notifications.
    
    Conditions:
    
        HasEmail1:
            !Not [!Equals [!Ref EmailID1, ""]]
    
        HasEmail2:
            !Not [!Equals [!Ref EmailID2, ""]]
    
        HasEmail3:
            !Not [!Equals [!Ref EmailID3, ""]]
    
    Resources:
    
      SNSTopicCodebuildFailNotify:
        Type: AWS::SNS::Topic
        Properties: {}
    
      SNSSubscription1:
        Type: AWS::SNS::Subscription
        Condition: HasEmail1
        Properties:
          Protocol: email
          Endpoint:
            Ref: EmailID1
          TopicArn:
            Ref: SNSTopicCodebuildFailNotify
    
      SNSSubscription2:
        Type: AWS::SNS::Subscription
        Condition: HasEmail2
        Properties:
          Protocol: email
          Endpoint:
            Ref: EmailID2
          TopicArn:
            Ref: SNSTopicCodebuildFailNotify
    
      SNSSubscription3:
        Type: AWS::SNS::Subscription
        Condition: HasEmail3
        Properties:
          Protocol: email
          Endpoint:
            Ref: EmailID3
          TopicArn:
            Ref: SNSTopicCodebuildFailNotify