Created
July 11, 2018 02:07
-
-
Save 42milez/40c9c3017b75983640173ccee921c05f to your computer and use it in GitHub Desktop.
Create snapshots and delete old snapshots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import collections | |
import logging | |
import time | |
ec2 = boto3.client('ec2') | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
# lambdaファンクションのエントリーポイント | |
def lambda_handler(event, context): | |
volumes = create_snapshots() | |
delete_snapshots(volumes) | |
def create_snapshots(): | |
instances = describe_instances([ | |
{ | |
'Name': 'tag-key', | |
'Values': ['Auto Snapshot'] | |
}, | |
{ | |
'Name': 'tag-value', | |
'Values': ['Yes'] | |
} | |
]) | |
volumes = {} | |
for instance in instances: | |
tags = {t['Key']: t['Value'] for t in instance['Tags']} | |
generation = int(tags.get('Available Snapshots', 0)) | |
if generation < 1: | |
continue | |
for block_device in instance['BlockDeviceMappings']: | |
if block_device.get('Ebs') is None: | |
continue | |
volume_id = block_device['Ebs']['VolumeId'] | |
description = 'Auto Snapshot ' + '%s (%s)' % (volume_id, instance['RootDeviceName']) | |
snapshot = create_snapshot(volume_id, description, [{ | |
'ResourceType': 'snapshot', | |
'Tags': [ | |
{ | |
'Key': 'Name', | |
'Value': tags['Name'], | |
}, | |
{ | |
'Key': 'Rotation', | |
'Value': 'Yes', | |
} | |
] | |
}]) | |
logger.info('New snapshot: %s(%s)' % (snapshot['SnapshotId'], description)) | |
volumes[snapshot['VolumeId']] = generation | |
return volumes | |
def delete_snapshots(volumes): | |
auto_snapshots = describe_snapshots(list(volumes.keys())) | |
for volume_id, snapshots in list(auto_snapshots.items()): | |
num_snapshot = len(snapshots) | |
generation = volumes[volume_id] | |
delete_count = num_snapshot - generation | |
if delete_count <= 0: | |
continue | |
snapshots.sort(key=lambda x: x['StartTime']) | |
outdated_snapshots = snapshots[0:delete_count] | |
for snapshot in outdated_snapshots: | |
delete_snapshot(snapshot['SnapshotId']) | |
logger.info('delete snapshot %s(%s)' % (snapshot['SnapshotId'], snapshot['Description'])) | |
def describe_instances(filters): | |
reservations = ec2.describe_instances(Filters=filters)['Reservations'] | |
return sum([ | |
[i for i in r['Instances']] | |
for r in reservations | |
], []) | |
def describe_snapshots(volume_ids): | |
snapshots = ec2.describe_snapshots( | |
Filters=[ | |
{ | |
'Name': 'volume-id', | |
'Values': volume_ids, | |
}, | |
{ | |
'Name': 'tag-key', | |
'Values': [ | |
'Rotation' | |
], | |
}, | |
{ | |
'Name': 'tag-value', | |
'Values': [ | |
'Yes' | |
] | |
} | |
] | |
)['Snapshots'] | |
snapshot_groups = collections.defaultdict(lambda: []) | |
{snapshot_groups[snapshot['VolumeId']].append(snapshot) for snapshot in snapshots} | |
return snapshot_groups | |
def create_snapshot(volume_id, description, tags): | |
time.sleep(0.5) | |
return ec2.create_snapshot(VolumeId=volume_id, Description=description, TagSpecifications=tags) | |
def delete_snapshot(snapshot_id): | |
time.sleep(0.5) | |
return ec2.delete_snapshot(SnapshotId=snapshot_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment