Fix bug with autoscaling in non-default region.
The bug appeared when using a region different than 'us-east-1' and caused KeyError when accessing launch_configurations. Rename the global autoscaling backend to reduce chances of similar naming issues.
This commit is contained in:
		
							parent
							
								
									d8dee720ab
								
							
						
					
					
						commit
						53a11c18c1
					
				@ -52,7 +52,7 @@ class FakeLaunchConfiguration(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        instance_profile_name = properties.get("IamInstanceProfile")
 | 
					        instance_profile_name = properties.get("IamInstanceProfile")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        config = autoscaling_backend.create_launch_configuration(
 | 
					        config = default_autoscaling_backend.create_launch_configuration(
 | 
				
			||||||
            name=resource_name,
 | 
					            name=resource_name,
 | 
				
			||||||
            image_id=properties.get("ImageId"),
 | 
					            image_id=properties.get("ImageId"),
 | 
				
			||||||
            key_name=properties.get("KeyName"),
 | 
					            key_name=properties.get("KeyName"),
 | 
				
			||||||
@ -107,12 +107,13 @@ class FakeAutoScalingGroup(object):
 | 
				
			|||||||
                 min_size, launch_config_name, vpc_zone_identifier,
 | 
					                 min_size, launch_config_name, vpc_zone_identifier,
 | 
				
			||||||
                 default_cooldown, health_check_period, health_check_type,
 | 
					                 default_cooldown, health_check_period, health_check_type,
 | 
				
			||||||
                 load_balancers, placement_group, termination_policies, autoscaling_backend):
 | 
					                 load_balancers, placement_group, termination_policies, autoscaling_backend):
 | 
				
			||||||
 | 
					        self.autoscaling_backend = autoscaling_backend
 | 
				
			||||||
        self.name = name
 | 
					        self.name = name
 | 
				
			||||||
        self.availability_zones = availability_zones
 | 
					        self.availability_zones = availability_zones
 | 
				
			||||||
        self.max_size = max_size
 | 
					        self.max_size = max_size
 | 
				
			||||||
        self.min_size = min_size
 | 
					        self.min_size = min_size
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.launch_config = autoscaling_backend.launch_configurations[launch_config_name]
 | 
					        self.launch_config = self.autoscaling_backend.launch_configurations[launch_config_name]
 | 
				
			||||||
        self.launch_config_name = launch_config_name
 | 
					        self.launch_config_name = launch_config_name
 | 
				
			||||||
        self.vpc_zone_identifier = vpc_zone_identifier
 | 
					        self.vpc_zone_identifier = vpc_zone_identifier
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -126,8 +127,6 @@ class FakeAutoScalingGroup(object):
 | 
				
			|||||||
        self.instances = []
 | 
					        self.instances = []
 | 
				
			||||||
        self.set_desired_capacity(desired_capacity)
 | 
					        self.set_desired_capacity(desired_capacity)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.autoscaling_backend = autoscaling_backend
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def create_from_cloudformation_json(cls, resource_name, cloudformation_json):
 | 
					    def create_from_cloudformation_json(cls, resource_name, cloudformation_json):
 | 
				
			||||||
        properties = cloudformation_json['Properties']
 | 
					        properties = cloudformation_json['Properties']
 | 
				
			||||||
@ -135,7 +134,7 @@ class FakeAutoScalingGroup(object):
 | 
				
			|||||||
        launch_config_name = properties.get("LaunchConfigurationName")
 | 
					        launch_config_name = properties.get("LaunchConfigurationName")
 | 
				
			||||||
        load_balancer_names = properties.get("LoadBalancerNames", [])
 | 
					        load_balancer_names = properties.get("LoadBalancerNames", [])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        group = autoscaling_backend.create_autoscaling_group(
 | 
					        group = default_autoscaling_backend.create_autoscaling_group(
 | 
				
			||||||
            name=resource_name,
 | 
					            name=resource_name,
 | 
				
			||||||
            availability_zones=properties.get("AvailabilityZones", []),
 | 
					            availability_zones=properties.get("AvailabilityZones", []),
 | 
				
			||||||
            desired_capacity=properties.get("DesiredCapacity"),
 | 
					            desired_capacity=properties.get("DesiredCapacity"),
 | 
				
			||||||
@ -164,7 +163,7 @@ class FakeAutoScalingGroup(object):
 | 
				
			|||||||
        self.max_size = max_size
 | 
					        self.max_size = max_size
 | 
				
			||||||
        self.min_size = min_size
 | 
					        self.min_size = min_size
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.launch_config = autoscaling_backend.launch_configurations[launch_config_name]
 | 
					        self.launch_config = self.autoscaling_backend.launch_configurations[launch_config_name]
 | 
				
			||||||
        self.launch_config_name = launch_config_name
 | 
					        self.launch_config_name = launch_config_name
 | 
				
			||||||
        self.vpc_zone_identifier = vpc_zone_identifier
 | 
					        self.vpc_zone_identifier = vpc_zone_identifier
 | 
				
			||||||
        self.health_check_period = health_check_period
 | 
					        self.health_check_period = health_check_period
 | 
				
			||||||
@ -186,7 +185,7 @@ class FakeAutoScalingGroup(object):
 | 
				
			|||||||
        if self.desired_capacity > curr_instance_count:
 | 
					        if self.desired_capacity > curr_instance_count:
 | 
				
			||||||
            # Need more instances
 | 
					            # Need more instances
 | 
				
			||||||
            count_needed = self.desired_capacity - curr_instance_count
 | 
					            count_needed = self.desired_capacity - curr_instance_count
 | 
				
			||||||
            reservation = autoscaling_backend.ec2_backend.add_instances(
 | 
					            reservation = self.autoscaling_backend.ec2_backend.add_instances(
 | 
				
			||||||
                self.launch_config.image_id,
 | 
					                self.launch_config.image_id,
 | 
				
			||||||
                count_needed,
 | 
					                count_needed,
 | 
				
			||||||
                self.launch_config.user_data,
 | 
					                self.launch_config.user_data,
 | 
				
			||||||
@ -200,7 +199,7 @@ class FakeAutoScalingGroup(object):
 | 
				
			|||||||
            count_to_remove = curr_instance_count - self.desired_capacity
 | 
					            count_to_remove = curr_instance_count - self.desired_capacity
 | 
				
			||||||
            instances_to_remove = self.instances[:count_to_remove]
 | 
					            instances_to_remove = self.instances[:count_to_remove]
 | 
				
			||||||
            instance_ids_to_remove = [instance.id for instance in instances_to_remove]
 | 
					            instance_ids_to_remove = [instance.id for instance in instances_to_remove]
 | 
				
			||||||
            autoscaling_backend.ec2_backend.terminate_instances(instance_ids_to_remove)
 | 
					            self.autoscaling_backend.ec2_backend.terminate_instances(instance_ids_to_remove)
 | 
				
			||||||
            self.instances = self.instances[count_to_remove:]
 | 
					            self.instances = self.instances[count_to_remove:]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -360,3 +359,5 @@ for region, ec2_backend in ec2_backends.items():
 | 
				
			|||||||
    autoscaling_backends[region] = AutoScalingBackend(ec2_backend)
 | 
					    autoscaling_backends[region] = AutoScalingBackend(ec2_backend)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
autoscaling_backend = autoscaling_backends['us-east-1']
 | 
					autoscaling_backend = autoscaling_backends['us-east-1']
 | 
				
			||||||
 | 
					default_autoscaling_backend = autoscaling_backend
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user