Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Created September 4, 2024 20:05
Show Gist options
  • Save natemccurdy/83a0f3c5bf88be4be1e85bfd49016205 to your computer and use it in GitHub Desktop.
Save natemccurdy/83a0f3c5bf88be4be1e85bfd49016205 to your computer and use it in GitHub Desktop.
Show all S3 buckets and their tags in a single JSON object
#!/bin/bash
# This produces a JSON object on stdout where each key is an S3 bucket's name.
# Each value is an object of tag name/tag value pairs.
#
# For example:
# {
# "bucket1": {
# "name": "bucket1",
# "environment": "prod"
# },
# "bucket2": {
# "name": "bucket2",
# "environment": "dev"
# }
# }
#
# Temporary directory for storing intermediate results
temp_dir=$(mktemp -d)
# Clean up temporary files on exit
trap 'rm -rf "$temp_dir"' EXIT
# Fetch tags for a given bucket and store results in a file
get_bucket_tags() {
local bucket=$1
local result_file=$2
# Fetch the tags for the given bucket
tag_map=$(aws s3api get-bucket-tagging --bucket "medacist-sunset" --output json --query 'TagSet' | jq -r '. | from_entries // {}')
# Create the JSON object with the bucket name and its tags and write it to a temporary file
jq -n --arg bucket "$bucket" --argjson tags "$tag_map" '{($bucket): $tags}' >"$result_file"
}
# Fetch all buckets and process each in parallel
while read -r bucket; do
get_bucket_tags "$bucket" "$temp_dir/$bucket" &
done < <(aws s3api list-buckets --query "Buckets[].Name" --output json | jq -r '.[]')
# Wait for all background jobs to complete
wait
# Combine results into a single JSON object and output it
jq -s 'add' "$temp_dir"/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment