Skip to content

Instantly share code, notes, and snippets.

@stevehodgkiss
Created April 14, 2022 06:47
Show Gist options
  • Save stevehodgkiss/f3475c7ab587632da771a1e929288bb2 to your computer and use it in GitHub Desktop.
Save stevehodgkiss/f3475c7ab587632da771a1e929288bb2 to your computer and use it in GitHub Desktop.
Add s3 bucket tags with aws-cli, bash & jq
#!/bin/bash
additional_tag_set='[{"Key": "my-tag", "Value": "true"}]'
declare -a buckets=(
a-bucket
another-bucket
)
for bucket in "${buckets[@]}"; do
taggings=$(aws s3api get-bucket-tagging --bucket "$bucket" 2> /dev/null)
if [ "$taggings" == "" ]; then
taggings="{\"TagSet\": []}"
fi
tag_set=$(echo "$taggings" | jq --compact-output ".TagSet = (.TagSet + $additional_tag_set | unique)")
echo "aws s3api put-bucket-tagging --bucket $bucket --tagging '$tag_set'"
done
# pipe to sh to execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment