From 53a11c18c1c17197c27eeda6f97737ee6310f3f5 Mon Sep 17 00:00:00 2001 From: Marco Rucci Date: Fri, 7 Nov 2014 22:13:00 +0100 Subject: [PATCH] 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. --- moto/autoscaling/models.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index 92789d3dc..1e2cf689e 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -52,7 +52,7 @@ class FakeLaunchConfiguration(object): instance_profile_name = properties.get("IamInstanceProfile") - config = autoscaling_backend.create_launch_configuration( + config = default_autoscaling_backend.create_launch_configuration( name=resource_name, image_id=properties.get("ImageId"), key_name=properties.get("KeyName"), @@ -107,12 +107,13 @@ class FakeAutoScalingGroup(object): min_size, launch_config_name, vpc_zone_identifier, default_cooldown, health_check_period, health_check_type, load_balancers, placement_group, termination_policies, autoscaling_backend): + self.autoscaling_backend = autoscaling_backend self.name = name self.availability_zones = availability_zones self.max_size = max_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.vpc_zone_identifier = vpc_zone_identifier @@ -126,8 +127,6 @@ class FakeAutoScalingGroup(object): self.instances = [] self.set_desired_capacity(desired_capacity) - self.autoscaling_backend = autoscaling_backend - @classmethod def create_from_cloudformation_json(cls, resource_name, cloudformation_json): properties = cloudformation_json['Properties'] @@ -135,7 +134,7 @@ class FakeAutoScalingGroup(object): launch_config_name = properties.get("LaunchConfigurationName") load_balancer_names = properties.get("LoadBalancerNames", []) - group = autoscaling_backend.create_autoscaling_group( + group = default_autoscaling_backend.create_autoscaling_group( name=resource_name, availability_zones=properties.get("AvailabilityZones", []), desired_capacity=properties.get("DesiredCapacity"), @@ -164,7 +163,7 @@ class FakeAutoScalingGroup(object): self.max_size = max_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.vpc_zone_identifier = vpc_zone_identifier self.health_check_period = health_check_period @@ -186,7 +185,7 @@ class FakeAutoScalingGroup(object): if self.desired_capacity > curr_instance_count: # Need more instances 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, count_needed, self.launch_config.user_data, @@ -200,7 +199,7 @@ class FakeAutoScalingGroup(object): count_to_remove = curr_instance_count - self.desired_capacity instances_to_remove = self.instances[:count_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:] @@ -360,3 +359,5 @@ for region, ec2_backend in ec2_backends.items(): autoscaling_backends[region] = AutoScalingBackend(ec2_backend) autoscaling_backend = autoscaling_backends['us-east-1'] +default_autoscaling_backend = autoscaling_backend +