autoscaling

This commit is contained in:
dzikafoczka 2024-12-21 11:29:04 +01:00
parent d1c017c086
commit 0fa568fbe1

View File

@ -244,8 +244,8 @@ def create_auto_scaling_group(autoscaling_client, launch_template_id, target_gro
DesiredCapacity=2,
TargetGroupARNs=[target_group_arn],
AvailabilityZones=[f"{REGION}a", f"{REGION}b"],
HealthCheckType='ELB',
HealthCheckGracePeriod=300,
HealthCheckType='EC2',
HealthCheckGracePeriod=90,
VPCZoneIdentifier=f"{subnet_id_1},{subnet_id_2}",
Tags=[
{
@ -265,65 +265,72 @@ 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 Policy
up_and_down = autoscaling_client.put_scaling_policy(
# Put Scaling Policy Up
up = autoscaling_client.put_scaling_policy(
AutoScalingGroupName=f"{PREFIX}-asg",
PolicyName=f"{PREFIX}-scale-policy",
PolicyType='TargetTrackingScaling',
Cooldown=180,
TargetTrackingConfiguration={
'PredefinedMetricSpecification': {
'PredefinedMetricType': 'ASGAverageCPUUtilization'
},
'TargetValue': 70.0,
'DisableScaleIn': False
}
PolicyName=f"{PREFIX}-scale-policy-up",
PolicyType='SimpleScaling',
AdjustmentType='ChangeInCapacity',
ScalingAdjustment=1,
Cooldown=90
)
print(f"Scale policy created with ARN: {up_and_down['PolicyARN']}")
return up_and_down['PolicyARN']
print(f"Scale policy created with ARN: {up['PolicyARN']}")
def create_cloudwatch_alarms(cloudwatch_client, sp):
# 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,
Cooldown=90
)
print(f"Scale policy created with ARN: {down['PolicyARN']}")
return up['PolicyARN'], down['PolicyARN']
def create_cloudwatch_alarms(cloudwatch_client, sp_up, sp_down):
# Create CloudWatch Alarms
response = cloudwatch_client.put_metric_alarm(
cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-alarm",
ComparisonOperator='GreaterThanThreshold',
EvaluationPeriods=1,
MetricName='CPUUtilization',
Namespace='AWS/EC2',
MetricName='RequestCount',
Namespace='AWS/ApplicationELB',
Period=60,
Statistic='Average',
Threshold=70.0,
Statistic='Sum',
Threshold=50,
ActionsEnabled=True,
AlarmActions=[sp],
AlarmDescription='Scale-up or scale-down alarm',
AlarmActions=[sp_up],
AlarmDescription='Scale-up',
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
}],
Unit='Percent'
Unit='Count'
)
print(f"Scale-up alarm created")
# Scale-down alarm
response = cloudwatch_client.put_metric_alarm(
cloudwatch_client.put_metric_alarm(
AlarmName=f"{PREFIX}-scale-down-alarm",
ComparisonOperator='LessThanThreshold',
EvaluationPeriods=1,
MetricName='CPUUtilization',
Namespace='AWS/EC2',
MetricName='RequestCount',
Namespace='AWS/ApplicationELB',
Period=60,
Statistic='Average',
Threshold=30.0,
Threshold=10,
ActionsEnabled=True,
AlarmActions=[sp],
AlarmActions=[sp_down],
AlarmDescription='Scale-down alarm',
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
}],
Unit='Percent'
Unit='Count'
)
print(f"Scale-down alarm created")
@ -404,11 +411,11 @@ def main():
# Scaling Policies
print("Creating Scaling Policies...")
up_and_down = put_scaling_policies(autoscaling_client, asg_arn)
sp_up, sp_down = put_scaling_policies(autoscaling_client, asg_arn)
# CloudWatch Alarms
print("Creating CloudWatch Alarms...")
create_cloudwatch_alarms(cloudwatch_client, up_and_down)
create_cloudwatch_alarms(cloudwatch_client, sp_up, sp_down)
# Load Balancer DNS name
response = elbv2_client.describe_load_balancers(
@ -430,7 +437,6 @@ def main():
'load_balancer_arn': load_balancer_arn,
'listener_arn': listener_arn,
'asg_name': f"{PREFIX}-asg",
'up_and_down_policy_arn': up_and_down,
'dns_name': dns_name,
}