简体   繁体   中英

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. 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. 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. 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. All the volume related values are in the same column "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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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