Last active
March 22, 2024 16:00
-
-
Save amosshapira/8df5e76be82eb774e8fd7ae22363fcee to your computer and use it in GitHub Desktop.
Find self-owned AMI's which are not used by any instances or launch configurations in a region
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/bash | |
# Find self-owned AMI's which are not used by any instances or launch configurations in the default region. | |
comm -23 \ | |
<(aws ec2 describe-images --owner self --query 'Images[].[ImageId]' --output text | sort) \ | |
<((aws ec2 describe-instances --query 'Reservations[].Instances[].[ImageId]' --output text; | |
aws autoscaling describe-launch-configurations --query 'LaunchConfigurations[].[ImageId]' --output text) | sort -u) |
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/bash | |
# Find self-owned AMI's which are not used by any instances or launch configurations in the default region. | |
# Output with details in descending order of name and creation date. | |
aws ec2 describe-images --image-ids \ | |
$(comm -23 \ | |
<(aws ec2 describe-images --owner self --query 'Images[].[ImageId]' --output text | sort) \ | |
<((aws ec2 describe-instances --query 'Reservations[].Instances[].[ImageId]' --output text; \ | |
aws autoscaling describe-launch-configurations --query 'LaunchConfigurations[].[ImageId]' --output text) | \ | |
sort -u | \ | |
tr '\n' ' ')) \ | |
--query 'Images[].[ImageId,Name,CreationDate]' \ | |
--output text | \ | |
sort -t $'\t' -k 2 -k 3.1,3.4nr -k 3.6,3.7nr -k 3.9,3.10nr -k 3.12,3.13nr -k 3.15,3.16nr -k 3.18,3.19nr |
Thank you very much for the update. I'm not currently in a position to test this but your improvement could make sense.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I appreciate these are quite old scripts, but I have just come across them wanting to use something similar to trim some "AMI sprule"!
For the find-unused-amis-with-details script, having line 11 (tr command) as part of the clause, means AMIs which are in use also get reported. Simply removing the tr utility out of the command chain, then the returned list is actually correct again.
I have then been able to view the results of this, and curate a list of AMIs which are definitely "orphaned" and can be safely given the flick...
Just an FYI for anyone else who finds these scripts in the future...