question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

boto3 Autoscaling creating scale up and down policy and cloudwatch

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
magic5650commented, Nov 13, 2019

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

0reactions
miguelconde91commented, Aug 30, 2018

I have the same problem, any idea?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found