SNS 中的订阅存在并且需要使用 1 个使用 cloudformation 的订阅

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

我有一个 cloudformation.yaml 文件,并且我使用以下脚本添加了超过 1 个订阅。 如果我提供所有 3 个邮件 ID,它会按预期工作,如果我只添加 1 或 2 个邮件 ID,则堆栈创建失败。我在这里有 2 个问题如下:

  1. 即使我只提供 1 个电子邮件 ID,如何使我的模板正常工作?

  2. 由于我只实现了电子邮件通知,我需要在创建堆栈时从 UI 中删除“订阅协议”参数,并将电子邮件设置为默认参数。

有人可以帮我输入吗?

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

您可以为每封电子邮件设置 Conditions 并有条件地创建您的订阅:

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