Skip to content

Instantly share code, notes, and snippets.

@nonara
Created October 6, 2023 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nonara/d176ed2bb6cfed53fdb42c0ec20e9f4d to your computer and use it in GitHub Desktop.
Save nonara/d176ed2bb6cfed53fdb42c0ec20e9f4d to your computer and use it in GitHub Desktop.
AWS resource scanner
#!/bin/bash
##
## Scan for resources across multiple regions in AWS account
##
## Usage:
##
## All us regions:
## aws-scan.sh
##
## Specified regions:
## sh aws-scan.sh --region us-west-1 --region us-east-2
##
# Default regions
default_regions=("us-west-1" "us-west-2" "us-east-1" "us-east-2")
# Initialize empty array for specified regions
specified_regions=()
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--region)
specified_regions+=("$2")
shift
shift
;;
*)
shift
;;
esac
done
# Decide which regions to use
if [ ${#specified_regions[@]} -eq 0 ]; then
regions=("${default_regions[@]}")
else
regions=("${specified_regions[@]}")
fi
printf "\nListing AWS Resources by Region"
# List S3 Buckets
printf "\n\n==== S3 Buckets ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
for bucket in $(aws s3api list-buckets --query "Buckets[].Name" --output text); do
bucket_region=$(aws s3api get-bucket-location --bucket "$bucket" --query 'LocationConstraint' --output text)
if [ "$bucket_region" == "None" ]; then
bucket_region="us-east-1"
fi
if [ "$bucket_region" == "$region" ]; then
printf "\n S3: %s" "$bucket"
fi
done
done
# List RDS Instances and Aurora Clusters
printf "\n\n==== RDS Instances and Aurora Clusters ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
aws rds describe-db-instances --region "$region" --query 'DBInstances[].DBInstanceIdentifier' --output text | tr '\t' '\n' | while read -r instance; do
printf "\n RDS: %s", "$instance"
done
aws rds describe-db-clusters --region "$region" --query 'DBClusters[].DBClusterIdentifier' --output text | tr '\t' '\n' | while read -r cluster; do
printf "\n Aurora Cluster: %s", "$cluster"
done
done
# List Lambda Functions
printf "\n\n==== Lambda Functions ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
aws lambda list-functions --region "$region" --query 'Functions[].FunctionName' --output text | tr '\t' '\n' | while read -r function; do
printf "\n Lambda: %s" "$function"
done
done
# List API Gateways
printf "\n\n==== API Gateways ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
aws apigateway get-rest-apis --region "$region" --query 'items[].name' --output text | tr '\t' '\n' | while read -r api; do
printf "\n API Gateway: %s", "$api"
done
done
# List Cognito User Pools
printf "\n\n==== Cognito User Pools ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
aws cognito-idp list-user-pools --max-results 20 --region "$region" --query 'UserPools[].Name' --output text | awk '{printf "\n Cognito User Pool: %s", $1}'
done
# List ECS Clusters
printf "\n\n==== ECS Clusters ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
aws ecs list-clusters --region "$region" --query 'clusterArns[]' --output text | tr '\t' '\n' | while read -r cluster; do
cluster_name=$(echo "$cluster" | awk -F '/' '{print $2}')
printf "\n ECS: %s", "$cluster_name"
done
done
# List CloudFront Distributions
printf "\n\n==== CloudFront Distributions ===="
aws cloudfront list-distributions --query 'DistributionList.Items[].Id' --output text | tr '\t' '\n' | while read -r distribution; do
printf "\n CloudFront: %s", "$distribution"
done
# List Route53 Domains
printf "\n\n==== Route53 Domains ===="
aws route53 list-hosted-zones --query 'HostedZones[].Name' --output text | tr '\t' '\n' | while read -r domain; do
printf "\n Route53 Domain: %s", "$domain"
done
# List Certificates
printf "\n\n==== Certificates ===="
aws acm list-certificates --query 'CertificateSummaryList[].CertificateArn' --output text | tr '\t' '\n' | while read -r certificate; do
certificate_id=$(echo "$certificate" | awk -F '/' '{print $2}')
printf "\n Certificate: %s", "$certificate_id"
done
# List Amplify Apps
printf "\n\n==== Amplify Apps ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
aws amplify list-apps --region "$region" --query 'apps[].name' --output text | tr '\t' '\n' | while read -r app; do
printf "\n Amplify: %s", "$app"
done
done
# List CloudWatch Alarms
printf "\n\n==== CloudWatch Alarms ===="
for region in "${regions[@]}"; do
printf "\n\n-- %s --" "$region"
aws cloudwatch describe-alarms --region "$region" --query 'MetricAlarms[].AlarmName' --output text | tr '\t' '\n' | while read -r alarm; do
printf "\n CloudWatch Alarm: %s", "$alarm"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment