Last active
April 22, 2018 19:55
-
-
Save downspot/dd01a3b8c350bfc2f7fa508a14b2d4f2 to your computer and use it in GitHub Desktop.
EC2 AMI retention
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
#!/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