Last active
April 1, 2023 16:55
-
-
Save bluPhy/87e72e6e039611675ed8b0c5da59e222 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# This script list all AMIs in use in all regions in an account | |
# Author: [email protected] | |
# Check if AWS CLI is installed | |
if ! command -v aws &> /dev/null; then | |
echo "AWS CLI could not be found" | |
exit | |
fi | |
# Check if AWS CLI is configured | |
if ! aws sts get-caller-identity &> /dev/null; then | |
echo "AWS CLI is not configured" | |
exit | |
fi | |
# List all AMIs in use in all regions | |
for region in $(aws ec2 describe-regions --output text | awk '/REGION/ {print $NF}' | sort); do | |
echo "Listing region: $region" | |
AMIs="" # Reset AMIs | |
for AMI in $(aws ec2 describe-instances --query "Reservations[*].Instances[*].{ImageId:ImageId}" --output=text --region "$region" | sort -u); do | |
if [ -n "$AMI" ]; then | |
AMIs+=" $AMI" | |
fi | |
done | |
if [ -n "$AMIs" ]; then | |
printf "\nListing AMIs:%s in region:%s\n" "$AMIs" "$region" | |
aws ec2 describe-images --filters "Name=state,Values=available" "Name=image-type,Values=machine" --image-ids $AMIs --query "Images[*].{ImageId:ImageId,Location:ImageLocation,Name:Name}" --output=table --region "$region" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment