2017-03-17 17 views

回答

0

這是我的解決辦法我得到它的工作。謝謝約翰。

elbList = boto3.client('elb') 
ec2 = boto3.resource('ec2') 

bals = elbList.describe_load_balancers() 
for elb in bals['LoadBalancerDescriptions']: 
    print 'ELB DNS Name : ' + elb['DNSName'] 
    for ec2Id in elb['Instances']: 
     running_instances = \ 
      ec2.instances.filter(Filters=[{'Name': 'instance-state-name' 
           , 'Values': ['running']}, 
           {'Name': 'instance-id', 
           'Values': [ec2Id['InstanceId']]}]) 
     for instance in running_instances: 
      print("    Instance : " + instance.public_dns_name); 
2

經典負載平衡器

的boto3 describe_load_balancers()函數返回實例的列表:

{ 
    'LoadBalancerDescriptions': [ 
     { 
      'LoadBalancerName': 'string', 
      'DNSName': 'string', 
      .... 
      'Instances': [ 
       { 
        'InstanceId': 'string' 
       }, 
      ], 
      .... 
     }, 
    ], 
    'NextMarker': 'string' 
} 

Instances部返回負載平衡器的實例的ID。

應用負載平衡器(ELBv2)

這一個是硬。應用程序負載平衡器有多個目標組。實例上的端口已註冊到目標組。

似乎List實例在目標集團的唯一命令是describe_target_health(),它返回實例端口(因爲一個實例可以服務於多個目標):

{ 
    'TargetHealthDescriptions': [ 
     { 
      'Target': { 
       'Id': 'i-0f76fade', 
       'Port': 80, 
      }, 
      'TargetHealth': { 
       'Description': 'Given target group is not configured to receive traffic from ELB', 
       'Reason': 'Target.NotInUse', 
       'State': 'unused', 
      }, 
     }, 
     { 
      'HealthCheckPort': '80', 
      'Target': { 
       'Id': 'i-0f76fade', 
       'Port': 80, 
      }, 
      'TargetHealth': { 
       'State': 'healthy', 
      }, 
     }, 
    ], 
    'ResponseMetadata': { 
     '...': '...', 
    }, 
} 
+1

@Sam嗨,如果這或任何答案有通過點擊複選標記解決您的問題,請考慮[接受它](http://meta.stackexchange.com/q/5234/179419)。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 –

0

這是一個簡單的腳本找到實例連接到ELB的列表,輸入您可能必須提供ELB名稱

#!/usr/bin/python 

import boto3 
import sys 
import string 


elb_name = raw_input("What is ELB name? :: ") 

print ("\n") 
print ("THE LIST OF INSTANCES ATTACHED TO THIS ELB IS \n") 
elbList = boto3.client('elb') 
ec2 = boto3.resource('ec2') 

bals = elbList.describe_load_balancers() 
for elb in bals['LoadBalancerDescriptions']: 

    set2 = elb['LoadBalancerName'] 
    if elb_name == set2 : 
     inst = elb['Instances'] 
     ct = sys.getrefcount(inst) 
     count = ct 
     for x in range(count): 
      iv = elb['Instances'][x] 
      id = str(iv.values()) 
      id = string.replace(id, "'", "") 
      print id.strip('[]') 
相關問題