AWS Autoscaling - scale down script by Netflix.




    #  Copyright (C) 2011 Netflix

    # Licensed under the Apache License, Version 2.0 (the "License");

    # you may not use this file except in compliance with the License.

    # You may obtain a copy of the License at




    # Unless required by applicable law or agreed to in writing, software

    # distributed under the License is distributed on an "AS IS" BASIS,

    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

    # See the License for the specific language governing permissions and

    # limitations under the License.



    # Justin Becker

    # Creates AWS policies and alarms neccessary to enable auto-scaling

    # Optionally can create the alarm to send an email notification on 

    # an auto-scaling event.


    # Also creates a script to delete all the policies and

    # alarms created.

    if [ $# -lt 8 ]; then 

      echo "usage: scale-up asgname percentage namespace metric threshold period occurrences cooldown [topic-arn]"



    # COOLDOWN : 

    # PERCENTAGE : Number of machines to scale by

    # NAMESPACE : Cloudwatch namespace.  Example, "AWS/EC2"

    # METRIC : Cloudwatch metric.  Example, "CPUUtilization"

    # THRESHOLD : Metric threshold.  Example, 60 -- CPU greater than 60%

    # PERIOD :  Time in seconds, must be greater than 60

    # OCCURRENCES : Number of occurrences of metric crossing threshold in period

    # COOLDOWN : Amount of time to wait before policy is active again, allows a time-buffer before firing another capacity event.













    echo "Creating policy: "$POLICY_NAME

    ARN_POLICY=`as-put-scaling-policy $POLICY_NAME --auto-scaling-group $ASG  --adjustment=-$PERCENTAGE --type PercentChangeInCapacity --cooldown $COOLDOWN`

    echo "Policy created: "$ARN_POLICY

    echo "Creating alarm: "$POLICY_ALARM_NAME 

    # Check if topic-arn passed in, if so, add as an action

    if [ $# -eq 9 ]; then


        ARN_POLICY_ALARM=`mon-put-metric-alarm $POLICY_ALARM_NAME  --dimensions "AutoScalingGroupName=$ASG" --comparison-operator  LessThanThreshold  --evaluation-periods  $OCCURRENCES --metric-name  $METRIC  --namespace  $NAMESPACE  --period  $PERIOD  --statistic Average --threshold  $THRESHOLD --alarm-actions $ARN_POLICY,$TOPIC_ARN`


        ARN_POLICY_ALARM=`mon-put-metric-alarm $POLICY_ALARM_NAME  --dimensions "AutoScalingGroupName=$ASG" --comparison-operator  LessThanThreshold  --evaluation-periods  $OCCURRENCES --metric-name  $METRIC  --namespace  $NAMESPACE  --period  $PERIOD  --statistic Average --threshold  $THRESHOLD --alarm-actions $ARN_POLICY`


    echo "Alarm created: "$POLICY_ALARM_NAME 

    echo "Creating rollback file: "$ROLLBACK



    echo "#!/bin/sh" > $ROLLBACK

    echo "echo 'Deleting alarm: '"$POLICY_ALARM_NAME >> $ROLLBACK

    echo "mon-delete-alarms "$POLICY_ALARM_NAME >> $ROLLBACK



    echo "echo 'Deleting policy: '"$POLICY_NAME >> $ROLLBACK

    echo "as-delete-policy "$ARN_POLICY >> $ROLLBACK

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise © 2020