Skip to content

Instantly share code, notes, and snippets.

@nitrocode
Last active September 23, 2020 00:21
Show Gist options
  • Select an option

  • Save nitrocode/b6da03f103f3a0b42220c6fd057102f6 to your computer and use it in GitHub Desktop.

Select an option

Save nitrocode/b6da03f103f3a0b42220c6fd057102f6 to your computer and use it in GitHub Desktop.
Tag S3 buckets with their bucket name for cost analysis

Tag S3 Buckets with their name

The AWS Cost Explorer tool cannot separate the S3 bucket resource expenditures by name so we can tag them with their own bucket name.

TODO: If possible, convert to a cloud custodian policy.

wget https://gist.githubusercontent.com/nitrocode/b6da03f103f3a0b42220c6fd057102f6/raw/tag-buckets.sh
chmod +x tag-buckets.sh
./tag-buckets.sh
#!/bin/bash
# get buckets
aws s3 ls > all-buckets.txt
# get bucket names
cat all-buckets.txt| cut -d' ' -f3- > all-bucket-names.txt
# create directory
mkdir -p buckets/
# run through all buckets and tag them with bucket_name
cat all-bucket-names.txt | while read bucket; do
# print current bucket
echo "Bucket: $bucket";
# check if bucket has tags
if aws s3api get-bucket-tagging --bucket $bucket > buckets/$bucket.tags.json; then
# if tags then test if bucket_name already exists and continue to next one
[[ `cat buckets/$bucket.tags.json | jq '.TagSet[] | select(.Key=="bucket_name") | length' | wc -l` -gt 0 ]] && continue
else
# if no tags, then give it an empty set
echo "{}" > buckets/$bucket.tags.json;
fi
# append the tags with bucket_name=$bucket locally
jq --arg bucket $bucket --monochrome-output --compact-output '.TagSet[.TagSet | length] |= . + {"Key": "bucket_name", "Value": $bucket}' buckets/$bucket.tags.json > buckets/$bucket.tags.appended.json;
# append the bucket's tags
aws s3api put-bucket-tagging --bucket $bucket --tagging file://buckets/$bucket.tags.appended.json;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment