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
@DarkMukke
Copy link

why do you mix back ticks and $() ?

for bucket in $(aws s3api list-buckets | jq .Buckets[].Name -r); do
    tags=$(aws s3api get-bucket-tagging --bucket $bucket | jq -c '.[][] | {(.Key): .Value}' | tr '\n' '\t')
    echo $bucket '|' $tags
done

@Debuntu
Copy link

Debuntu commented Apr 7, 2020

I tried this....but seems like AWS CLI doestnt support the command - "$aws s3api list-buckets | jq .Buckets[].Name"

@Wolfium
Copy link

Wolfium commented Apr 13, 2020

I tried this....but seems like AWS CLI doestnt support the command - "$aws s3api list-buckets | jq .Buckets[].Name"

What I think is that you need jq tool installed and available from the console where executing the command, as it is a simple command piped at the aws output result

@parekhdevang
Copy link

anyway to filter output to show only the buckets without any tags?

@RomanDanyk
Copy link

I tried this....but seems like AWS CLI doesn't support the command - "$aws s3api list-buckets | jq .Buckets[].Name"

Try this one:

aws s3api list-buckets | jq -r '.Buckets[].Name'

@DPeterK
Copy link

DPeterK commented Sep 16, 2020

anyway to filter output to show only the buckets without any tags?

You could try this:

for BUCKET in $(aws s3api list-buckets | jq .Buckets[].Name -r); do
    RESULT=$(aws s3api get-bucket-tagging --bucket $BUCKET 2>&1)
    if [[ $RESULT =~ "(NoSuchTagSet)" ]]; then
        echo $BUCKET
    fi
done

Note you have to capture the error text and test for it rather than being able to check the response code, as unfortunately aws s3api returns a success code even if the command errors:

$ aws s3api get-bucket-tagging --bucket $BUCKET

An error occurred (NoSuchTagSet) when calling the GetBucketTagging operation: The TagSet does not exist
$ echo $?
0

@AnuradhaWickramasinghe
Copy link

AnuradhaWickramasinghe commented Apr 28, 2021

update the above line and it will give you clear output
echo $bucket '|' $tags >> buckets-tags.txt

@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