简体   繁体   中英

Create AWS SNS topic and reference it in an ASG using troposhere

I am trying to create a SNS topic and reference that topic in an ASG to get notifications when instances launches or terminates. Here is a section of troposphere code I am using:

    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',
                    ],
                ),
            ],
    ))

I have pulled out the ARN of the topic and given as a value to the "TopicARN" of NotificationConfigurations in an ASG but this code doesn't output a CF template. Am I missing anything here or Is there a better way to achieve this?

Appreciate your help in advance!

Thanks!

You have to call the to_json method of the template object. If you just wanted to print it out to the screen, for example, in your case you would:

print self.template.to_json()

Or call it on the created class itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM