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()