LoginSignup
0
0

More than 5 years have passed since last update.

AWS del ami/snapshot

Last updated at Posted at 2017-03-31

via ssm (bash)

export list=$(aws ec2 describe-images --owner self --query 'Images[][ImageId]' --output text | xargs)

for i in ${list[@]}
do
  aws ssm start-automation-execution --document-name AWS-DeleteImage --parameters ImageId=$i
done

tag:Name filter --filter "Name=tag:Name,Values=xxxx-001"

via boto3

./list-ami.py > list
./del-ami.py < list
list-ami.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import boto3
import sys

client = boto3.client('ec2')


def _list_ami():

    _filters = [
        {'Name': 'tag-key', 'Values': ['hoge']},
        {'Name': 'tag-value', 'Values': ['fuga']}
        ]

    ret = client.describe_images(
        Filters=_filters
    )

    # pirnt snapshot
    for img in ret['Images']:
        print img['ImageId']

def main():
    _list_ami()


if __name__ == '__main__':
    main()

del-ami.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import boto3
import sys

client = boto3.client('ec2')


def _del_ami(ami_id):
    ret = client.describe_images(ImageIds=[ami_id])

    # deregister_ami
    print "del-ami : " + ami_id
    client.deregister_image(ImageId=ami_id)

    # delete snapshot
    for snap in ret['Images'][0]['BlockDeviceMappings']:
        if 'Ebs' in snap:
            print "del-snap: " + snap['Ebs']['SnapshotId']
            client.delete_snapshot(SnapshotId=snap['Ebs']['SnapshotId'])


def main():
    for line in sys.stdin:
        _del_ami(line.rstrip())


if __name__ == '__main__':
    main()


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0