簡體   English   中英

CloudFormation:有條件的 AutoScalingGroup 通知

[英]CloudFormation: conditional AutoScalingGroup notifications

我想使用 SNS 接收 AutoScaling 事件通知,但僅限於我的 PROD 環境。 如何配置我的 CloudFormation 模板來執行此操作?

應該是這樣的:

Parameters:
  Environment:
    Description: Environment of the application
    Type: String
    Default: dev
    AllowedValues:
      - dev
      - prod

Conditions:
  IsDev: !Equals [ !Ref Environment, dev]
  IsProd: !Equals [ !Ref Environment, prod]

Resources:
  mySNSTopic:
    Type: AWS::SNS::Topic
    Properties: 
      Subscription: 
        - Endpoint: "my@email.com"
          Protocol: "email"

  myProdAutoScalingGroupWithNotifications:
    Type: AWS::AutoScaling::AutoScalingGroup
    Condition: IsProd
    Properties:
      NotificationConfigurations:
        - NotificationTypes: 
            - "autoscaling:EC2_INSTANCE_LAUNCH_ERROR"
            - "autoscaling:EC2_INSTANCE_TERMINATE"
            - "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
          TopicARN: !Ref "mySNSTopic"

  myDevAutoScalingGroupWithoutNotifications:
    Type: AWS::AutoScaling::AutoScalingGroup
    Condition: IsDev
    Properties:

或者 CloudFormation 是否也支持以下內容:

Parameters:
  Environment:
    Description: Environment of the application
    Type: String
    Default: dev
    AllowedValues:
      - dev
      - prod

Conditions:
  IsProd: !Equals [ !Ref Environment, prod]

Resources:
  mySNSTopic:
    Type: AWS::SNS::Topic
    Properties: 
      Subscription: 
        - Endpoint: "my@email.com"
          Protocol: "email"

  myAutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      NotificationConfigurations:
        - Condition: IsProd
          NotificationTypes: 
            - "autoscaling:EC2_INSTANCE_LAUNCH_ERROR"
            - "autoscaling:EC2_INSTANCE_TERMINATE"
            - "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
          TopicARN: !Ref "mySNSTopic"

使用Fn::If function 應該是雙倍的:

  NotificationConfigurations:
    - !If 
        - IsProd
        - NotificationTypes: 
            - "autoscaling:EC2_INSTANCE_LAUNCH_ERROR"
            - "autoscaling:EC2_INSTANCE_TERMINATE"
            - "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
          TopicARN: !Ref "mySNSTopic"          
        - !Ref "AWS::NoValue" 

也可以試試下面的形式:

  NotificationConfigurations:
    !If
      - IsProd
      - - NotificationTypes: 
            - "autoscaling:EC2_INSTANCE_LAUNCH_ERROR"
            - "autoscaling:EC2_INSTANCE_TERMINATE"
            - "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
          TopicARN: !Ref "mySNSTopic"          
      - !Ref "AWS::NoValue"  

請注意壓痕。 您可能需要調整它以匹配您的模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM