简体   繁体   中英

How to get list of all the ebs snapshots available in account with python boto using paginator

I am trying to get list of all the available snapshots in AWS account. I have written following code.

def lambda_handler(event, context):
  ec2 = boto3.client('ec2')
  resp_describe_snapshots = ec2.describe_snapshots(OwnerIds=['self'])
  snapshot =  resp_describe_snapshots['Snapshots']

  snapshots = {}
  os.chdir('/tmp')

  for snapshotIdList in resp_describe_snapshots['Snapshots']:
    snapid =  snapshotIdList.get('SnapshotId')
    Des = snapshotIdList.get('Description')
    sttime = snapshotIdList.get('StartTime').strftime("%b %d %Y")

    #print(snapshotIdList.get('SnapshotId')+ ","+ snapshotIdList.get('Description')+ ","+ snapshotIdList.get('StartTime').strftime("%b %d %Y"))    

  for response in ec2.get_paginator('describe_snapshots').paginate(OwnerIds=['self'],Filters=[{'Name': 'tag:Name','Values': ['Ubuntu']}]):
    snapshots.update([(snapshot['SnapshotId'], snapshot) for snapshot in response['Snapshots']])
    print(snapshots)

How can I list all the snapshots ID and it's creation date ?

I also want to create a csv file.

Here is my example of pagination.

import boto3

boto3 = boto3.session.Session(region_name='ap-northeast-2')
ec2 = boto3.client('ec2')

page_iterator = ec2.get_paginator('describe_snapshots').paginate()

for page in page_iterator:
    for snapshot in page['Snapshots']:
        print(snapshot['SnapshotId'], snapshot['StartTime'])

The result is

snap-5... 2015-MM-dd 18:21:59+00:00
snap-e... 2016-MM-dd 22:15:38+00:00
snap-e... 2016-MM-dd 22:15:46+00:00
snap-e... 2017-MM-dd 12:59:00+00:00
snap-e... 2016-MM-dd 20:39:12+00:00
snap-8... 2017-MM-dd 13:04:34+00:00
snap-7... 2017-MM-dd 17:28:49+00:00
snap-b... 2016-MM-dd 14:52:38+00:00
snap-b... 2016-MM-dd 21:18:23+00:00
snap-d... 2016-MM-dd 07:43:38+00:00
snap-2... 2015-MM-dd 00:07:19+00:00
snap-b... 2017-MM-dd 22:03:26+00:00
snap-0... 2016-MM-dd 00:32:35+00:00
snap-c... 2016-MM-dd 21:57:05+00:00
snap-7... 2016-MM-dd 04:10:45+00:00
...

where the number of results are over 10,000.


To make csv,

import boto3
import csv

boto3 = boto3.session.Session(region_name='ap-northeast-2')
ec2 = boto3.client('ec2')

with open('snapshots.csv', 'w') as csvfile: 
    fields = ['SnapshotId', 'StartDate'] 
    writer = csv.writer(csvfile)
    writer.writerow(fields) 

    page_iterator = ec2.get_paginator('describe_snapshots').paginate()

    for page in page_iterator:
        for snapshot in page['Snapshots']:
            temp = [snapshot['SnapshotId'], snapshot['StartTime']]
            writer.writerows([temp])

csvfile.close()

To update csv into S3 directly,

import boto3
import csv
import io

boto3 = boto3.session.Session(region_name='ap-northeast-2')
ec2 = boto3.client('ec2')
s3 = boto3.client('s3')

fields = ['SnapshotId', 'StartDate'] 
csvio = io.StringIO()
writer = csv.writer(csvio)
writer.writerow(fields) 

page_iterator = ec2.get_paginator('describe_snapshots').paginate()

for page in page_iterator:
    for snapshot in page['Snapshots']:
        temp = [snapshot['SnapshotId'], snapshot['StartTime']]
        writer.writerows([temp])


s3.put_object(Body=csvio.getvalue(), ContentType='application/vnd.ms-excel', Bucket='bucket', Key='snapshot.csv') 
csvio.close()

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