11

I have multiple AWS accounts and I need to list all S3 buckets per account and then view each buckets total size.

Currently, I can only view the storage size of a single S3 bucket with:

aws s3 ls s3://mybucket --recursive --human-readable --summarize
11

Resolution 1

So I solved this with the following script. I originally posted the question just in case there was an easier way that I was not aware of.

#!/bin/bash
aws_profile=('profile1' 'profile2' 'profile3');

#loop AWS profiles
for i in "${aws_profile[@]}"; do
  echo "${i}"
  buckets=($(aws --profile "${i}" --region your_region s3 ls s3:// --recursive | awk '{print $3}'))

  #loop S3 buckets
  for j in "${buckets[@]}"; do
  echo "${j}"
  aws --profile "${i}" --region your_region s3 ls s3://"${j}" --recursive --human-readable --summarize | awk END'{print}'
  done

done

Resolution 2

Using Dashboards in CloudWatch in the AWS console.

You can then simply specify all S3 buckets and add the numbers stats to show the storage size metrics.

This won't cost you plenty of API calls and can be significantly faster depending on the size of the s3 buckets(takes quite awhile to get the size on very large buckets).

Verdict

Creating the Dashboard (Resolution 2) on each AWS account was the most efficient option for me cause it is way quicker for me to log in and grab the metrics manually from each AWS account than to wait for the scripts API calls to finish. :(

| improve this answer | |
  • Why | awk END'{print}' ? – Tensibai Oct 4 '17 at 14:57
  • 1
    Using the recursive option shows the size of every folder and file and I only need the output of the total size of the bucket. – Kyle Steenkamp Oct 4 '17 at 15:08
  • You need to select a '1 day' or greater period for the dashboard to display anything – Jeremy Leipzig Jul 16 '18 at 17:38
  • 2
    Beware that the solution proposed in Resolution 2 increments your Cloudwatch cost since dashboards have a $3 cost each at the moment. aws.amazon.com/cloudwatch/pricing/?nc1=h_ls – Drubio Mar 6 '19 at 17:52
  • What does the [@] in "${buckets[@]}" mean? – Joe Mar 21 at 20:14
3

You will need to write a script that parses and queries this data because as far as I'm aware, there is not a tool or cli function that performs this. Luckily, you can gather all of this information with the CLI.

  1. List and parse all of the accounts in your org.

    aws organizations list-accounts
    
  2. For each account, list and parse all of the buckets.

    aws s3api list-buckets --query "Buckets[].Name"
    
  3. Finally, get the size of each bucket within each account. You can use the same cli command you were before, but be warned that you are going to be listing the individual size of each item within the bucket. You can also use this cli command to get bucket size.

    aws s3api list-objects --bucket BUCKETNAME --output json --query "
    [sum(Contents[].Size), length(Contents[])]"
    
| improve this answer | |
  • 1
    1. does not list the profile information of the AWS account. I did not have time to regex this from the aws config file where this information is stored so I just hardcoded the values in the script i posted below – Kyle Steenkamp Oct 4 '17 at 14:44
2

You're going to have to write a script to do this unless there's a tool to do it that I can't find.

If you're working with multiple aws accounts you'll need to be switching between accounts to get the bucket sizes.

To get all of the buckets and their total size you can use 'aws s3api'. Using list-buckets you can get all of the buckets in one account and then you'll be able to list their sizes and try the next account.

| improve this answer | |
1

None of these show the size of the bucket if Versioning is turned on and massive amounts of data have been "deleted". In the S3 GUI console, the Metrics tab shows what you are being billed for.

Scraped the following command out of a script found at https://serverfault.com/questions/84815/how-can-i-get-the-size-of-an-amazon-s3-bucket . It works well on Windows. Substitute yourregion and yourbucketname:

aws cloudwatch get-metric-statistics --namespace AWS/S3 --start-time 2019-05-09T00:01:00Z --end-time 2019-05-11T23:59:00Z --period 86400 --statistics Average --region yourregion --metric-name BucketSizeBytes --dimensions Name=BucketName,Value=yourbucketname Name=StorageType,Value=StandardStorage

This gives the average size in bytes over the days bounded by the start and end times. The result can shock you if your bucket has Versioning Enabled (active) and you think you have "deleted" massive amounts of data.

To clean up Versioning-enabled buckets, you must set a LifeCycle rule. You can choose to permanently delete Previous Versions. You may omit the checkboxes at the Transitions step.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.