#!/bin/bash # Initialize counters for the summary report TOTAL_AL2=0 TOTAL_NOT_AL2=0 TOTAL_UNKNOWN=0 # Get a list of all enabled regions REGIONS=$(aws ec2 describe-regions --query "Regions[].RegionName" --output text) for REGION in $REGIONS; do echo "--- Region: $REGION ---" # Get all instance IDs in the current region INSTANCES=$(aws ec2 describe-instances --region "$REGION" --query "Reservations[].Instances[].InstanceId" --output text) if [ -z "$INSTANCES" ]; then echo "no instance found" continue fi for INSTANCE_ID in $INSTANCES; do echo -n "Instance $INSTANCE_ID: " # Capture console output OUTPUT=$(aws ec2 get-console-output --region "$REGION" --instance-id "$INSTANCE_ID" --query "Output" --output text) if [ -z "$OUTPUT" ] || [ "$OUTPUT" == "None" ]; then echo "cannot determine (Empty output)" ((TOTAL_UNKNOWN++)) continue fi # NESTED LOGIC: Handle Amazon Linux branding overlap if echo "$OUTPUT" | grep -qiE "Amazon Linux|\.amzn"; then # Sub-check: Is it actually AL2023? if echo "$OUTPUT" | grep -qiE "Amazon Linux 2023|\.amzn2023"; then echo "NOT running Amazon Linux 2 (Detected AL2023)" ((TOTAL_NOT_AL2++)) # Sub-check: Confirm it is indeed AL2 elif echo "$OUTPUT" | grep -qiE "Amazon Linux 2|\.amzn2\."; then echo "RUNNING Amazon Linux 2" ((TOTAL_AL2++)) else echo "cannot determine (Generic Amazon Linux string)" ((TOTAL_UNKNOWN++)) fi # Check for other known Operating Systems elif echo "$OUTPUT" | grep -qiE "Red Hat|Enterprise Linux|\.el[0-9]|Ubuntu|Debian|CentOS|Fedora|SLES|SUSE"; then echo "NOT running Amazon Linux 2" ((TOTAL_NOT_AL2++)) # Fallback for everything else else echo "cannot determine the operating system" ((TOTAL_UNKNOWN++)) fi done done # Output the final report echo -e "\n" echo "========================================" echo " AL2 EOL MIGRATION SUMMARY " echo "========================================" echo "Total AL2 Instances (Action Required): $TOTAL_AL2" echo "Total Non-AL2 Instances (Safe): $TOTAL_NOT_AL2" echo "Total Unknown (Needs Review): $TOTAL_UNKNOWN" echo "========================================"