Tag S3 Bucket using cli

Anju
Apr 12, 2023

--

for bucket in `aws s3api list-buckets --query "Buckets[].Name" --profile <profile_name> --output table| grep test | awk '{print $2}'`; do aws s3api put-bucket-tagging --bucket $bucket --tagging 'TagSet=[{Key=service_id,Value=111}, {Key=Environment, Value=test}]'  --profile <profile_name>; done

Get Tags for each bucket:

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

--

--

Anju
Anju

Written by Anju

A DevOps engineer who loves automating everything (almost), exploring new places, and finding peace in nature. Always looking for the next adventure!

No responses yet