0

我試圖創建一個SNS主題並在ASG中引用該主題以在實例啓動或終止時獲取通知。這裏是對流層代碼段我使用:創建AWS SNS主題並使用troposhere在ASG中引用

email_topic = self.template.add_resource(Topic(
     'ec2_lauch_termination', 
     Subscription=[ 
      Subscription(
       Endpoint=Ref(alarm_email), 
       Protocol="email" 
      ), 
     ], 
    )) 

    self.template.add_output(Output(
      "TopicArn", 
      Value=GetAtt(email_topic, "Arn"), 
      Description="ARN of email topic", 
    )) 

    for i in range(0, self.subnet_count): 
      asg_name = "autoScalingGroup" + str(i) 
      asg = self.template.add_resource(AutoScalingGroup(
       asg_name, 
       DesiredCapacity=Ref(self.desired_capacity), 
       HealthCheckType="EC2", 
       LaunchConfigurationName=Ref(launch_config), 
       MinSize=Ref(self.min_size), 
       MaxSize=Ref(self.max_size), 
       VPCZoneIdentifier=[Select(i, Ref(self.instance_subnets))], 
       Tags=[ 
        Tag("Name", Join("-", [Ref(self.resource_name), Ref(self.env_tag), Ref(self.vpc_short_name), "pdx"]), True), 
        Tag("Name", "XXXX", True), 
        Tag("Service", Ref(self.service_tag), True), 
        Tag("Environment", Ref(self.env_tag), True), 
        Tag("Address", Ref(self.address_tag), True) 
       ], 

     NotificationConfigurations=[ 
      NotificationConfigurations(
       TopicARN=GetAtt(email_topic, "Arn"), 
       NotificationTypes=[ 
        'autoscaling:EC2_INSTANCE_LAUNCH', 
        'autoscaling:EC2_INSTANCE_LAUNCH_ERROR', 
        'autoscaling:EC2_INSTANCE_TERMINATE', 
        'autoscaling:EC2_INSTANCE_TERMINATE_ERROR', 
        ], 
       ), 
      ], 
    )) 

我已經退出了該主題的ARN和數值到在ASG NotificationConfigurations的「TopicARN」賦予但是這個代碼不輸出CF模板。我在這裏錯過了什麼或有更好的方法來實現這一目標嗎?

提前感謝您的幫助!

謝謝!

回答

0

您必須調用模板對象的to_json方法。如果你只是想打印出來的畫面,例如,你的情況,你會:

print self.template.to_json() 

或者調用它創建的類本身。