Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RezaAmbler/32a6f5f78c1851550c3bba352a490ecb to your computer and use it in GitHub Desktop.
Save RezaAmbler/32a6f5f78c1851550c3bba352a490ecb to your computer and use it in GitHub Desktop.
aws report ec2 instances and size
# script generates a CSV file listing details of all EC2 instances across AWS regions, including instance ID, type, associated EBS volume IDs,
# sizes, types, the instance name from its tags, and all other tags, outputting this information into a structured format.
echo "Region,Instance ID,Instance Type,Volume IDs,Volume Sizes,Volume Types,Instance Name,Tags" > /tmp/ec2-data.csv
aws ec2 describe-regions --query "Regions[].RegionName" --output text | tr '\t' '\n' | while read region; do
# Fetch volume details (including size and type) and store in a file
aws ec2 describe-volumes --region "$region" --query 'Volumes[]' --output json | jq -r 'reduce .[] as $item ({}; .[$item.VolumeId] = {"Size": ($item.Size | tostring), "Type": $item.VolumeType})' > /tmp/volumeDetails.json
# Fetch instance details and include volume size and type along with the instance name
aws ec2 describe-instances --region "$region" --query 'Reservations[].Instances[]' --output json | jq -r --arg region "$region" --slurpfile volumeDetails /tmp/volumeDetails.json '.[] | [$region, .InstanceId, .InstanceType, ([.BlockDeviceMappings[].Ebs.VolumeId] | join(", ")), ([.BlockDeviceMappings[].Ebs.VolumeId] | map(. as $volumeId | ($volumeDetails[0][$volumeId].Size)) | join(", ")), ([.BlockDeviceMappings[].Ebs.VolumeId] | map(. as $volumeId | ($volumeDetails[0][$volumeId].Type)) | join(", ")), (try (.Tags[] | select(.Key == "Name").Value) catch "N/A"), (.Tags | map(.Key + "=" + .Value) | join(", "))] | @csv' >> /tmp/ec2-data.csv
done
rm /tmp/volumeDetails.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment