创建AWS SNS主题并使用troposhere在ASG中引用它

时间:2017-08-07 05:33:10

标签: python-2.7 amazon-web-services amazon-cloudformation troposphere

我正在尝试创建一个SNS主题,并在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模板。我在这里遗漏了什么,或者有更好的方法来实现这个目标吗?

提前感谢您的帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须调用模板对象的print self.template.to_json() 方法。如果您只想将其打印到屏幕上,例如,在您的情况下,您将:

{{1}}

或者在创建的类本身上调用它。