Created
June 13, 2017 08:34
-
-
Save komuro-hiraku/ccc89f1a477a46a34797d88ec510bbc0 to your computer and use it in GitHub Desktop.
Delete AMI and EBS Snapshot. CSV Format (`AMI_ID`, `SnapshotId`)
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
# create your ami list. Sorted by CreationDate ASC | |
aws ec2 describe-images --profile <Your Profile> --owners "AMI Owner Id" | jq -r '.Images[] | {imageId: .ImageId, snapshotId: .BlockDeviceMappings[].Ebs.SnapshotId, creationDate: .CreationDate} | [.imageId, .snapshotId, .creationDate] | @csv' | sort -t, -k3 |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import csv | |
import click | |
import time | |
import botocore | |
from boto3.session import Session | |
profile = "<your aws profile>" | |
session = Session(profile_name=profile) | |
ec2_client = session.client('ec2') | |
# オーナーIDを指定してAMIの情報取得 | |
def describe_images(owner_id): | |
"""Get AMI Information""" | |
response = ec2_client.describe_images( | |
Owners=[ | |
str(owner_id) | |
] | |
) | |
return response.get("Images", {}) | |
# AMI登録解除 | |
def unregister_ami(ami_id, is_dryrun=True): | |
"""Unregister EC2 AMI""" | |
response = ec2_client.deregister_image( | |
DryRun=bool(is_dryrun), | |
ImageId=str(ami_id) | |
) | |
return response.get('ResponseMetadata', {}).get('HTTPStatusCode', -1) == 200 | |
# Snapshot削除 | |
def delete_snapshot(snapshot_id, is_dryrun=True): | |
"""Delete snapshot""" | |
response = ec2_client.delete_snapshot( | |
SnapshotId=str(snapshot_id), | |
DryRun=bool(is_dryrun) | |
) | |
return response.get('ResponseMetadata', {}).get('HTTPStatusCode', -1) == 200 | |
# msのsleep | |
usleep = lambda x: time.sleep(x/1000.0) | |
def process_from_csv(file_name, is_dryrun): | |
"""Execute from CSV file""" | |
with open(str(file_name), 'r') as file: | |
reader = csv.reader(file) | |
for row in reader: | |
try: | |
unregistered = unregister_ami(row[0], bool(is_dryrun)) # unregister ami | |
deleted = delete_snapshot(row[1], bool(is_dryrun)) # delete snapshot | |
print("{ami_id}, {unregistered}, {deleted}".format(ami_id=row[0],unregistered=unregistered,deleted=deleted)) | |
usleep(1000) # sleep in milisec | |
except botocore.exceptions.ClientError as e: | |
print("{}, False, False, {}".format(row[0], e)) | |
usleep(1000) # sleep in milisec | |
@click.command() | |
@click.argument('file') | |
@click.option('--dry-run', '-D', type=bool, help='Dry Run', default=True) | |
def execute_unregister(file, dry_run): | |
"""Option parser""" | |
file_name = str(file) | |
process_from_csv(file_name, bool(dry_run)) | |
def main(): | |
"""main function""" | |
execute_unregister() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment