boto3 Autoscaling creating scale up and down policy and cloudwatch
See original GitHub issueI am able to create Launch configuration and Autoscale using boto3. But How to create scale up alarm and scale down policy and cloudwatch alarm. On internet i am only able to find docs to configure it using boto2 but i need boto3 Over boto2 we can do like below and it works. But i need to acheive same using boto3. I am able to find put_scaling_policy function but not able to understand the example. Can someone suggest a working config using boto3. Below is the boto2 code
#Defining Scale Up and Scale down policy
from boto.ec2.autoscale import ScalingPolicy
scale_up_policy = ScalingPolicy(
name='scale_up', adjustment_type='ChangeInCapacity',
as_name=ag.name, scaling_adjustment=1, cooldown=180)
scale_down_policy = ScalingPolicy(
name='scale_down', adjustment_type='ChangeInCapacity',
as_name=ag.name, scaling_adjustment=-1, cooldown=180)
#Creating the Autoscale policy
conn.create_scaling_policy(scale_up_policy)
conn.create_scaling_policy(scale_down_policy)
#Creating Cloudwatch alarm
import boto.ec2.cloudwatch
cloudwatch = boto.ec2.cloudwatch.connect_to_region(region)
#Create an alarm for when to scale up, and one for when to scale down.
from boto.ec2.cloudwatch import MetricAlarm
scale_up_alarm = MetricAlarm(
name='scale_up_on_cpu', namespace='AWS/EC2',
metric='CPUUtilization', statistic='Average',
comparison='>', threshold='70',
period='60', evaluation_periods=2,
alarm_actions=[scale_up_policy.policy_arn],
dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_up_alarm)
scale_down_alarm = MetricAlarm(
name='scale_down_on_cpu', namespace='AWS/EC2',
metric='CPUUtilization', statistic='Average',
comparison='<', threshold='40',
period='60', evaluation_periods=2,
alarm_actions=[scale_down_policy.policy_arn],
dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_down_alarm)
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
class AutoScaling. Client - Boto3 Docs 1.26.33 documentation
Amazon EC2 Auto Scaling is designed to automatically launch and terminate EC2 instances based on user-defined scaling policies, scheduled actions, and health ...
Read more >Amazon EC2 Auto Scaling examples using SDK for Python ...
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Python (Boto3) with...
Read more >AWS Autoscaling group configured with ELB and Alarms in ...
Scaling Policies and Alarms: It is the set of rules for determining when to scale an instance up or down in an autoscaling...
Read more >An Introduction to boto's Autoscale interface — boto v2.49.0
Autoscale Group (AG): An AG can be viewed as a collection of criteria for maintaining or scaling a set of EC2 instances over...
Read more >python - How can I configure Auto Scaling with boto using ...
I have successfully created a launch configuration and a Auto Scaling group, but am having trouble creating scaling policies and metric alarms.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
def put_extend_scaling_policy(autoscaling_group_name, region): client = boto3.client(‘autoscaling’, region_name=region) response = client.put_scaling_policy( AutoScalingGroupName=autoscaling_group_name, PolicyName=autoscaling_group_name + ‘_CPU_extend’, PolicyType=‘SimpleScaling’, AdjustmentType=‘ChangeInCapacity’, ScalingAdjustment=2, Cooldown=300, ) PolicyARN = response[‘PolicyARN’] client = boto3.client(‘cloudwatch’, region_name=region) response = client.put_metric_alarm( AlarmName=‘awsec2-’ + autoscaling_group_name + ‘-CPU-Over-50’, AlarmDescription=‘string’, ActionsEnabled=True, AlarmActions=[ PolicyARN, ], MetricName=‘CPUUtilization’, Namespace=‘AWS/EC2’, Statistic=‘Average’, Dimensions=[ { ‘Name’: ‘AutoScalingGroupName’, ‘Value’: autoscaling_group_name }, ], Period=60, Unit=‘Percent’, EvaluationPeriods=2, DatapointsToAlarm=2, Threshold=50.0, ComparisonOperator=‘GreaterThanOrEqualToThreshold’, TreatMissingData=‘ignore’, ) return PolicyARN
I have the same problem, any idea?