def create_autoscaling_policy(autoscale, cluster_name, opts): scale_up_policy = ScalingPolicy( name='scale_up', adjustment_type='ChangeInCapacity', as_name=cluster_name + "-ag", scaling_adjustment=5, cooldown=60) scale_down_policy = ScalingPolicy( name='scale_down', adjustment_type='ChangeInCapacity', as_name=cluster_name + "-ag", scaling_adjustment=-1, cooldown=60) autoscale.create_scaling_policy(scale_up_policy) autoscale.create_scaling_policy(scale_down_policy) scale_up_policy = autoscale.get_all_policies( as_group=cluster_name + "-ag", policy_names=['scale_up'])[0] scale_down_policy = autoscale.get_all_policies( as_group=cluster_name + "-ag", policy_names=['scale_down'])[0] alarm_dimensions = {"AutoScalingGroupName": cluster_name + "-ag"} cloudwatch = boto.ec2.cloudwatch.connect_to_region(opts.region) scale_up_alarm = MetricAlarm( name='scale_up_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='50', period='60', evaluation_periods=1, 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=1, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm)
def scaling_policy(my_group): 'define the scaing policy for the group' scale_up_policy = ScalingPolicy(name='scale_up', adjustment_type='ChangeInCapacity', as_name=my_group, scaling_adjustment=1, cooldown=180) scale_down_policy = ScalingPolicy(name='scale_down', adjustment_type='ChangeInCapacity', as_name=my_group, scaling_adjustment=-1, cooldown=180) ##########Submitting policy to AWS as_conn.create_scaling_policy(scale_up_policy) as_conn.create_scaling_policy(scale_down_policy) ## need to refresh scaling policy by requesting them back scale_up_policy = as_conn.get_all_policies(as_group=my_group, policy_names=['scale_up'])[0] scale_down_policy = as_conn.get_all_policies(as_group=my_group, policy_names=['scale_down' ])[0] ###CloudWatch alarms that will define when to run the Auto Scaling Policies alarm_dimensions = {"AutoScalingGroupName": my_group} #def set_metricAlarm(): 'set alarm for scaing the instances in as_group' scale_up_alarm = MetricAlarm(name='scale_up_on_cpu_"my_group"', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='70', period='300', 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_"my_group"', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='<', threshold='40', period='300', evaluation_periods=2, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm)
def main(): parser = optparse.OptionParser('Usage: %prog <options>') parser.add_option('-e', '--email', dest='email', help='The email address to send notifications to whenever an ' 'alert is triggered.') parser.add_option('-n', '--name', dest='name', help='The name of the alarm (e.g., BillingAlarm-1000).') parser.add_option('-t', '--threshold', dest='threshold', help='The dollar amount of estimated monthly charges which, ' 'when exceeded, causes an alert to be triggered.') parser.add_option('-p', '--period', dest='period', default=Defaults.PREIOD, help='The period in seconds over which the estimated monthly ' 'charges statistic is applied.') (opts, args) = parser.parse_args() if (0 != len(args) or opts.email is None or opts.threshold is None): parser.print_help() return 1 try: sns = boto.connect_sns() topic = sns.create_topic('BillingNotifications') \ ['CreateTopicResponse'] \ ['CreateTopicResult'] \ ['TopicArn'] res = sns.subscribe(topic, 'email', opts.email) \ ['SubscribeResponse'] \ ['SubscribeResult'] \ ['SubscriptionArn'] if res == 'pending confirmation': raw_input('Please confirm subscription. Press [ENTER] when done...') cloudwatch = boto.connect_cloudwatch() alarm = MetricAlarm(name=opts.name if opts.name is not None else 'BillingAlarm-{0}'.format(opts.threshold), description='Estimated Monthly Charges', alarm_actions=[topic], metric=Defaults.METRIC, namespace='AWS/Billing', statistic=Defaults.STATISTIC, dimensions={'Currency':'USD'}, period=opts.period, evaluation_periods=1, threshold=int(opts.threshold), comparison='>=') cloudwatch.create_alarm(alarm) except Error, err: sys.stderr.write('[ERROR] {0}\n'.format(err)) return 1
def create_autoscalegroup_watch(conn): lc = LaunchConfiguration(name='my-launch_config', image_id='ami-2b7b2c42', key_name='key2bench', security_groups=['default']) conn.create_launch_configuration(lc) scale_up_policy = ScalingPolicy( name='scale_up', adjustment_type='ChangeInCapacity', as_name='Auto_scale_group', scaling_adjustment=1, cooldown=180) scale_down_policy = ScalingPolicy( name='scale_down', adjustment_type='ChangeInCapacity', as_name='Auto_scale_group', scaling_adjustment=-1, cooldown=180) conn.create_scaling_policy(scale_up_policy) conn.create_scaling_policy(scale_down_policy) scale_up_policy = conn.get_all_policies( as_group='Auto_scale_group', policy_names=['scale_up'])[0] scale_down_policy = conn.get_all_policies( as_group='Auto_scale_group', policy_names=['scale_down'])[0] # ======create auto scale group with configuration ====== # ============= CloudWatch ==================== cloudwatch = boto.ec2.cloudwatch.connect_to_region('us-east-1') alarm_dimensions = {"AutoScalingGroupName": 'Auto_scale_group'} scale_up_alarm = MetricAlarm( name='scale_up_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='80', period='60', evaluation_periods=5, 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='20', period='60', evaluation_periods=5, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm) conn.put_notification_configuration(ag, 'arn:aws:sns:us-east-1:233941742685:Auto_Scale_events',['autoscaling:EC2_INSTANCE_LAUNCH', 'autoscaling:EC2_INSTANCE_TERMINATE'])
def create_alarm(): conn = get_autoscale_connection() # create scaling policy for scaling up and down scale_up_policy = ScalingPolicy( name="scale_up", adjustment_type="ChangeInCapacity", as_name=group_name, scaling_adjustment=1, cooldown=5 ) scale_down_policy = ScalingPolicy( name="scale_down", adjustment_type="ChangeInCapacity", as_name=group_name, scaling_adjustment=-1, cooldown=5 ) conn.create_scaling_policy(scale_up_policy) conn.create_scaling_policy(scale_down_policy) # refresh the policy scale_up_policy = conn.get_all_policies(as_group=group_name, policy_names=["scale_up"])[0] scale_down_policy = conn.get_all_policies(as_group=group_name, policy_names=["scale_down"])[0] # create cloud watch to assign the policy cloudwatch = boto.ec2.cloudwatch.connect_to_region( amazon_key.REGION, aws_access_key_id=amazon_key.ACCESS_KEY, aws_secret_access_key=amazon_key.SECRET_KEY ) alarm_dimensions = {"AutoScalingGroupName": group_name} scale_up_alarm = MetricAlarm( name="scale_up_on_cpu", namespace="AWS/EC2", metric="CPUUtilization", statistic="Average", comparison=">", threshold="60", period="60", evaluation_periods=1, 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="30", period="60", evaluation_periods=1, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions, ) cloudwatch.create_alarm(scale_down_alarm)
def create_autoscaling_policy(autoscale, cluster_name, opts): scale_up_policy = ScalingPolicy( name='scale_up', adjustment_type='ChangeInCapacity', as_name=cluster_name + "-ag", scaling_adjustment=opts.scale_up_nodes_amount, cooldown=opts.scale_up_cooldown) scale_down_policy = ScalingPolicy( name='scale_down', adjustment_type='ChangeInCapacity', as_name=cluster_name + "-ag", scaling_adjustment=-opts.scale_down_nodes_amount, cooldown=opts.scale_down_cooldown) autoscale.create_scaling_policy(scale_up_policy) autoscale.create_scaling_policy(scale_down_policy) scale_up_policy = autoscale.get_all_policies(as_group=cluster_name + "-ag", policy_names=['scale_up'])[0] scale_down_policy = autoscale.get_all_policies( as_group=cluster_name + "-ag", policy_names=['scale_down'])[0] alarm_dimensions = {"AutoScalingGroupName": cluster_name + "-ag"} cloudwatch = boto.ec2.cloudwatch.connect_to_region(opts.region) scale_up_alarm = MetricAlarm(name='scale_up_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='50', period='60', evaluation_periods=1, 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=1, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm)
def scaling_policy(my_group): 'define the scaing policy for the group' scale_up_policy = ScalingPolicy(name='scale_up', adjustment_type='ChangeInCapacity', as_name=my_group, scaling_adjustment=1, cooldown=180) scale_down_policy = ScalingPolicy(name='scale_down', adjustment_type='ChangeInCapacity', as_name=my_group, scaling_adjustment=-1, cooldown=180) ##########Submitting policy to AWS as_conn.create_scaling_policy(scale_up_policy) as_conn.create_scaling_policy(scale_down_policy) ## need to refresh scaling policy by requesting them back scale_up_policy = as_conn.get_all_policies(as_group=my_group, policy_names=['scale_up'])[0] scale_down_policy = as_conn.get_all_policies(as_group=my_group, policy_names=['scale_down'])[0] ###CloudWatch alarms that will define when to run the Auto Scaling Policies alarm_dimensions = {"AutoScalingGroupName":my_group} #def set_metricAlarm(): 'set alarm for scaing the instances in as_group' scale_up_alarm = MetricAlarm( name='scale_up_on_cpu_"my_group"', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='70', period='300', 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_"my_group"', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='<', threshold='40', period='300', evaluation_periods=2, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm)
as_group=AUTO_SCALE_GROUP, policy_names=['scale_up'])[0] scale_down_policy = conn.get_all_policies( as_group=AUTO_SCALE_GROUP, policy_names=['scale_down'])[0] cloudwatch = boto.ec2.cloudwatch.connect_to_region("us-east-1", aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=SECRET_ACCESS_KEY) alarm_dimensions = {"AutoScalingGroupName": AUTO_SCALE_GROUP} scale_up_alarm = MetricAlarm( name='scale_up_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='65', period='180', evaluation_periods=1, 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='25', period='240', evaluation_periods=1, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm) time.sleep(60) i = 0 while i < 3: req = "http://" + load_generator.public_dns_name + "/warmup"
def create_AutoScaling(): print "Creating AutoScaling..." # establish connection as_conn = AutoScaleConnection(AWSAccessKeyId, AWSSecretKey) # create launch configuration global lc lc = LaunchConfiguration(name='lc', image_id=DATA_CEN_AMI, key_name=ACCESS_KEY, instance_monitoring=True, security_groups=[SECURITY_GRP], instance_type=MACHINE_TYPE) as_conn.create_launch_configuration(lc) # create tag for autoscaling group as_tag = Tag(key="Project", value="2.2", propagate_at_launch=True, resource_id='my_group') # create aotoscaling group global ag ag = AutoScalingGroup(group_name='my_group', load_balancers=['myELB'], availability_zones=['us-east-1a'], launch_config=lc, min_size=MIN_SIZE, max_size=MAX_SIZE, connection=as_conn, tags=[as_tag]) # associate the autoscaling group with launch configuration as_conn.create_auto_scaling_group(ag) # build the scale policy scale_up_policy = ScalingPolicy(name='scale_up', adjustment_type='ChangeInCapacity', as_name='my_group', scaling_adjustment=1, cooldown=60) scale_down_policy = ScalingPolicy(name='scale_down', adjustment_type='ChangeInCapacity', as_name='my_group', scaling_adjustment=-1, cooldown=60) # register the scale policy as_conn.create_scaling_policy(scale_up_policy) as_conn.create_scaling_policy(scale_down_policy) # refresh the scale policy for extra information scale_up_policy = as_conn.get_all_policies(as_group='my_group', policy_names=['scale_up'])[0] scale_down_policy = as_conn.get_all_policies(as_group='my_group', policy_names=['scale_down' ])[0] # create cloudwatch alarm cloudwatch = CloudWatchConnection(aws_access_key_id=AWSAccessKeyId, aws_secret_access_key=AWSSecretKey, is_secure=True) # region='us-east-1a') # assocate cloudwatch with alarm alarm_dimensions = {"AutoScalingGroupName": 'my_group'} # create scale up alarm scale_up_alarm = MetricAlarm(name='scale_up_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='50', period='60', evaluation_periods=2, alarm_actions=[scale_up_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_up_alarm) # create scale down alarm scale_down_alarm = MetricAlarm( name='scale_down_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='<', threshold='20', period='60', evaluation_periods=1, alarm_actions=[scale_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm) print "AutoScaling created successfully"
cloudwatch = boto.ec2.cloudwatch.connect_to_region('us-east-1') alarm_dimensions = {"AutoScalingGroupName": 'my_autoscale_group'} 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) ec2 = boto.ec2.connect_to_region('us-east-1')
def start_elb(tag, user_data, region, auto_register, as_ami, subnet_id, security_groups, public_ip_address, iam_role, zone_strings, elastic_load_balancer): print "Using tag \"" + tag + "\"" conn_reg = boto.ec2.connect_to_region(region_name=region) # =================Construct a list of all availability zones for your region========= conn_elb = boto.ec2.elb.connect_to_region(region_name=region) conn_as = boto.ec2.autoscale.connect_to_region(region_name=region) # =================Create a Load Balancer============================================= # For a complete list of options see http://boto.cloudhackers.com/ref/ec2.html#module-boto.ec2.elb.healthcheck hc = HealthCheck('healthCheck', interval=elastic_load_balancer['interval'], target=elastic_load_balancer['health_check_target'], timeout=elastic_load_balancer['timeout']) # ELB does not accept any special characters elb_tag = tag elb_tag = elb_tag.replace("_", "") elb_tag = elb_tag.replace("-", "") elb_tag = elb_tag.replace(".", "") print "ELB name: \"" + elb_tag + "\"" # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.elb.ELBConnection.create_load_balancer lb = conn_elb.create_load_balancer( name=elb_tag + 'Elb', zones=None, subnets=subnet_id, security_groups=security_groups, listeners=elastic_load_balancer['connection_forwarding']) if auto_register: aws_library.add_instances_to_lb(tag=tag, lb=lb, region=region) lb.configure_health_check(hc) # DNS name for your new load balancer print "Map the CNAME of your website to: %s" % lb.dns_name # =================Create a Auto Scaling Group and a Launch Configuration============================================ # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.launchconfig.LaunchConfiguration lc = LaunchConfiguration(name=elb_tag + "Lc", image_id=as_ami['id'], key_name=as_ami['access_key'], security_groups=as_ami['security_groups'], instance_type=as_ami['instance_type'], instance_monitoring=as_ami['instance_monitoring'], instance_profile_name=iam_role, user_data=user_data) conn_as.create_launch_configuration(lc) # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.group.AutoScalingGroup ag = AutoScalingGroup(group_name=elb_tag + "Sg", load_balancers=[elb_tag], availability_zones=zone_strings, launch_config=lc, min_size=autoscaling_group['min_size'], max_size=autoscaling_group['max_size'], associate_public_ip_address=public_ip_address, vpc_zone_identifier=subnet_id) conn_as.create_auto_scaling_group(ag) # =================Create Scaling Policies============================================= # Policy for scaling the number of servers up and down # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.policy.ScalingPolicy scaling_up_policy = ScalingPolicy(name=elb_tag + "webserverScaleUpPolicy", adjustment_type='ChangeInCapacity', as_name=ag.name, scaling_adjustment=1, cooldown=60) scaling_down_policy = ScalingPolicy(name=elb_tag + "webserverScaleDownPolicy", adjustment_type='ChangeInCapacity', as_name=ag.name, scaling_adjustment=-1, cooldown=180) conn_as.create_scaling_policy(scaling_up_policy) conn_as.create_scaling_policy(scaling_down_policy) scaling_up_policy = conn_as.get_all_policies( as_group=elb_tag + "Sg", policy_names=[elb_tag + "webserverScaleUpPolicy"])[0] scaling_down_policy = conn_as.get_all_policies( as_group=elb_tag + "Sg", policy_names=[elb_tag + "webserverScaleDownPolicy"])[0] cloudwatch = boto.ec2.cloudwatch.connect_to_region(region) alarm_dimensions = {"AutoScalingGroupName": 'my_group'} scale_up_alarm = MetricAlarm(name=elb_tag + 'scale_up_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='70', period='60', evaluation_periods=2, alarm_actions=[scaling_up_policy.policy_arn], dimensions=alarm_dimensions) scale_down_alarm = MetricAlarm( name=elb_tag + 'scale_down_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='<', threshold='40', period='60', evaluation_periods=2, alarm_actions=[scaling_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm) cloudwatch.create_alarm(scale_up_alarm)
def main(): parser = optparse.OptionParser('Usage: %prog [options]') parser.add_option('-n', '--name', dest='name', help='The name of this configuration (e.g., TEST).') parser.add_option('-i', '--image', dest='image', default=Defaults.IMAGE, help='The Amazon Machine Image (AMI) ID that will be used to launch ' 'EC2 instances. The most recent Amazon Linux AMI 2013.09.2 (ami-' 'a43909e1) is used by default.') parser.add_option('-t', '--type', dest='type', default=Defaults.TYPE, help='The type of the Amazon EC2 instance. If not specified, micro ' 'instance (t1.micro) type will be used.') parser.add_option('-k', '--key', dest='key', help='The name of the key pair to use when creating EC2 instances. ' 'This options is required.') parser.add_option('-g', '--group', dest='group', help='Security group that will be used when creating EC2 instances. ' 'This option is required.') parser.add_option('-m', '--min', dest='min', default=Defaults.MIN_INSTANCES, help='The minimum number of EC2 instances in the auto scaling group. ' 'By default it is set to 2.') parser.add_option('-M', '--max', dest='max', default=Defaults.MAX_INSTANCES, help='The maximum size of the auto scaling group. By default it is ' 'set to 4.') parser.add_option('-z', '--zone', dest='zones', action='append', help='The availability zone for the auto scaling group. This option ' 'is required.') parser.add_option('-l', '--load-balancer', dest='lbs', action='append', help='The name of an existing AWS load balancer to use, if any.') parser.add_option('--min-threshold', dest='min_threshold', default=Defaults.MIN_THRESHOLD, help='The minimum CPU utilization ' 'threshold that triggers an alarm. This option is not required and ' 'is set to 40% by default.') parser.add_option('--max-threshold', dest='max_threshold', default=Defaults.MAX_THRESHOLD, help='The maximum CPU utilization ' 'threshold that triggers an alarm. This option is not required and ' 'is set to 60% by default.') parser.add_option('-a', '--adjustment', dest='adjustment', default=Defaults.ADJUSTMENT, help='The number of EC2 instances by ' 'which to scale up or down. This is set to 1 by default.') parser.add_option('-p', '--period', dest='period', default=Defaults.PERIOD, help='The evaluation period in seconds. This is optional and is set ' 'to 300 seconds by default.') (opts, args) = parser.parse_args() if (0 != len(args) or opts.name is None or opts.key is None or opts.group is None or opts.zones is None): parser.print_help() return 1 try: c = boto.connect_autoscale() lc = _create_launch_configuration(c, opts) g = _create_autoscaling_group(c, lc, opts) policy_up = ScalingPolicy(name=opts.name + s.POLICY_UP_SUFFIX, as_name=g.name, scaling_adjustment=opts.adjustment, adjustment_type='ChangeInCapacity') c.create_scaling_policy(policy_up) cloudwatch = boto.connect_cloudwatch() alarm_high = MetricAlarm(name=opts.name + s.ALARM_HIGH_SUFFIX, alarm_actions=[policy_up], metric='CPUUtilization', namespace='AWS/EC2', statistic='Average', dimensions={'AutoScalingGroupName': g.name}, period=opts.period, evaluation_periods=1, threshold=int(opts.max_threshold), comparison='>') cloudwatch.create_alarm(alarm_high) policy_down = ScalingPolicy(name=opts.name + s.POLICY_DOWN_SUFFIX, as_name=g.name, scaling_adjustment=-opts.adjustment, adjustment_type='ChangeInCapacity') autoscale.create_scaling_policy(policy_down) alarm_low = MetricAlarm(name=opts.name + s.ALARM_LOW_SUFFIX, alarm_actions=[policy_down], metric='CPUUtilization', namespace='AWS/EC2', statistic='Average', dimensions={'AutoScalingGroupName': g.name}, period=opts.period, evaluation_periods=1, threshold=int(opts.min_threshold), comparison='<') cloudwatch.create_alarm(alarm_low) except Error, err: sys.stderr.write('[ERROR] {0}\n'.format(err)) return 1
def start_elb(tag, user_data, region, auto_register, as_ami, subnet_id, security_groups, public_ip_address, iam_role, zone_strings, elastic_load_balancer): print "Using tag \"" + tag + "\"" conn_reg = boto.ec2.connect_to_region(region_name=region) # =================Construct a list of all availability zones for your region========= conn_elb = boto.ec2.elb.connect_to_region(region_name=region) conn_as = boto.ec2.autoscale.connect_to_region(region_name=region) # =================Create a Load Balancer============================================= # For a complete list of options see http://boto.cloudhackers.com/ref/ec2.html#module-boto.ec2.elb.healthcheck hc = HealthCheck('healthCheck', interval=elastic_load_balancer['interval'], target=elastic_load_balancer['health_check_target'], timeout=elastic_load_balancer['timeout']) # ELB does not accept any special characters elb_tag = tag elb_tag = elb_tag.replace("_", "") elb_tag = elb_tag.replace("-", "") elb_tag = elb_tag.replace(".", "") print "ELB name: \"" + elb_tag + "\"" # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.elb.ELBConnection.create_load_balancer lb = conn_elb.create_load_balancer(name=elb_tag + 'Elb', zones=None, subnets=subnet_id, security_groups=security_groups, listeners=elastic_load_balancer['connection_forwarding']) if auto_register: aws_library.add_instances_to_lb(tag=tag, lb=lb, region=region) lb.configure_health_check(hc) # DNS name for your new load balancer print "Map the CNAME of your website to: %s" % lb.dns_name # =================Create a Auto Scaling Group and a Launch Configuration============================================ # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.launchconfig.LaunchConfiguration lc = LaunchConfiguration(name=elb_tag + "Lc", image_id=as_ami['id'], key_name=as_ami['access_key'], security_groups=as_ami['security_groups'], instance_type=as_ami['instance_type'], instance_monitoring=as_ami['instance_monitoring'], instance_profile_name=iam_role, user_data=user_data) conn_as.create_launch_configuration(lc) # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.group.AutoScalingGroup ag = AutoScalingGroup(group_name=elb_tag + "Sg", load_balancers=[elb_tag], availability_zones=zone_strings, launch_config=lc, min_size=autoscaling_group['min_size'], max_size=autoscaling_group['max_size'], associate_public_ip_address=public_ip_address, vpc_zone_identifier=subnet_id) conn_as.create_auto_scaling_group(ag) # =================Create Scaling Policies============================================= # Policy for scaling the number of servers up and down # For a complete list of options see # http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.policy.ScalingPolicy scaling_up_policy = ScalingPolicy(name=elb_tag + "webserverScaleUpPolicy", adjustment_type='ChangeInCapacity', as_name=ag.name, scaling_adjustment=1, cooldown=60) scaling_down_policy = ScalingPolicy(name=elb_tag + "webserverScaleDownPolicy", adjustment_type='ChangeInCapacity', as_name=ag.name, scaling_adjustment=-1, cooldown=180) conn_as.create_scaling_policy(scaling_up_policy) conn_as.create_scaling_policy(scaling_down_policy) scaling_up_policy = conn_as.get_all_policies( as_group=elb_tag + "Sg", policy_names=[elb_tag + "webserverScaleUpPolicy"])[0] scaling_down_policy = conn_as.get_all_policies( as_group=elb_tag + "Sg", policy_names=[elb_tag + "webserverScaleDownPolicy"])[0] cloudwatch = boto.ec2.cloudwatch.connect_to_region(region) alarm_dimensions = {"AutoScalingGroupName": 'my_group'} scale_up_alarm = MetricAlarm(name=elb_tag + 'scale_up_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='>', threshold='70', period='60', evaluation_periods=2, alarm_actions=[scaling_up_policy.policy_arn], dimensions=alarm_dimensions) scale_down_alarm = MetricAlarm(name=elb_tag + 'scale_down_on_cpu', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='<', threshold='40', period='60', evaluation_periods=2, alarm_actions=[scaling_down_policy.policy_arn], dimensions=alarm_dimensions) cloudwatch.create_alarm(scale_down_alarm) cloudwatch.create_alarm(scale_up_alarm)
def main(): parser = optparse.OptionParser('Usage: %prog <options>') parser.add_option( '-e', '--email', dest='email', help='The email address to send notifications to whenever an ' 'alert is triggered.') parser.add_option('-n', '--name', dest='name', help='The name of the alarm (e.g., BillingAlarm-1000).') parser.add_option( '-t', '--threshold', dest='threshold', help='The dollar amount of estimated monthly charges which, ' 'when exceeded, causes an alert to be triggered.') parser.add_option( '-p', '--period', dest='period', default=Defaults.PREIOD, help='The period in seconds over which the estimated monthly ' 'charges statistic is applied.') (opts, args) = parser.parse_args() if (0 != len(args) or opts.email is None or opts.threshold is None): parser.print_help() return 1 try: sns = boto.connect_sns() topic = sns.create_topic('BillingNotifications') \ ['CreateTopicResponse'] \ ['CreateTopicResult'] \ ['TopicArn'] res = sns.subscribe(topic, 'email', opts.email) \ ['SubscribeResponse'] \ ['SubscribeResult'] \ ['SubscriptionArn'] if res == 'pending confirmation': raw_input( 'Please confirm subscription. Press [ENTER] when done...') cloudwatch = boto.connect_cloudwatch() alarm = MetricAlarm(name=opts.name if opts.name is not None else 'BillingAlarm-{0}'.format(opts.threshold), description='Estimated Monthly Charges', alarm_actions=[topic], metric=Defaults.METRIC, namespace='AWS/Billing', statistic=Defaults.STATISTIC, dimensions={'Currency': 'USD'}, period=opts.period, evaluation_periods=1, threshold=int(opts.threshold), comparison='>=') cloudwatch.create_alarm(alarm) except Error, err: sys.stderr.write('[ERROR] {0}\n'.format(err)) return 1