Skip to content

Instantly share code, notes, and snippets.

@hisrarul
Created April 25, 2020 06:36
Show Gist options
  • Save hisrarul/4be36432870f410ce1c9c302152e47c5 to your computer and use it in GitHub Desktop.
Save hisrarul/4be36432870f410ce1c9c302152e47c5 to your computer and use it in GitHub Desktop.
Calculate the cost of data stored in AWS S3 Bucket

Chapter 1: Calculate the cost of data stored in AWS S3.

Scenario: There are so many buckets which we are consuming for data stroage. Some of these buckets are costing huge amount to us. Now, we need to find out which bucket is costing more amount so we can plan for data archive.

Problem statement

We need to create the excel sheet which will contain the name of the bucket and the storage occupied by the files in those bucket. The s3 service charges is $0.25 per GB so accordingly we can apply formula in our excel sheet.

Solution

  • Configure AWS CLI on your machine with ACCESS KEY and SECRET ACCESS KEY.
  • Make sure that user have required permission if not then attach an IAM policy.
  • Create a shell script on your your linux machine.
cat > s3Usage.sh

Paste the below content and hit Enter

for bucket in $(aws s3 ls --profile default | awk '{print $3}')
do
bucketsize=$(aws s3 ls --summarize --recursive s3://$bucket --profile default | grep 'Total Size' | awk '{print $3}')
echo $bucket,$bucketsize >> s3usage.csv
done
echo "Your file has been saved at $PWD/s3usage.csv"

You can open csv file in your excel and update accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment