autoscaling

This commit is contained in:
dzikafoczka 2025-01-08 09:01:06 +01:00
parent eff581121c
commit 9db25d6764

View File

@ -264,76 +264,46 @@ def create_auto_scaling_group(autoscaling_client, launch_template_id, target_gro
print(f"Auto Scaling Group created with ARN: {asg_arn}")
return asg_arn
def put_scaling_policies(autoscaling_client, asg_arn):
# Put Scaling Policy Up
up = autoscaling_client.put_scaling_policy(
def put_scaling_policy(autoscaling_client, asg_arn):
# Put Scaling Policy based on CPU Utilization
policy = autoscaling_client.put_scaling_policy(
AutoScalingGroupName=f"{PREFIX}-asg",
PolicyName=f"{PREFIX}-scale-policy-up",
PolicyType='SimpleScaling',
AdjustmentType='ChangeInCapacity',
ScalingAdjustment=2, # Scale up faster by adding 2 instances at a time
Cooldown=30 # Short cooldown for rapid scaling up
PolicyName=f"{PREFIX}-scale-policy-cpu",
PolicyType='TargetTrackingScaling',
TargetTrackingConfiguration={
'PredefinedMetricSpecification': {
'PredefinedMetricType': 'ASGAverageCPUUtilization'
},
'TargetValue': 20.0, # Target CPU utilization percentage
'DisableScaleIn': False # Allow scale in
}
)
print(f"Scale-up policy created with ARN: {up['PolicyARN']}")
print(f"Scaling policy created with ARN: {policy['PolicyARN']}")
return policy['PolicyARN']
# Put Scaling Policy Down
down = autoscaling_client.put_scaling_policy(
AutoScalingGroupName=f"{PREFIX}-asg",
PolicyName=f"{PREFIX}-scale-policy-down",
PolicyType='SimpleScaling',
AdjustmentType='ChangeInCapacity',
ScalingAdjustment=-1, # Scale down slower by removing 1 instance at a time
Cooldown=120 # Longer cooldown for scaling down
)
print(f"Scale-down policy created with ARN: {down['PolicyARN']}")
return up['PolicyARN'], down['PolicyARN']
def create_cloudwatch_alarms(cloudwatch_client, sp_up, sp_down):
# Create CloudWatch Alarms for scaling up
def create_cloudwatch_alarm(cloudwatch_client, sp_arn):
# Create a CloudWatch Alarm for CPU Utilization
cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-up-alarm",
ComparisonOperator='GreaterThanThreshold',
AlarmName=f"{PREFIX}-cpu-utilization-alarm",
ComparisonOperator='GreaterThanOrEqualToThreshold',
EvaluationPeriods=1,
MetricName='RequestCountPerTarget',
Namespace='AWS/ApplicationELB',
Period=30, # Short evaluation period for quick scaling up
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Period=60,
Statistic='Average',
Threshold=50, # Lower threshold for scaling up faster
Threshold=20.0,
ActionsEnabled=True,
AlarmActions=[sp_up],
AlarmDescription='Scale-up alarm',
AlarmActions=[sp_arn],
AlarmDescription='Alarm to scale out/in based on CPU utilization',
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
}],
Unit='Count'
Unit='Percent'
)
print(f"Scale-up alarm created")
# Create CloudWatch Alarms for scaling down
cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-down-alarm",
ComparisonOperator='LessThanThreshold',
EvaluationPeriods=2, # Longer evaluation period for slower scaling down
MetricName='RequestCountPerTarget',
Namespace='AWS/ApplicationELB',
Period=120, # Longer period to prevent frequent scale-down actions
Statistic='Average',
Threshold=10, # Higher threshold to delay scaling down
ActionsEnabled=True,
AlarmActions=[sp_down],
AlarmDescription='Scale-down alarm',
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
}],
Unit='Count'
)
print(f"Scale-down alarm created")
print(f"CPU Utilization alarm created")
# Main function
def main():
@ -411,11 +381,11 @@ def main():
# Scaling Policies
print("Creating Scaling Policies...")
sp_up, sp_down = put_scaling_policies(autoscaling_client, asg_arn)
sp = put_scaling_policy(autoscaling_client, asg_arn)
# CloudWatch Alarms
print("Creating CloudWatch Alarms...")
create_cloudwatch_alarms(cloudwatch_client, sp_up, sp_down)
create_cloudwatch_alarm(cloudwatch_client, sp)
# Load Balancer DNS name
response = elbv2_client.describe_load_balancers(