Skip to content

Commit

Permalink
Add instance scale-in protection
Browse files Browse the repository at this point in the history
  • Loading branch information
braydencw1 committed Jul 31, 2024
1 parent 2142ee2 commit c88dd95
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions plugins/modules/autoscaling_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@
- List of VPC subnets to use
type: list
elements: str
protected_from_scale_in:
description:
- If V(true), instances will have scale-in protection enabled.
type: bool
default: false
tags:
description:
- A list of tags to add to the Auto Scale Group.
Expand Down Expand Up @@ -1128,6 +1133,7 @@ def create_autoscaling_group(connection):
vpc_zone_identifier = module.params.get("vpc_zone_identifier")
set_tags = module.params.get("tags")
purge_tags = module.params.get("purge_tags")
protected_from_scale_in = module.params.get("protected_from_scale_in")
health_check_period = module.params.get("health_check_period")
health_check_type = module.params.get("health_check_type")
default_cooldown = module.params.get("default_cooldown")
Expand Down Expand Up @@ -1186,6 +1192,7 @@ def create_autoscaling_group(connection):
HealthCheckType=health_check_type,
DefaultCooldown=default_cooldown,
TerminationPolicies=termination_policies,
NewInstancesProtectedFromScaleIn=protected_from_scale_in,
)
if vpc_zone_identifier:
ag["VPCZoneIdentifier"] = vpc_zone_identifier
Expand Down Expand Up @@ -1483,14 +1490,15 @@ def get_chunks(l, n):
yield l[i:i + n] # fmt: skip


def update_size(connection, group, max_size, min_size, dc):
def update_size(connection, group, max_size, min_size, dc, protected_from_scale_in):
module.debug("setting ASG sizes")
module.debug(f"minimum size: {min_size}, desired_capacity: {dc}, max size: {max_size}")
updated_group = dict()
updated_group["AutoScalingGroupName"] = group["AutoScalingGroupName"]
updated_group["MinSize"] = min_size
updated_group["MaxSize"] = max_size
updated_group["DesiredCapacity"] = dc
updated_group["NewInstancesProtectedFromScaleIn"] = protected_from_scale_in
update_asg(connection, **updated_group)


Expand All @@ -1501,6 +1509,7 @@ def replace(connection):
group_name = module.params.get("name")
max_size = module.params.get("max_size")
min_size = module.params.get("min_size")
protected_from_scale_in = module.params.get("protected_from_scale_in")
desired_capacity = module.params.get("desired_capacity")
launch_config_name = module.params.get("launch_config_name")

Expand Down Expand Up @@ -1570,7 +1579,7 @@ def replace(connection):
# This should get overwritten if the number of instances left is less than the batch size.

as_group = describe_autoscaling_groups(connection, group_name)[0]
update_size(connection, as_group, max_size + batch_size, min_size + batch_size, desired_capacity + batch_size)
update_size(connection, as_group, max_size + batch_size, min_size + batch_size, desired_capacity + batch_size, protected_from_scale_in)

if wait_for_instances:
wait_for_new_inst(connection, group_name, wait_timeout, as_group["MinSize"] + batch_size, "viable_instances")
Expand Down Expand Up @@ -1598,7 +1607,7 @@ def replace(connection):
module.debug("breaking loop")
break

update_size(connection, as_group, max_size, min_size, desired_capacity)
update_size(connection, as_group, max_size, min_size, desired_capacity, protected_from_scale_in)
as_group = describe_autoscaling_groups(connection, group_name)[0]
asg_properties = get_properties(as_group)
module.debug("Rolling update complete.")
Expand Down Expand Up @@ -1902,6 +1911,7 @@ def main():
state=dict(default="present", choices=["present", "absent"]),
tags=dict(type="list", default=[], elements="dict"),
purge_tags=dict(type="bool", default=False),
protected_from_scale_in=dict(type="bool", default=False),
health_check_period=dict(type="int", default=300),
health_check_type=dict(default="EC2", choices=["EC2", "ELB"]),
default_cooldown=dict(type="int", default=300),
Expand Down

0 comments on commit c88dd95

Please sign in to comment.