def create_auto_scaling_resources(self, worker_security_group, worker_lb):
        worker_launch_config_name = 'lcWorker'

        worker_launch_config = self.add_resource(
            asg.LaunchConfiguration(
                worker_launch_config_name,
                EbsOptimized=True,
                ImageId=Ref(self.worker_ami),
                IamInstanceProfile=Ref(self.worker_instance_profile),
                InstanceType=Ref(self.worker_instance_type),
                KeyName=Ref(self.keyname),
                SecurityGroups=[Ref(worker_security_group)],
                UserData=Base64(Join('', self.get_cloud_config()))))

        worker_auto_scaling_group_name = 'asgWorker'

        worker_asg = self.add_resource(
            asg.AutoScalingGroup(
                worker_auto_scaling_group_name,
                AvailabilityZones=Ref(self.availability_zones),
                Cooldown=300,
                DesiredCapacity=Ref(self.worker_auto_scaling_desired),
                HealthCheckGracePeriod=600,
                HealthCheckType='ELB',
                LaunchConfigurationName=Ref(worker_launch_config),
                LoadBalancerNames=[Ref(worker_lb)],
                MaxSize=Ref(self.worker_auto_scaling_max),
                MinSize=Ref(self.worker_auto_scaling_min),
                NotificationConfigurations=[
                    asg.NotificationConfigurations(
                        TopicARN=Ref(self.notification_topic_arn),
                        NotificationTypes=[
                            asg.EC2_INSTANCE_LAUNCH,
                            asg.EC2_INSTANCE_LAUNCH_ERROR,
                            asg.EC2_INSTANCE_TERMINATE,
                            asg.EC2_INSTANCE_TERMINATE_ERROR
                        ])
                ],
                VPCZoneIdentifier=Ref(self.private_subnets),
                Tags=[asg.Tag('Name', 'Worker', True)]))

        self.add_resource(
            asg.ScheduledAction(
                'schedWorkerAutoScalingStart',
                AutoScalingGroupName=Ref(worker_asg),
                DesiredCapacity=Ref(
                    self.worker_auto_scaling_schedule_start_capacity),
                Recurrence=Ref(
                    self.worker_auto_scaling_schedule_start_recurrence)))

        self.add_resource(
            asg.ScheduledAction(
                'schedWorkerAutoScalingEnd',
                AutoScalingGroupName=Ref(worker_asg),
                DesiredCapacity=Ref(
                    self.worker_auto_scaling_schedule_end_capacity),
                Recurrence=Ref(
                    self.worker_auto_scaling_schedule_end_recurrence)))

        return worker_asg
