From a29fb59a7224385e70239ffc007bb69beabfe268 Mon Sep 17 00:00:00 2001 From: Shawn Siefkas Date: Thu, 30 Jul 2015 09:52:32 -0500 Subject: [PATCH] Adding SNS notification support to ec2_asg module Addresses #1844 --- cloud/amazon/ec2_asg.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/cloud/amazon/ec2_asg.py b/cloud/amazon/ec2_asg.py index 614e3144c8c..d5476e7b0cc 100644 --- a/cloud/amazon/ec2_asg.py +++ b/cloud/amazon/ec2_asg.py @@ -130,6 +130,18 @@ options: default: Default choices: ['OldestInstance', 'NewestInstance', 'OldestLaunchConfiguration', 'ClosestToNextInstanceHour', 'Default'] version_added: "2.0" + notification_topic: + description: + - A SNS topic ARN to send auto scaling notifications to. + default: None + required: false + version_added: "2.0" + notification_types: + description: + - A list of auto scaling events to trigger notifications on. + default: ['autoscaling:EC2_INSTANCE_LAUNCH', 'autoscaling:EC2_INSTANCE_LAUNCH_ERROR', 'autoscaling:EC2_INSTANCE_TERMINATE', 'autoscaling:EC2_INSTANCE_TERMINATE_ERROR'] + required: false + version_added: "2.0" extends_documentation_fragment: - aws - ec2 @@ -392,6 +404,8 @@ def create_autoscaling_group(connection, module): as_groups = connection.get_all_groups(names=[group_name]) wait_timeout = module.params.get('wait_timeout') termination_policies = module.params.get('termination_policies') + notification_topic = module.params.get('notification_topic') + notification_types = module.params.get('notification_types') if not vpc_zone_identifier and not availability_zones: region, ec2_url, aws_connect_params = get_aws_connection_info(module) @@ -437,6 +451,10 @@ def create_autoscaling_group(connection, module): if wait_for_instances: wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances') wait_for_elb(connection, module, group_name) + + if notification_topic: + ag.put_notification_configuration(notification_topic, notification_types) + as_group = connection.get_all_groups(names=[group_name])[0] asg_properties = get_properties(as_group) changed = True @@ -500,6 +518,12 @@ def create_autoscaling_group(connection, module): except BotoServerError as e: module.fail_json(msg="Failed to update Autoscaling Group: %s" % str(e), exception=traceback.format_exc(e)) + if notification_topic: + try: + as_group.put_notification_configuration(notification_topic, notification_types) + except BotoServerError as e: + module.fail_json(msg="Failed to update Autoscaling Group notifications: %s" % str(e), exception=traceback.format_exc(e)) + if wait_for_instances: wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances') wait_for_elb(connection, module, group_name) @@ -513,6 +537,11 @@ def create_autoscaling_group(connection, module): def delete_autoscaling_group(connection, module): group_name = module.params.get('name') + notification_topic = module.params.get('notification_topic') + + if notification_topic: + ag.delete_notification_configuration(notification_topic) + groups = connection.get_all_groups(names=[group_name]) if groups: group = groups[0] @@ -800,7 +829,14 @@ def main(): health_check_type=dict(default='EC2', choices=['EC2', 'ELB']), default_cooldown=dict(type='int', default=300), wait_for_instances=dict(type='bool', default=True), - termination_policies=dict(type='list', default='Default') + termination_policies=dict(type='list', default='Default'), + notification_topic=dict(type='str', default=None), + notification_types=dict(type='list', default=[ + 'autoscaling:EC2_INSTANCE_LAUNCH', + 'autoscaling:EC2_INSTANCE_LAUNCH_ERROR', + 'autoscaling:EC2_INSTANCE_TERMINATE', + 'autoscaling:EC2_INSTANCE_TERMINATE_ERROR' + ]) ), )