简体   繁体   English

Boto3 获取具有 ebs 卷 ID、大小为 excel 的所有 ec2 实例的列表

[英]Boto3 get list of all ec2 instances with ebs volume id, size to an excel

Export AWS EC2 details to xlsx/csv using boto3 and python - This works but to gather EBS volumes, type and size attached to each ec2 instance and append to the same line in the excel is challenging for me. 使用 boto3 和 python 将 AWS EC2 详细信息导出到 xlsx/csv - 这可行,但收集附加到每个 ec2 实例的 EBS 卷、类型和大小以及 append 到 excel 中的同一行对我来说是一个挑战。 Below one, just appends the volume info in the next line.在一个下方,只需在下一行中附加音量信息。 If I have a separate function and call it in "result.append", I could fetch only the first volume.如果我有一个单独的 function 并在“result.append”中调用它,我只能获取第一卷。 If I return multiple values in the function like volume id, volume size, volume type - I could add all 3 of these values to the same cell in the excel, instead of a separate column for each.如果我在 function 中返回多个值,如卷 ID、卷大小、卷类型 - 我可以将所有 3 个值添加到 excel 中的同一个单元格,而不是每个单独的列。 Please help.请帮忙。 I'm obviously in learning phase.我显然处于学习阶段。

                volume_iterator = ec3.volumes.all()
                for v in volume_iterator:
                    for a in v.attachments:
                        if a['InstanceId'] == each['InstanceId']:
                                result.append({
                                    'volume.id': v.id,
                                    'volume.size': v.size,
                                    'volume.state': v.volume_type
                                })

Final output in CSV looks like below. CSV 中的最终 output 如下所示。 All the volume related values are in the same column "volume.id".所有与音量相关的值都在同一列“volume.id”中。 Volume info should be separated.卷信息应该分开。

ImageId InstanceType    InstanceId  InstanceName volume.id                  volume.type volume.size

ami-042e828f5df03   t3.large    i-07db6118eb51e <server_name>   [{8, 'vol-0085fdebc7', 'gp3'}, {'vol-0d417698824e', 'gp3', 128}]    

This works.这行得通。


import boto3
import csv
import datetime
import logging
from os import environ
import collections
import time
import sys

### ENABLE The profilename below, while testing from local. Disable this and session line in 63, enable line 64 session before pushing to Lambda#######
profilename='<>'
aws_Acct='<>.csv'
volume_id_list=[]
result = []
regions = ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']
#regions = ['us-east-1']

#Name Tag
def get_tag(each, tag_name):
    if 'Tags' in each:
        for tag in each['Tags']:
          if tag['Key'] == tag_name:
              return tag['Value']
    return ''

#Volumes
def get_vol(each, ec2):
    resultVol = {
        "vol_id": "",
        "vol_size": "",
        "vol_type": ""
    }
    resp = ec2.describe_volumes(
        Filters=[{'Name':'attachment.instance-id','Values':[each['InstanceId']]}]
    )
    for volume in (resp["Volumes"]):
        resultVol['vol_id'] += (str(volume["VolumeId"]) + "\n")
        resultVol['vol_size'] += (str(volume["Size"]) + "\n")
        resultVol['vol_type'] += (str(volume["VolumeType"]) + "\n")

    return resultVol

#Security Groups
def sec_gp(each, ec2):
    resultSG = {
        "sg_id": "",
        "sg_name": ""
    }
    for sg in each['SecurityGroups']:
        resultSG['sg_id'] += (str(sg["GroupId"]) + "\n")
        resultSG['sg_name'] += (str(sg["GroupName"]) + "\n")

    return resultSG

def lambda_handler(event, context):
    try:
        logging.basicConfig(level=logging.INFO)
        logging.info('EC2 Inventory details')

        for region in regions:
            session = boto3.Session(profile_name=profilename, region_name=region)
            #session = boto3.Session(region_name=region)
            ec2 = session.client('ec2')
            response = ec2.describe_instances()
            for item in response["Reservations"]:
                for each in item['Instances']:
                    volsss = get_vol(each, ec2)
                    sgss = sec_gp(each, ec2)
                    #print(sgss)
                    result.append({
                        'ImageId': each.get('ImageId', ''),
                        'InstanceType': each.get('InstanceType', ''),
                        'PublicIp': each.get('PublicIpAddress', ''),
                        'PrivateIp': each.get('PrivateIpAddress', ''),
                        'InstanceId': each.get('InstanceId', ''),
                        'SubnetId': each.get('SubnetId', ''),
                        'VpcId': each.get('VpcId', ''),
                        'InstanceName': get_tag(each, 'Name'),
                        'volume.size': volsss['vol_size'],
                        'volume.id': volsss['vol_id'],
                        'volume.type': volsss['vol_type'],
                        'DeleteOnTermination': each.get('DeleteOnTermination', ''),
                        'SGGroupName': sgss['sg_name'],
                        'SGGroupID': sgss['sg_id'],
                        'State': each['State']['Name'],
                        'Region': each['Placement']['AvailabilityZone']
                    })
                    
        # Write to csv file.
        header = ['ImageId', 'InstanceType', 'InstanceId', 'InstanceName', 'PublicIp', 'PrivateIp', 'Region', 'State', 'volume.id', 'volume.size', 'volume.type', 'SubnetId', 'VpcId', 'SGGroupName', 'SGGroupID', 'DeleteOnTermination']
        with open(aws_Acct, 'w') as file:
            writer = csv.DictWriter(file, fieldnames=header)
            writer.writeheader()
            writer.writerows(result)

    except Exception as e:
        logging.error(
            'EC2 inventory with uncaught exception: {}'.format(e)
        )

if __name__ == '__main__':
    lambda_handler(None, None)

Final output looks like:最终 output 看起来像:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM