[英]not getting all aws instances using python boto3 - problem with Reservation
我編寫了一個 python-boto3 腳本來從帳戶和區域獲取所有 aws 實例列表。
該腳本運行良好,但未提供所有實例。
例如,如果 n 個實例具有相同的 Reservation 編號,則僅在 Reservation 下獲得一個實例。
請查看下面的腳本,請幫助我如何獲取所有 aws 實例列表,而不管預訂編號如何。
rg = 'us-west-2'
config = Config(
retries = dict(
max_attempts = 100
)
)
ec = boto3.client('ec2', config=config, region_name=rg)
def get_tags():
tag_list = []
resp = ec.describe_instances()['Reservations']
#resp = ec.describe_instances()
#print(resp)
tag_result = [['Name','InstanceId','State','t1:product','t1:environment-type','t1:environment-name']]
for ec2 in resp:
#for ec2 in resp["Reservation"]:
#print(InstanceId)
tag_list = []
在查看您的代碼時,您試圖做什么並不明顯,但這里有一些代碼遍歷所有實例並顯示它們的標簽:
import boto3
ec2_client = boto3.client('ec2')
response = ec2_client.describe_instances()
for reservation in response['Reservations']:
for instance in reservation['Instances']:
print(instance['InstanceId'])
for tag in instance['Tags']:
print(tag['Key'], tag['Value'])
下面是使用 boto3 資源方法的等效代碼:
import boto3
ec2_resource = boto3.resource('ec2')
for instance in ec2_resource.instances.all():
print(instance.id)
for tag in instance.tags:
print(tag['Key'], tag['Value'])
請注意, InstanceId
和State
是實例 object 上的可用目錄。它們不是標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.