Skip to content

Instantly share code, notes, and snippets.

@astaykov
Last active November 24, 2023 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astaykov/b8666cf7f730ee4e08451b1e62069bbb to your computer and use it in GitHub Desktop.
Save astaykov/b8666cf7f730ee4e08451b1e62069bbb to your computer and use it in GitHub Desktop.
list all resources in all available regions. filter by specific resource type. only for current account.
#!/bin/bash
## Define the output CSV file
output_file="aws_resources.csv"
## List of resource types to include in the report
resource_types=("ec2" "lambda" "ecs" "eks" "rds" "dynamodb" "elasticmapreduce" "kinesis" "elasticache")
## Check if the AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "AWS CLI is not installed. Please install it and try again."
exit 1
fi
# Get the list of available regions
regions=$(aws ec2 describe-regions --query 'Regions[*].RegionName' --output text)
# Create the header for the CSV file
echo "Region,ResourceType,ResourceId" > "$output_file"
# Loop through each region
for region in $regions; do
# Loop through each resource type
for resource_type in "${resource_types[@]}"; do
# Get the list of resources for the current type and region
case "$resource_type" in
"ec2")
resources=$(aws ec2 describe-instances --region "$region" --query 'Reservations[*].Instances[*].[InstanceId]' --output text)
;;
"lambda")
resources=$(aws lambda list-functions --region "$region" --query 'Functions[*].[FunctionName]' --output text)
;;
"ecs")
resources=$(aws ecs list-clusters --region "$region" --query 'clusterArns' --output text)
;;
"eks")
resources=$(aws eks list-clusters --region "$region" --query 'clusters' --output text)
;;
"rds")
resources=$(aws rds describe-db-instances --region "$region" --query 'DBInstances[*].[DBInstanceIdentifier]' --output text)
;;
"dynamodb")
resources=$(aws dynamodb list-tables --region "$region" --query 'TableNames' --output text)
;;
"elasticmapreduce")
resources=$(aws emr list-clusters --region "$region" --query 'Clusters[*].[Id]' --output text)
;;
"kinesis")
resources=$(aws kinesis list-streams --region "$region" --query 'StreamNames' --output text)
;;
"elasticache")
resources=$(aws elasticache describe-cache-clusters --region "$region" --query 'CacheClusters[*].[CacheClusterId]' --output text)
;;
*)
echo "Unknown resource type: $resource_type"
exit 1
;;
esac
# Print the results to the CSV file
# Print the results to the CSV file
# Loop through each resource and construct the CSV line
while IFS= read -r resource; do
if [ -n "$resource" ]; then
echo "$region,$resource_type,$resource" >> "$output_file"
fi
done <<< "$resources"
echo "$region, $resource_type" echo "$region, $resource_type"
done
done
echo "AWS resource collection completed. Results saved in: $output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment