Skip to content

Instantly share code, notes, and snippets.

@downspot
Last active April 22, 2018 19:55
Embed
What would you like to do?
EC2 AMI retention
#!/bin/sh
date=`date +%Y-%m-%d -d "30 day ago"`
for region in us-east-1 us-west-2 ; do
aws ec2 describe-images --region $region --owners self --output text --filters 'Name=name,Values=IMAGE-NAME-*' --query 'Images[?CreationDate<=`'${date}'`][ImageId]' | grep -v `aws ec2 describe-instances --region $region --output text --filters 'Name=tag:Name,Values=IGNORE-STRING-*' --query 'Reservations[*].Instances[*].[ImageId]' | sed -n 1p` > remove_ami-$region.txt
for remove in `cat remove_ami-$region.txt` ; do
aws ec2 deregister-image --region $region --image-id $remove
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment