Skip to content

Instantly share code, notes, and snippets.

@amosshapira
Last active March 22, 2024 16:00
Show Gist options
  • Save amosshapira/8df5e76be82eb774e8fd7ae22363fcee to your computer and use it in GitHub Desktop.
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
#!/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)
#!/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
@amosshapira
Copy link
Author

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