Am trying to get all the S3 buckets with the environment and service tag. I have almost 200 buckets...How can i do that...Pls help me

aws s3api list-buckets 

The above command listing all the bucket name with the creation date...How can i list all the bucket with the specific tags. I searched so many blogs but i couldn't get the exact command to get the bucket with the tags.I tried using with the jq command like

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

am getting the following error:

Invalid numeric literal at line 2, column 0

My output for aws s3api list-buckets

2019-01-24T04:53:07.000Z | aws-xxx |||| 2019-02-18T18:33:15.000Z | aws-yyy 
4

Best Answer


You should be using the "get-bucket-tagging" api to get the tags associated with your bucket.

https://docs.aws.amazon.com/cli/latest/reference/s3api/get-bucket-tagging.html

I've found a simple script in github for doing this job . I hope this works for you.

https://gist.github.com/filipenf/0528d26f0dba78b72b39

If you goal is to find resources (eg Amazon EC2 instance, Amazon S3 buckets) that have specific tags, please look at What Is AWS Resource Groups?:

You can use resource groups to organize your AWS resources. Resource groups make it easier to manage and automate tasks on large numbers of resources at one time. This guide shows you how to create and manage resource groups in AWS Resource Groups.

I couldn't find a sensible way to do this, I don't think you can even search by tags in the UI.

But you can do something like

s3_buckets_with_cache() {local S3CACHE JSONBUCKETS bucketwithtags tagsS3CACHE="${DIR_CACHE}/s3buckets.json"if [[ ! -f ${S3CACHE} ]]; thenJSONBUCKETS='{"buckets":[]}'IFS=$'\n'for bucket in $(aws s3api list-buckets | jq .Buckets[].Name -r); dotags=$(aws s3api get-bucket-tagging --bucket "${bucket}" 2>/dev/null)# shellcheck disable=SC2181[[ "$?" -ne 0 ]] && continuetags=$(echo "${tags}" | jq -c '.[][] | {(.Key): .Value}' | jq --slurp .)[[ "${tags}" == "[]" ]] && continuebucketwithtags="{\"bucket\": \"${bucket}\", \"tags\" : ${tags} }"JSONBUCKETS=$(echo "${JSONBUCKETS}" | jq ".buckets += [${bucketwithtags}]")doneecho "${JSONBUCKETS}" >"${S3CACHE}"ficat "${S3CACHE}"}

And then call that with

AWS_BUCKET_NAME=$(s3_buckets_with_cache | jq -r '.buckets[] | select(.tags[].foo == "bar" and .tags[].abc == "xyz") | .bucket')

Although this does not have cache invalidation or anything like that but you could easily check the timestamp of the file and invalidate it that way.

And I only added the cache because I had to crawl 3000 buckets for their tags and I don't want to do that every time I need to get a bucket name by tag.

You can do this by AWS resource-groups CLI

Example: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/resource-groups/search-resources.html#examples

But the web UI seems more powerful which consists of link to each resource and export option to csv.https://console.aws.amazon.com/resource-groups/tag-editor/find-resources