Example #2
0
    def add_scheduled_action(self, name, autoscaling_group, min_size, max_size,
                             desired_size, recurrence):
        """
        Helper method creates a scheduled action for a given autoscaling group
        @param name [string] Unique name for the scheduled action
        @param name [AutoScalingGroup] Troposphere object containing the
        autoscaling group
        @param min_size [int] minimum size for the group
        @param max_size [int] maximum size for the group
        @param desired_size [int] the size resulting from this action
        @param min_size [string] cron string scehdule to trigger this action
        """

        return self.add_resource(
            autoscaling.ScheduledAction(
                name,
                AutoScalingGroupName=Ref(autoscaling_group),
                DesiredCapacity=desired_size,
                Recurrence=recurrence,
                MaxSize=max_size,
                MinSize=min_size,
            ))
    def create_auto_scaling_resources(self, app_server_security_group,
                                      app_server_lb,
                                      backward_compat_app_server_lb):
        self.add_condition('BlueCondition', Equals('Blue', Ref(self.color)))
        self.add_condition('GreenCondition', Equals('Green', Ref(self.color)))

        blue_app_server_launch_config = self.add_resource(
            asg.LaunchConfiguration(
                'lcAppServerBlue',
                Condition='BlueCondition',
                ImageId=Ref(self.app_server_ami),
                IamInstanceProfile=Ref(self.app_server_instance_profile),
                InstanceType=Ref(self.app_server_instance_type),
                KeyName=Ref(self.keyname),
                SecurityGroups=[Ref(app_server_security_group)],
                UserData=Base64(
                    Join(
                        '',
                        self.get_cloud_config(
                            self.blue_tile_distribution_endpoint)))))

        blue_app_server_asg = self.add_resource(
            asg.AutoScalingGroup(
                'asgAppServerBlue',
                AvailabilityZones=Ref(self.availability_zones),
                Condition='BlueCondition',
                Cooldown=300,
                DesiredCapacity=Ref(self.app_server_auto_scaling_desired),
                HealthCheckGracePeriod=600,
                HealthCheckType='ELB',
                LaunchConfigurationName=Ref(blue_app_server_launch_config),
                LoadBalancerNames=[
                    Ref(app_server_lb),
                    Ref(backward_compat_app_server_lb)
                ],
                MaxSize=Ref(self.app_server_auto_scaling_max),
                MinSize=Ref(self.app_server_auto_scaling_min),
                NotificationConfigurations=[
                    asg.NotificationConfigurations(
                        TopicARN=Ref(self.notification_topic_arn),
                        NotificationTypes=[
                            asg.EC2_INSTANCE_LAUNCH,
                            asg.EC2_INSTANCE_LAUNCH_ERROR,
                            asg.EC2_INSTANCE_TERMINATE,
                            asg.EC2_INSTANCE_TERMINATE_ERROR
                        ])
                ],
                VPCZoneIdentifier=Ref(self.private_subnets),
                Tags=[asg.Tag('Name', 'AppServer', True)]))

        self.add_resource(
            asg.ScheduledAction(
                'schedTileServerAutoScalingStartBlue',
                AutoScalingGroupName=Ref(blue_app_server_asg),
                Condition='BlueCondition',
                DesiredCapacity=Ref(
                    self.app_server_auto_scaling_schedule_start_capacity),
                Recurrence=Ref(
                    self.app_server_auto_scaling_schedule_start_recurrence)))

        self.add_resource(
            asg.ScheduledAction(
                'schedTileServerAutoScalingEndBlue',
                AutoScalingGroupName=Ref(blue_app_server_asg),
                Condition='BlueCondition',
                DesiredCapacity=Ref(
                    self.app_server_auto_scaling_schedule_end_capacity),
                Recurrence=Ref(
                    self.app_server_auto_scaling_schedule_end_recurrence)))

        green_app_server_launch_config = self.add_resource(
            asg.LaunchConfiguration(
                'lcAppServerGreen',
                Condition='GreenCondition',
                ImageId=Ref(self.app_server_ami),
                IamInstanceProfile=Ref(self.app_server_instance_profile),
                InstanceType=Ref(self.app_server_instance_type),
                KeyName=Ref(self.keyname),
                SecurityGroups=[Ref(app_server_security_group)],
                UserData=Base64(
                    Join(
                        '',
                        self.get_cloud_config(
                            self.green_tile_distribution_endpoint)))))

        green_app_server_asg = self.add_resource(
            asg.AutoScalingGroup(
                'asgAppServerGreen',
                AvailabilityZones=Ref(self.availability_zones),
                Condition='GreenCondition',
                Cooldown=300,
                DesiredCapacity=Ref(self.app_server_auto_scaling_desired),
                HealthCheckGracePeriod=600,
                HealthCheckType='ELB',
                LaunchConfigurationName=Ref(green_app_server_launch_config),
                LoadBalancerNames=[
                    Ref(app_server_lb),
                    Ref(backward_compat_app_server_lb)
                ],
                MaxSize=Ref(self.app_server_auto_scaling_max),
                MinSize=Ref(self.app_server_auto_scaling_min),
                NotificationConfigurations=[
                    asg.NotificationConfigurations(
                        TopicARN=Ref(self.notification_topic_arn),
                        NotificationTypes=[
                            asg.EC2_INSTANCE_LAUNCH,
                            asg.EC2_INSTANCE_LAUNCH_ERROR,
                            asg.EC2_INSTANCE_TERMINATE,
                            asg.EC2_INSTANCE_TERMINATE_ERROR
                        ])
                ],
                VPCZoneIdentifier=Ref(self.private_subnets),
                Tags=[asg.Tag('Name', 'AppServer', True)]))

        self.add_resource(
            asg.ScheduledAction(
                'schedTileServerAutoScalingStartGreen',
                AutoScalingGroupName=Ref(green_app_server_asg),
                Condition='GreenCondition',
                DesiredCapacity=Ref(
                    self.app_server_auto_scaling_schedule_start_capacity),
                Recurrence=Ref(
                    self.app_server_auto_scaling_schedule_start_recurrence)))

        self.add_resource(
            asg.ScheduledAction(
                'schedTileServerAutoScalingEndGreen',
                AutoScalingGroupName=Ref(green_app_server_asg),
                Condition='GreenCondition',
                DesiredCapacity=Ref(
                    self.app_server_auto_scaling_schedule_end_capacity),
                Recurrence=Ref(
                    self.app_server_auto_scaling_schedule_end_recurrence)))