[无法使用python boto3获取所有aws实例-保留问题

问题描述 投票:0回答:2

我已经编写了python-boto3脚本来从帐户和区域获取所有aw实例列表。

脚本运行正常,但未提供所有实例。

例如,如果n个实例具有相同的保留编号,则在保留下仅获得一个实例。

请看下面的脚本,请帮我如何获得所有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 = []
python-3.x amazon-web-services instance boto3
2个回答
0
投票

在查看您的代码时,您尝试执行的操作并不明显,但是以下代码遍历所有实例并显示其标记:

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'])

请注意,InstanceIdState是实例对象上的可用目录。它们不是标签。


0
投票

请检查下面更新的代码,但仍然无法正确获取所有实例详细信息(如果n个实例具有相同的保留编号,则只能在保留编号下获取一个实例)

请检查并帮助我。

   output = "Prod.csv"    
     ec = boto3.client('ec2', config=config, region_name=rg)

        def get_tags():
            tag_list = []
            resp =  ec.describe_instances()
            tag_result = [['Name','InstanceId','State','bt:product','bt:environment-type','bt:environment-name']]
            for ec2 in resp['Reservations']:
                tag_list = []
                try:
                    ecName = next(item for item in ec2['Instances'][0]['Tags'] if item['Key'] == 'Name')['Value']
                    tag_list.append(ecName)
                except:
                    ecName = 'Null'
                    tag_list.append(ecName)
                insId = ec2['Instances'][0]['InstanceId']
                instanceID = str(insId)
                tag_list.append(instanceID)
                state = ec2['Instances'][0]['State']['Name']
                tag_list.append(state)
                try:
                    btprod = next(item for item in ec2['Instances'][0]['Tags'] if item['Key'] == 'bt:product')['Value']
                    tag_list.append(btprod)
                except:
                    btprod = 'Null'
                    tag_list.append(btprod)
                try:
                    btenvtype = next(item for item in ec2['Instances'][0]['Tags'] if item['Key'] == 'b1:environment-type')['Value']
                    tag_list.append(btenvtype)
                except:
                    btenvtype = 'Null'
                    tag_list.append(btenvtype)
                try:
                    btenvname = next(item for item in ec2['Instances'][0]['Tags'] if item['Key'] == 'b1:environment-name')['Value']
                    tag_list.append(btenvname)
                except:
                    btenvname = 'Null'
                    tag_list.append(btenvname)
                tag_result.append(tag_list)
            return tag_result
        def main():

            tag = get_tags()
            with open(output,'w') as resultFile:
                wr = csv.writer(resultFile, dialect='excel')
                wr.writerows(tag)
                log = open(output, 'r').read()
© www.soinside.com 2019 - 2024. All rights reserved.