Skip to content

Instantly share code, notes, and snippets.

@josuemb
Created December 7, 2022 19:11
Show Gist options
  • Save josuemb/1cbecb5ba1f3c3c41f9792c93b4d5c10 to your computer and use it in GitHub Desktop.
Save josuemb/1cbecb5ba1f3c3c41f9792c93b4d5c10 to your computer and use it in GitHub Desktop.
Get all AWS resources in the account using AWS CLI.
#!/bin/bash
#NOTES:
# Before using this shell you must configure your aws cli to access your account.
# See more on: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html
# If you want to store all resources into a text variable you can do something like:
# resources_text=$(./get-all-aws-resources.sh)
# If you want to store all resources into a array variable you can do something like:
# resources_array=($(./get-all-aws-resources.sh))
# If you want to store resources count into variable you can do something like:
# resources_count=${#resources_array[@]}
# Check if aws cli is installed
if ! [ "$(command -v aws)" ]; then
echo 'Error: aws cli is not installed.' >&2
echo 'See: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html' >&2
exit 1
fi
# Get all enabled regions for current account
enabled_regions=$(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text)
# Check if last command was successfull
if [ $? -ne 0 ];
then
exit 1
fi
# Iterate over all enabled regions
for current_region in $enabled_regions
do
# Return ARNS (https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html) for all found resources
aws resourcegroupstaggingapi get-resources --region $current_region --query "ResourceTagMappingList[].{Name:ResourceARN}" --output text
if [ $? -ne 0 ];
then
exit 1
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment