From 006c00fbf3b6a0005c182df37c9d7bf95bd041f8 Mon Sep 17 00:00:00 2001 From: Murat Ugur Eminoglu Date: Sat, 23 Sep 2023 20:03:47 +0300 Subject: [PATCH 1/4] Add AWS Agressive Autoscale Lambda Script --- aws-agressive-autoscale-lambda.py | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 aws-agressive-autoscale-lambda.py diff --git a/aws-agressive-autoscale-lambda.py b/aws-agressive-autoscale-lambda.py new file mode 100644 index 00000000..b78f0042 --- /dev/null +++ b/aws-agressive-autoscale-lambda.py @@ -0,0 +1,77 @@ +import boto3, os + + +def lambda_handler(event, context): + # Get the viewer_count and publisher_count from api gateway + viewer_count = event['params']['querystring']['viewer_count'] + publisher_count = event['params']['querystring']['publisher_count'] + + # Constants for instance limits + C5_XLARGE_EDGE_LIMIT = 150 + C5_4XLARGE_EDGE_LIMIT = C5_XLARGE_EDGE_LIMIT * 4 + C5_9XLARGE_EDGE_LIMIT = C5_XLARGE_EDGE_LIMIT * 7 + C5_XLARGE_ORIGIN_LIMIT = 40 + C5_4XLARGE_ORIGIN_LIMIT = C5_XLARGE_ORIGIN_LIMIT * 4 + C5_9XLARGE_ORIGIN_LIMIT = C5_XLARGE_ORIGIN_LIMIT * 9 + + # Initialize AWS clients (use the environment variables) + autoscaling_client = boto3.client('autoscaling') + ec2_client = boto3.client('ec2') + # Find Auto Scaling Group names with specific prefixes + asg_names = autoscaling_client.describe_auto_scaling_groups() + asg_edge_name = [group for group in asg_names['AutoScalingGroups'] if 'EdgeGroup' in group['AutoScalingGroupName']] + asg_origin_name = [group for group in asg_names['AutoScalingGroups'] if + 'OriginGroup' in group['AutoScalingGroupName']] + asg_edge_group_names = [group['AutoScalingGroupName'] for group in asg_edge_name][0] + asg_origin_group_names = [group['AutoScalingGroupName'] for group in asg_origin_name][0] + + print(asg_edge_name) + print(asg_edge_group_names) + + # Describe Auto Scaling Groups + edge_autoscaling_group = autoscaling_client.describe_auto_scaling_groups( + AutoScalingGroupNames=[asg_edge_group_names]) + origin_autoscaling_group = autoscaling_client.describe_auto_scaling_groups( + AutoScalingGroupNames=[asg_origin_group_names]) + + # Get instance types and current instance counts + edge_instance_type = edge_autoscaling_group['AutoScalingGroups'][0]['Instances'][0]['InstanceType'] + origin_instance_type = edge_autoscaling_group['AutoScalingGroups'][0]['Instances'][0]['InstanceType'] + edge_current_instance_count = len(edge_autoscaling_group['AutoScalingGroups'][0]['Instances']) + origin_current_instance_count = len(origin_autoscaling_group['AutoScalingGroups'][0]['Instances']) + + # Check and upgrade Auto Scaling Groups based on instance type + if edge_instance_type == "c5.xlarge": + edge_count = -(-viewer_count // C5_XLARGE_EDGE_LIMIT) + print(edge_count) + check_and_upgrade(edge_count, edge_current_instance_count, asg_edge_group_names) + if origin_instance_type == "c5.xlarge": + origin_count = -(-publisher_count // C5_XLARGE_ORIGIN_LIMIT) + print(origin_count) + check_and_upgrade(origin_count, origin_current_instance_count, asg_origin_group_names) + if edge_instance_type == "c5.4xlarge": + edge_count = -(-viewer_count // C5_4XLARGE_EDGE_LIMIT) + print(edge_count) + check_and_upgrade(edge_count, edge_current_instance_count, asg_edge_group_names) + if origin_instance_type == "c5.4xlarge": + origin_count = -(-publisher_count // C5_4XLARGE_ORIGIN_LIMIT) + print(origin_count) + check_and_upgrade(origin_count, origin_current_instance_count, asg_origin_group_names) + if edge_instance_type == "c5.9xlarge": + edge_count = -(-viewer_count // C5_9XLARGE_EDGE_LIMIT) + print(edge_count) + check_and_upgrade(edge_count, edge_current_instance_count, asg_edge_group_names) + if origin_instance_type == "c5.9xlarge": + origin_count = -(-publisher_count // C5_9XLARGE_ORIGIN_LIMIT) + print(origin_count) + check_and_upgrade(origin_count, origin_current_instance_count, asg_origin_group_names) + + +def check_and_upgrade(count, current_instance_count, asg_name): + autoscaling_client = boto3.client('autoscaling') + if count > current_instance_count: + response = autoscaling_client.update_auto_scaling_group( + AutoScalingGroupName=asg_name, + DesiredCapacity=count, + MinSize=count + ) From 1699bca43f3df5a555a3efa53ad177e40fa57a42 Mon Sep 17 00:00:00 2001 From: Murat Ugur Eminoglu Date: Tue, 26 Sep 2023 10:40:00 +0300 Subject: [PATCH 2/4] Add description --- aws-agressive-autoscale-lambda.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws-agressive-autoscale-lambda.py b/aws-agressive-autoscale-lambda.py index b78f0042..635935e6 100644 --- a/aws-agressive-autoscale-lambda.py +++ b/aws-agressive-autoscale-lambda.py @@ -1,5 +1,6 @@ -import boto3, os +# This Lambda function calculates the number of instances based on the number of Viewers and Publishers coming from the API and quickly increases the instance in Auto Scaling. +import boto3, os def lambda_handler(event, context): # Get the viewer_count and publisher_count from api gateway From 90118cdc5886d9d853467c70201f9ac0b49ac673 Mon Sep 17 00:00:00 2001 From: Murat Ugur Eminoglu Date: Thu, 26 Oct 2023 12:43:40 +0300 Subject: [PATCH 3/4] Fix wrong variable --- aws-agressive-autoscale-lambda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-agressive-autoscale-lambda.py b/aws-agressive-autoscale-lambda.py index 635935e6..4c5b4a76 100644 --- a/aws-agressive-autoscale-lambda.py +++ b/aws-agressive-autoscale-lambda.py @@ -37,7 +37,7 @@ def lambda_handler(event, context): # Get instance types and current instance counts edge_instance_type = edge_autoscaling_group['AutoScalingGroups'][0]['Instances'][0]['InstanceType'] - origin_instance_type = edge_autoscaling_group['AutoScalingGroups'][0]['Instances'][0]['InstanceType'] + origin_instance_type = origin_autoscaling_group['AutoScalingGroups'][0]['Instances'][0]['InstanceType'] edge_current_instance_count = len(edge_autoscaling_group['AutoScalingGroups'][0]['Instances']) origin_current_instance_count = len(origin_autoscaling_group['AutoScalingGroups'][0]['Instances']) From f9c569d249d572613ec409041bab83276eccb52a Mon Sep 17 00:00:00 2001 From: Murat Ugur Eminoglu Date: Thu, 1 Feb 2024 18:41:30 +0300 Subject: [PATCH 4/4] Update aws-agressive-autoscale-lambda.py --- aws-agressive-autoscale-lambda.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/aws-agressive-autoscale-lambda.py b/aws-agressive-autoscale-lambda.py index 4c5b4a76..b896a786 100644 --- a/aws-agressive-autoscale-lambda.py +++ b/aws-agressive-autoscale-lambda.py @@ -26,9 +26,6 @@ def lambda_handler(event, context): asg_edge_group_names = [group['AutoScalingGroupName'] for group in asg_edge_name][0] asg_origin_group_names = [group['AutoScalingGroupName'] for group in asg_origin_name][0] - print(asg_edge_name) - print(asg_edge_group_names) - # Describe Auto Scaling Groups edge_autoscaling_group = autoscaling_client.describe_auto_scaling_groups( AutoScalingGroupNames=[asg_edge_group_names])