Skip to content

Commit 4f2444a

Browse files
committed
snapshots list and deletions
1 parent ce8f8f0 commit 4f2444a

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

boto3_eg/delete_snapshot_tags.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import boto3
2+
from pprint import pprint
3+
4+
# Getting OwnerID for AWS account
5+
6+
aws_mgt_con = boto3.session.Session(profile_name="default",region_name="ap-south-1")
7+
sts_con = aws_mgt_con.client(service_name="sts",region_name="ap-south-1")
8+
response = sts_con.get_caller_identity()
9+
accountId = response.get('Account')
10+
11+
# EC2
12+
ec2 = boto3.resource('ec2',region_name="ap-south-1")
13+
filters = { 'Name': 'tag:DeleteOn', 'Values': ['45'] }
14+
15+
# Get list of snapshot from your AWS account based on the filters defined.
16+
17+
snapshot_deletion = []
18+
19+
snapshot_iterator = ec2.snapshots.filter(Filters=[filters], OwnerIds=[accountId])
20+
for each_snap in snapshot_iterator:
21+
snapshot_deletion.append(each_snap.id)
22+
23+
print("List of snapshot for deletion: ", snapshot_deletion)
24+
25+
# Delete snapshots for those which have retention period of 45 days
26+
27+
for each_snap in snapshot_deletion:
28+
snapshot = ec2.Snapshot(each_snap)
29+
response = snapshot.delete(each_snap, DryRun=False)
30+
print('snapshots deleted ..', snapshot_deletion)

boto3_eg/list_snapshots_tags.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import boto3
2+
from pprint import pprint
3+
4+
# Getting OwnerID for AWS account
5+
6+
aws_mgt_con = boto3.session.Session(profile_name="default",region_name="ap-south-1")
7+
sts_con = aws_mgt_con.client(service_name="sts",region_name="ap-south-1")
8+
response = sts_con.get_caller_identity()
9+
accountId = response.get('Account')
10+
11+
# EC2
12+
ec2 = boto3.resource('ec2',region_name="ap-south-1")
13+
filters = { 'Name': 'tag:DeleteOn', 'Values': ['45'] }
14+
15+
# Get list of snapshot from your AWS account based on the filters defined.
16+
17+
snapshot_iterator = ec2.snapshots.filter(Filters=[filters], OwnerIds=[accountId])
18+
print("List of all snapshots ownerby you: ")
19+
for each_snap in snapshot_iterator:
20+
print(each_snap.id)

0 commit comments

Comments
 (0)