autoscaling

This commit is contained in:
dzikafoczka 2024-12-21 10:11:03 +01:00
parent 2c03bca76b
commit 23e1ff30a9

View File

@ -265,10 +265,10 @@ def create_auto_scaling_group(autoscaling_client, launch_template_id, target_gro
return asg_arn
def put_scaling_policies(autoscaling_client, asg_arn):
# Put Scaling Policies
up = autoscaling_client.put_scaling_policy(
# Put Scaling Policy
up_and_down = autoscaling_client.put_scaling_policy(
AutoScalingGroupName=f"{PREFIX}-asg",
PolicyName=f"{PREFIX}-scale-up",
PolicyName=f"{PREFIX}-scale-policy",
PolicyType='TargetTrackingScaling',
Cooldown=180,
TargetTrackingConfiguration={
@ -280,29 +280,13 @@ def put_scaling_policies(autoscaling_client, asg_arn):
}
)
print(f"Scale-up policy created with ARN: {up['PolicyARN']}")
print(f"Scale policy created with ARN: {up_and_down['PolicyARN']}")
return up_and_down['PolicyARN']
down = autoscaling_client.put_scaling_policy(
AutoScalingGroupName=f"{PREFIX}-asg",
PolicyName=f"{PREFIX}-scale-down",
PolicyType='TargetTrackingScaling',
Cooldown=180,
TargetTrackingConfiguration={
'PredefinedMetricSpecification': {
'PredefinedMetricType': 'ASGAverageCPUUtilization'
},
'TargetValue': 30.0,
'DisableScaleIn': False
}
)
print(f"Scale-down policy created with ARN: {down['PolicyARN']}")
return up['PolicyARN'], down['PolicyARN']
def create_cloudwatch_alarms(cloudwatch_client, asg_name, up_sp, down_sp):
def create_cloudwatch_alarms(cloudwatch_client, sp):
# Create CloudWatch Alarms
response = cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-up",
AlarmName=f"{PREFIX}-scale-alarm",
ComparisonOperator='GreaterThanThreshold',
EvaluationPeriods=1,
MetricName='CPUUtilization',
@ -311,21 +295,20 @@ def create_cloudwatch_alarms(cloudwatch_client, asg_name, up_sp, down_sp):
Statistic='Average',
Threshold=70.0,
ActionsEnabled=True,
AlarmActions=[up_sp],
AlarmDescription='Scale-up alarm',
Dimensions=[
{
AlarmActions=[sp],
AlarmDescription='Scale-up or scale-down alarm',
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
}
],
}],
Unit='Percent'
)
print(f"Scale-up alarm created with ARN: {response['AlarmArn']}")
print(f"Scale alarm created with ARN: {response['AlarmArn']}")
# Scale-down alarm
response = cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-down",
AlarmName=f"{PREFIX}-scale-down-alarm",
ComparisonOperator='LessThanThreshold',
EvaluationPeriods=1,
MetricName='CPUUtilization',
@ -334,14 +317,12 @@ def create_cloudwatch_alarms(cloudwatch_client, asg_name, up_sp, down_sp):
Statistic='Average',
Threshold=30.0,
ActionsEnabled=True,
AlarmActions=[down_sp],
AlarmActions=[sp],
AlarmDescription='Scale-down alarm',
Dimensions=[
{
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
}
],
}],
Unit='Percent'
)
@ -423,11 +404,11 @@ def main():
# Scaling Policies
print("Creating Scaling Policies...")
up_sp, down_sp = put_scaling_policies(autoscaling_client, asg_arn)
up_and_down = put_scaling_policies(autoscaling_client, asg_arn)
# CloudWatch Alarms
print("Creating CloudWatch Alarms...")
create_cloudwatch_alarms(cloudwatch_client, f"{PREFIX}-asg", up_sp, down_sp)
create_cloudwatch_alarms(cloudwatch_client, up_and_down)
# Load Balancer DNS name
response = elbv2_client.describe_load_balancers(