-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2_snapshots.py
More file actions
77 lines (55 loc) · 2.4 KB
/
ec2_snapshots.py
File metadata and controls
77 lines (55 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import json
import sys
import boto3
from utils.json_encoder import json_encoder
from utils.json_writer import json_writer
from utils.json_printer import json_printer
from utils.session import get_session
from utils.regions import get_all_regions
from utils.boto_error_handling import yield_handling_errors
def get_snapshots(ec2_client):
paginator = ec2_client.get_paginator('describe_snapshots')
response_iterator = paginator.paginate(DryRun=False,
OwnerIds=['self'],
PaginationConfig={'MaxItems': 5000, 'PageSize': 100})
for snapshots_page in response_iterator:
snapshots = snapshots_page['Snapshots']
for snapshot in snapshots:
perms = ec2_client.describe_snapshot_attribute(Attribute='createVolumePermission',
SnapshotId=snapshot['SnapshotId'])['CreateVolumePermissions']
# The permissions for a snapshot are specified using the
# createVolumePermission attribute of the snapshot.
#
# To make a snapshot public, set the group to all.
# To share a snapshot with a specific AWS account,
# set the user to the ID of the AWS account.
#
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-modifying-snapshot-permissions.html
snapshot['CreateVolumePermissions'] = perms
if perms:
print('Shared snapshot found: %s !' % snapshot['SnapshotId'])
shapshot['shared'] = True
else:
shapshot['shared'] = False
yield snapshot
def main():
session = get_session()
all_data = {}
for region in get_all_regions(session):
ec2_client = session.client('ec2', region)
all_data[region] = {}
print('Processing region: %s' % region)
iterator = yield_handling_errors(get_snapshots, ec2_client)
iterator = enumerate(iterator)
for i, snapshot in iterator:
all_data[region][i] = snapshot
sys.stdout.write('.')
sys.stdout.flush()
if all_data[region]:
print('\n')
os.makedirs('output', exist_ok=True)
json_writer('output/ec2_snapshots.json', all_data)
json_printer(all_data)
if __name__ == '__main__':
main()