1

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
Kivi
  • 485
  • 1
  • 9
  • 26

1 Answers1

0

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
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Thanks Marcin, everything works fine except one thing. If i did not give any mail id for subscription also the stack creation is success. I want to make 1 mail id as a mandatory one. Could you please confirm for this how it could be done ? – Kivi Jul 06 '21 at 13:09
  • @Kivi You can remove one default value and one the corresponding condition. If it works, accepting the answer would be appreciated. – Marcin Jul 06 '21 at 21:46