autoscaling
This commit is contained in:
parent
2c03bca76b
commit
23e1ff30a9
@ -265,10 +265,10 @@ 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 Policies
|
# Put Scaling Policy
|
||||||
up = autoscaling_client.put_scaling_policy(
|
up_and_down = autoscaling_client.put_scaling_policy(
|
||||||
AutoScalingGroupName=f"{PREFIX}-asg",
|
AutoScalingGroupName=f"{PREFIX}-asg",
|
||||||
PolicyName=f"{PREFIX}-scale-up",
|
PolicyName=f"{PREFIX}-scale-policy",
|
||||||
PolicyType='TargetTrackingScaling',
|
PolicyType='TargetTrackingScaling',
|
||||||
Cooldown=180,
|
Cooldown=180,
|
||||||
TargetTrackingConfiguration={
|
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(
|
def create_cloudwatch_alarms(cloudwatch_client, sp):
|
||||||
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):
|
|
||||||
# Create CloudWatch Alarms
|
# Create CloudWatch Alarms
|
||||||
response = cloudwatch_client.put_metric_alarm(
|
response = cloudwatch_client.put_metric_alarm(
|
||||||
AlarmName=f"{PREFIX}-scale-up",
|
AlarmName=f"{PREFIX}-scale-alarm",
|
||||||
ComparisonOperator='GreaterThanThreshold',
|
ComparisonOperator='GreaterThanThreshold',
|
||||||
EvaluationPeriods=1,
|
EvaluationPeriods=1,
|
||||||
MetricName='CPUUtilization',
|
MetricName='CPUUtilization',
|
||||||
@ -311,21 +295,20 @@ def create_cloudwatch_alarms(cloudwatch_client, asg_name, up_sp, down_sp):
|
|||||||
Statistic='Average',
|
Statistic='Average',
|
||||||
Threshold=70.0,
|
Threshold=70.0,
|
||||||
ActionsEnabled=True,
|
ActionsEnabled=True,
|
||||||
AlarmActions=[up_sp],
|
AlarmActions=[sp],
|
||||||
AlarmDescription='Scale-up alarm',
|
AlarmDescription='Scale-up or scale-down alarm',
|
||||||
Dimensions=[
|
Dimensions=[{
|
||||||
{
|
'Name': 'AutoScalingGroupName',
|
||||||
'Name': 'AutoScalingGroupName',
|
'Value': f"{PREFIX}-asg"
|
||||||
'Value': f"{PREFIX}-asg"
|
}],
|
||||||
}
|
|
||||||
],
|
|
||||||
Unit='Percent'
|
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(
|
response = cloudwatch_client.put_metric_alarm(
|
||||||
AlarmName=f"{PREFIX}-scale-down",
|
AlarmName=f"{PREFIX}-scale-down-alarm",
|
||||||
ComparisonOperator='LessThanThreshold',
|
ComparisonOperator='LessThanThreshold',
|
||||||
EvaluationPeriods=1,
|
EvaluationPeriods=1,
|
||||||
MetricName='CPUUtilization',
|
MetricName='CPUUtilization',
|
||||||
@ -334,14 +317,12 @@ def create_cloudwatch_alarms(cloudwatch_client, asg_name, up_sp, down_sp):
|
|||||||
Statistic='Average',
|
Statistic='Average',
|
||||||
Threshold=30.0,
|
Threshold=30.0,
|
||||||
ActionsEnabled=True,
|
ActionsEnabled=True,
|
||||||
AlarmActions=[down_sp],
|
AlarmActions=[sp],
|
||||||
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='Percent'
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -423,11 +404,11 @@ def main():
|
|||||||
|
|
||||||
# Scaling Policies
|
# Scaling Policies
|
||||||
print("Creating 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
|
# CloudWatch Alarms
|
||||||
print("Creating 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
|
# Load Balancer DNS name
|
||||||
response = elbv2_client.describe_load_balancers(
|
response = elbv2_client.describe_load_balancers(
|
||||||
|
Loading…
Reference in New Issue
Block a user