-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutomateEBStoS3.py
66 lines (50 loc) · 1.92 KB
/
AutomateEBStoS3.py
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
# Task - Automated EBS Snapshots to S3 using AWS Lambda
import boto3
# Helper function to get all regions
def get_regions(ec2_client):
return [region['RegionName'] for region in ec2_client.describe_regions()['Regions']]
# Handler
def lambda_handler(event, context):
ec2_client = boto3.client('ec2')
# Get list of regions usign helper function
regions = get_regions(ec2_client)
# Iterate over regions
for region in regions:
print (f"Checking region: {region}")
reg = region
# Connect to region
ec2 = boto3.client('ec2', region_name=reg)
# Get all in-use volumes in regions
result = ec2.describe_volumes(
Filters=[{
"Name": 'status',
"Values": ['in-use']
}]
)
for volume in result['Volumes']:
print (f"Backing up {volume['VolumeId']} in {volume['AvailabilityZone']}")
# Create snapshot
result = ec2.create_snapshot(
VolumeId = volume['VolumeId'],
Description = "Created by Lambda backup function: ebs-snapshots"
)
# Get snapshot resource
ec2resource = boto3.resource('ec2', region_name=reg)
snapshot = ec2resource.Snapshot(result['SnapshotId'])
# Find name tag for volume if it exists
if 'Tags' in volume:
for tags in volume['Tags']:
if tags["Key"] == 'Name':
volumename = tags["Value"]
else:
volumename = 'N/A'
# Add volume name to snapshot for easy identification
snapshot.create_tags(Tags=[{
'Key': 'Name',
'Value': volumename
}]
)
return "Done"
'''
regions = ec2.describe_regions().get('Regions',[] )
'''