1
0

autoscaling

This commit is contained in:
dzikafoczka 2025-01-07 20:08:05 +01:00
parent e4e7b98b3d
commit eff581121c

View File

@ -271,11 +271,11 @@ def put_scaling_policies(autoscaling_client, asg_arn):
PolicyName=f"{PREFIX}-scale-policy-up",
PolicyType='SimpleScaling',
AdjustmentType='ChangeInCapacity',
ScalingAdjustment=1,
Cooldown=60
ScalingAdjustment=2, # Scale up faster by adding 2 instances at a time
Cooldown=30 # Short cooldown for rapid scaling up
)
print(f"Scale policy created with ARN: {up['PolicyARN']}")
print(f"Scale-up policy created with ARN: {up['PolicyARN']}")
# Put Scaling Policy Down
down = autoscaling_client.put_scaling_policy(
@ -283,27 +283,27 @@ def put_scaling_policies(autoscaling_client, asg_arn):
PolicyName=f"{PREFIX}-scale-policy-down",
PolicyType='SimpleScaling',
AdjustmentType='ChangeInCapacity',
ScalingAdjustment=-1,
Cooldown=60
ScalingAdjustment=-1, # Scale down slower by removing 1 instance at a time
Cooldown=120 # Longer cooldown for scaling down
)
print(f"Scale policy created with ARN: {down['PolicyARN']}")
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
# Create CloudWatch Alarms for scaling up
cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-alarm",
AlarmName=f"{PREFIX}-scale-up-alarm",
ComparisonOperator='GreaterThanThreshold',
EvaluationPeriods=1,
MetricName='RequestCountPerTarget',
Namespace='AWS/ApplicationELB',
Period=60,
Statistic='Sum',
Threshold=50,
Period=30, # Short evaluation period for quick scaling up
Statistic='Average',
Threshold=50, # Lower threshold for scaling up faster
ActionsEnabled=True,
AlarmActions=[sp_up],
AlarmDescription='Scale-up',
AlarmDescription='Scale-up alarm',
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
@ -313,16 +313,16 @@ def create_cloudwatch_alarms(cloudwatch_client, sp_up, sp_down):
print(f"Scale-up alarm created")
# Scale-down alarm
# Create CloudWatch Alarms for scaling down
cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-down-alarm",
ComparisonOperator='LessThanThreshold',
EvaluationPeriods=1,
EvaluationPeriods=2, # Longer evaluation period for slower scaling down
MetricName='RequestCountPerTarget',
Namespace='AWS/ApplicationELB',
Period=60,
Period=120, # Longer period to prevent frequent scale-down actions
Statistic='Average',
Threshold=10,
Threshold=10, # Higher threshold to delay scaling down
ActionsEnabled=True,
AlarmActions=[sp_down],
AlarmDescription='Scale-down alarm',