Skip to content

Instantly share code, notes, and snippets.

@Klaudioz
Last active June 10, 2020 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Klaudioz/2477501add39cb3a5c04dd7e7ade62d9 to your computer and use it in GitHub Desktop.
Save Klaudioz/2477501add39cb3a5c04dd7e7ade62d9 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Get list of S3 buckets from AWS and cleaning the output
s3_bucket_list=$(aws s3api list-buckets --query "Buckets[].Name" | sed -e 's/[][]//g' -e 's/”//g' -e 's/,//g' -e '/^$/d' -e 's/^[ \t]*//;s/[ \t]*$//' -e 's/"//g')
# Loop through the list of S3 buckets and tag them
for bucket in $(echo "$s3_bucket_list")
do
echo "Tagging: ${bucket}"
aws s3api put-bucket-tagging --bucket "$bucket" --tagging "TagSet=[{Key=aws-cost-explorer,Value=$bucket}]"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment