def test_describe_instance_health_boto3(): elb = boto3.client('elb', region_name="us-east-1") ec2 = boto3.client('ec2', region_name="us-east-1") instances = ec2.run_instances(MinCount=2, MaxCount=2)['Instances'] lb_name = "my_load_balancer" elb.create_load_balancer( Listeners=[{ 'InstancePort': 80, 'LoadBalancerPort': 8080, 'Protocol': 'HTTP' }], LoadBalancerName=lb_name, ) elb.register_instances_with_load_balancer( LoadBalancerName=lb_name, Instances=[{'InstanceId': instances[0]['InstanceId']}] ) instances_health = elb.describe_instance_health( LoadBalancerName=lb_name, Instances=[{'InstanceId': instance['InstanceId']} for instance in instances] ) instances_health['InstanceStates'].should.have.length_of(2) instances_health['InstanceStates'][0]['InstanceId'].\ should.equal(instances[0]['InstanceId']) instances_health['InstanceStates'][0]['State'].\ should.equal('InService') instances_health['InstanceStates'][1]['InstanceId'].\ should.equal(instances[1]['InstanceId']) instances_health['InstanceStates'][1]['State'].\ should.equal('Unknown')
def test_describe_instance_health_boto3(): elb = boto3.client("elb", region_name="us-east-1") ec2 = boto3.client("ec2", region_name="us-east-1") instances = ec2.run_instances(MinCount=2, MaxCount=2)["Instances"] lb_name = "my_load_balancer" elb.create_load_balancer( Listeners=[{ "InstancePort": 80, "LoadBalancerPort": 8080, "Protocol": "HTTP" }], LoadBalancerName=lb_name, ) elb.register_instances_with_load_balancer(LoadBalancerName=lb_name, Instances=[{ "InstanceId": instances[0]["InstanceId"] }]) instances_health = elb.describe_instance_health( LoadBalancerName=lb_name, Instances=[{ "InstanceId": instance["InstanceId"] } for instance in instances], ) instances_health["InstanceStates"].should.have.length_of(2) instances_health["InstanceStates"][0]["InstanceId"].should.equal( instances[0]["InstanceId"]) instances_health["InstanceStates"][0]["State"].should.equal("InService") instances_health["InstanceStates"][1]["InstanceId"].should.equal( instances[1]["InstanceId"]) instances_health["InstanceStates"][1]["State"].should.equal("Unknown")
def test_describe_instance_health_boto3(): elb = boto3.client('elb', region_name="us-east-1") ec2 = boto3.client('ec2', region_name="us-east-1") instances = ec2.run_instances(MinCount=2, MaxCount=2)['Instances'] lb_name = "my_load_balancer" elb.create_load_balancer( Listeners=[{ 'InstancePort': 80, 'LoadBalancerPort': 8080, 'Protocol': 'HTTP' }], LoadBalancerName=lb_name, ) elb.register_instances_with_load_balancer(LoadBalancerName=lb_name, Instances=[{ 'InstanceId': instances[0]['InstanceId'] }]) instances_health = elb.describe_instance_health(LoadBalancerName=lb_name, Instances=[{ 'InstanceId': instance['InstanceId'] } for instance in instances ]) instances_health['InstanceStates'].should.have.length_of(2) instances_health['InstanceStates'][0]['InstanceId'].\ should.equal(instances[0]['InstanceId']) instances_health['InstanceStates'][0]['State'].\ should.equal('InService') instances_health['InstanceStates'][1]['InstanceId'].\ should.equal(instances[1]['InstanceId']) instances_health['InstanceStates'][1]['State'].\ should.equal('Unknown')
def create_load_balancer(elb, instances): hc = HealthCheck( interval=20, healthy_threshold=3, unhealthy_threshold=5, target='HTTP:80/' ) ports = [(80,80,'http')] lb = elb.create_load_balancer('elb1','us-east-1c',ports) lb.configure_health_check(hc) lb.register_instances(instances)
def create_elb(): elb = boto.ec2.elb.connect_to_region("us-east-1") hc = boto.ec2.elb.HealthCheck(interval=20, timeout=5, unhealthy_threshold=2,healthy_threshold=10,target='HTTP:8080/upload') zones = ['us-east-1a'] ports = [(80,80,'http'),(8080,8080,'http')] try: new_elb = elb.create_load_balancer('ELB_demo',zones,ports) except: print "creating new elb failed" sys.exit(1) new_elb.configure_health_check(hc) return new_elb
from boto.ec2.autoscale import ScalingPolicy import boto.ec2.cloudwatch from boto.ec2.cloudwatch import MetricAlarm import boto.ec2 #Create an ELB that: #Redirects HTTP:80 requests from the load balancer to HTTP:80 on the instance #Record the DNS Name of the created ELB #Set up the page /heartbeat?username=<yourandrewid> as the health check page #If you remember from last week, a data center instance needs to be activated by passing it your username. This step ensures that the ELB passes your username to your data center instances. elb=boto.ec2.elb.connect_to_region('us-east-1') hc=HealthCheck(interval=5,healthy_threshold=2,timeout=2,unhealthy_threshold=2,target='HTTP:80/heartbeat?username=jianj') zones=['us-east-1a'] ports=[(80,80,'http')] lb=elb.create_load_balancer('jianLb',zones,ports) lb.configure_health_check(hc) lbdns=lb.dns_name print 'load banlancer dns name:%s'%(lbdns) #Create a Launch Configuration for the instances that will become part of the auto scaling group, with the following parameters: #AMI ID: ami-ec14ba84 #Instance Type: m3.medium #Detailed Monitoring: enabled autoscale=boto.ec2.autoscale.connect_to_region('us-east-1') #the client #lc lc=LaunchConfiguration(name='jianLaunchConfig',image_id='ami-3c8f3a54',key_name='jj',security_groups=['http'],instance_type='m3.medium',instance_monitoring=True) autoscale.create_launch_configuration(lc) print 'launch cofig created' #ag ag=AutoScalingGroup(group_name='jianGroup',load_balancers=['jianLb'],availability_zones=['us-east-1a'],launch_config=lc,min_size=2,max_size=4,connection=autoscale)
print regions elb = boto.ec2.elb.connect_to_region(CONNECT_REGION,aws_access_key_id=AWS_ACCESS_KEY,aws_secret_access_key=AWS_SECRET_KEY) # conn = boto.ec2.connect_to_region("us-east-1") list = elb.get_all_load_balancers() print list hc = HealthCheck( interval=15, target='HTTP:8080/upload' ) zones = [CONNECT_AVAILABILITY_ZONE] ports = [(80, 80, 'http'), (8080, 8080, 'http')] lb = elb.create_load_balancer('my-lb', zones, ports) lb.configure_health_check(hc) print lb.dns_name while throughput <=2000: reservation=conn.run_instances(AMI,key_name=KEY_NAME,instance_type=INSTANCE_TYPE,placement=CONNECT_AVAILABILITY_ZONE,security_groups=[security_group]) instance_cnt print reservation instance = reservation.instances[0] instance_id = instance.id print instance_id instance_ids.append(instance_id)