1+ import boto3
2+ import collections
3+ import datetime
4+ import os
5+
6+ ec = boto3 .client ('ec2' )
7+
8+ def lambda_handler (event , context ):
9+
10+ # Get Current Region
11+ aws_region = os .getenv ('AWS_REGION' )
12+
13+ # Get All SnapShots With 'Offsite' Tag Set
14+ snapshots = ec .describe_snapshots (
15+ Filters = [
16+ { 'Name' : 'tag-key' , 'Values' : ['DestinationRegion' ] },
17+ ]
18+ )
19+
20+ for snapshot in snapshots ['Snapshots' ]:
21+
22+ # Reset Our Destination Region
23+ destination_region = None
24+
25+ # Obtain Tags From Source SnapShot
26+ for tag in snapshot ['Tags' ]:
27+
28+ # Obtain Destination Region From Source Snapshot Tag
29+ if tag ['Key' ] == 'DestinationRegion' :
30+ destination_region = tag ['Value' ]
31+
32+ # Check If We Need To Do A Copy Or Not
33+ if destination_region == aws_region :
34+
35+ print "\t Destination Region %s is the same as current region (%s) - skipping copy" % (
36+ destination_region ,
37+ aws_region
38+ )
39+
40+ continue
41+
42+ # Construct ec2 Client For Secondary Region
43+ secondary_ec = boto3 .client ('ec2' , region_name = destination_region )
44+
45+ # Determine If There's An Off-Site Copy Of This SnapShot
46+ os_snapshots = secondary_ec .describe_snapshots (
47+ Filters = [
48+ { 'Name' : 'tag:SourceSnapshotId' , 'Values' : [snapshot ['SnapshotId' ]] },
49+ { 'Name' : 'status' , 'Values' : ['pending' ,'completed' ] },
50+ ]
51+ )
52+
53+ # We Only Want To Delete Where Snapshot Has Copied Successfully
54+ if len (os_snapshots ['Snapshots' ]) >= 1 :
55+
56+ snapshot_states = [d ['State' ] for d in os_snapshots ['Snapshots' ]]
57+
58+ if 'pending' in snapshot_states :
59+ print "\t There is at least 1 Snapshot copy pending in %s - skipping delete & copy" % (
60+ destination_region
61+ )
62+
63+ continue
64+
65+ print "\t \t Found a corresponding Snapshot with Id %s in %s created from Snapshot %s" % (
66+ os_snapshots ['Snapshots' ][0 ]['SnapshotId' ],
67+ destination_region ,
68+ snapshot ['SnapshotId' ]
69+ )
70+
71+ print "Deleting source Snapshot %s from %s" % (
72+ snapshot ['SnapshotId' ],
73+ aws_region
74+ )
75+
76+ ec .delete_snapshot (
77+ DryRun = True ,
78+ SnapshotId = snapshot ['SnapshotId' ]
79+ )
80+
81+ continue
82+
83+ # Create Copy Of Snapshot Because One Doesn't Exist
84+ os_snapshot = secondary_ec .copy_snapshot (
85+ SourceRegion = aws_region ,
86+ SourceSnapshotId = snapshot ['SnapshotId' ],
87+ Description = snapshot ['Description' ],
88+ DestinationRegion = destination_region
89+ )
90+
91+ # If Snapshot Copy Executed Successfully, Copy The Tags
92+ if (os_snapshot ):
93+
94+ print "\t \t Snapshot copy %s created in %s of %s from %s" % (
95+ os_snapshot ['SnapshotId' ],
96+ destination_region ,
97+ snapshot ['SnapshotId' ],
98+ aws_region
99+ )
100+
101+ # Add Tags To Off-Site SnapShot
102+ destination_snapshot_tags = snapshot ['Tags' ] + [{ 'Key' : 'SourceSnapshotId' , 'Value' : snapshot ['SnapshotId' ] }]
103+ secondary_ec .create_tags (
104+ Resources = [os_snapshot ['SnapshotId' ]],
105+ Tags = destination_snapshot_tags
106+ )
107+
108+ # Add Tags To Source SnapShot
109+ ec .create_tags (
110+ Resources = [snapshot ['SnapshotId' ]],
111+ Tags = [
112+ { 'Key' : 'OffsiteSnapshotId' , 'Value' : os_snapshot ['SnapshotId' ] },
113+ ]
114+ )
0 commit comments