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