Skip to content

Instantly share code, notes, and snippets.

@filipenf
Created April 22, 2015 18:46
Show Gist options
  • Save filipenf/0528d26f0dba78b72b39 to your computer and use it in GitHub Desktop.
Save filipenf/0528d26f0dba78b72b39 to your computer and use it in GitHub Desktop.
Print a list of aws buckets along with their tags
#!/bin/bash
# lists all buckets along with their tags in the following format:
# bucket_name | { tag_name: tag_value }
# depends on AWS CLI and JQ
for bucket in `aws s3api list-buckets | jq .Buckets[].Name | tr -d \"`; do
tags=$(aws s3api get-bucket-tagging --bucket $bucket | jq -c '.[][] | {(.Key): .Value}' | tr '\n' '\t')
echo $bucket '|' $tags
done
@bcorner13
Copy link

I know this is old.. But you don't need jq for the bucket list step. Also added some additional stuff to answer other questions.

for bucket in `aws s3api list-buckets --query "Buckets[].Name" --output text` ; do
    echo $bucket
    tags=$(aws s3api get-bucket-tagging --bucket $bucket 2>/dev/null| jq -cr '.[][] | {(.Key): .Value}')
    output=${tags:-"no Tags assigned"}
    echo $output
done

~

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