#!/bin/bash
Β # List of all AWS regions (as of 2024)
# Since we don't have ec2:DescribeRegions permission, we'll use a hardcoded list
regions=(
"us-east-1"
"us-east-2"
"us-west-1"
"us-west-2"
"af-south-1"
"ap-east-1"
"ap-south-1"
"ap-south-2"
"ap-northeast-1"
"ap-northeast-2"
"ap-northeast-3"
"ap-southeast-1"
"ap-southeast-2"
"ap-southeast-3"
"ap-southeast-4"
"ca-central-1"
"ca-west-1"
"eu-central-1"
"eu-central-2"
"eu-west-1"
"eu-west-2"
"eu-west-3"
"eu-south-1"
"eu-south-2"
"eu-north-1"
"il-central-1"
"me-south-1"
"me-central-1"
"sa-east-1"
)
Β echo "Checking EC2 instances across all regions..."
echo "Current AWS Identity:"
aws sts get-caller-identity
echo ""
Β # Counter for total instances
total_instances=0
Β # Loop through each region
for region in "${regions[@]}"; do
echo "Checking region: $region"
Β # Try to describe instances in the region
result=$(aws ec2 describe-instances \
--region "$region" \
--query "Reservations[*].Instances[*].[InstanceId, Tags[?Key=='Name'].Value|[0], State.Name, InstanceType, PrivateIpAddress, PublicIpAddress]" \
--output json 2>&1)
Β # Check if the command was successful
if [[ $? -eq 0 ]]; then
# Count instances in this region
instance_count=$(echo "$result" | jq 'flatten | length' 2>/dev/null || echo "0")
Β if [[ $instance_count -gt 0 ]]; then
echo "Found $instance_count instance(s) in $region:"
aws ec2 describe-instances \
--region "$region" \
--query "Reservations[*].Instances[*].[InstanceId, Tags[?Key=='Name'].Value|[0], State.Name, InstanceType, PrivateIpAddress, PublicIpAddress]" \
--output table
total_instances=$((total_instances + instance_count))
else
echo "No instances found in $region"
fi
else
# Check if it's a permission error or region doesn't exist/not enabled
if echo "$result" | grep -q "UnauthorizedOperation"; then
echo "ERROR: No permission to describe instances in $region"
elif echo "$result" | grep -q "AuthFailure"; then
echo "ERROR: Authentication failure for $region"
elif echo "$result" | grep -q "not subscribed\|not enabled\|not found"; then
echo "SKIP: Region $region not enabled for this account"
else
echo "ERROR in $region: $result"
fi
fi
echo "---"
done
Β echo ""
echo "Total instances found across all regions: $total_instances"
Comments
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π