Skip to content

Instantly share code, notes, and snippets.

@frankitox
Last active December 12, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankitox/440c9d758ecd428e8fcd46163c588995 to your computer and use it in GitHub Desktop.
Save frankitox/440c9d758ecd428e8fcd46163c588995 to your computer and use it in GitHub Desktop.
Tag all your S3 buckets without loosing previous tags
#!/usr/bin/env bb
;; Run it using [babashka](https://github.com/borkdude/babashka)
;; > bb -f tag-buckets.clj
;; Related info: https://stackoverflow.com/questions/52645443/how-to-add-tags-to-an-s3-bucket-without-deleting-the-existing-tags-using-boto3
(defn parse [str]
(json/parse-string str true))
(defn print [data]
(println (json/generate-string data {:pretty true})))
(defn s3api [action bucket]
(shell/sh "aws" "s3api" action "--bucket" bucket))
(defn tag-set [tagset]
(into {} (map (juxt :Key :Value)) (:TagSet tagset)))
(->> (-> (shell/sh "aws" "s3api" "list-buckets") :out parse :Buckets)
(map :Name)
(map (fn [bucket]
(let [{:keys [exit out err]} (shell/sh "aws" "s3api" "get-bucket-tagging" "--bucket" bucket)]
[bucket (if (zero? exit) (-> out parse tag-set) {})
(when (and err (not (str/includes? (or err "")
"The TagSet does not exist")))
(parse err))])))
(map (fn [[bucket old-tags error]]
(if error
[bucket old-tags nil error]
(let [new-tags (assoc old-tags "S3:Bucket" bucket)
tags (->> (assoc new-tags "S3:Bucket" bucket)
(map (fn [[k v]]
(str "{Key=" k ",Value=" v "}")))
(str/join ","))
{:keys [exit out err] :as d} (shell/sh "aws" "s3api" "put-bucket-tagging" "--bucket" bucket
"--tagging" (str "TagSet=[" tags "]"))]
[bucket old-tags (when (zero? exit) new-tags) err]))))
(reduce (fn [m [bucket old-tags new-tags error]]
(if (or (not= old-tags new-tags) (seq error))
(conj m [bucket (cond-> {:old-tags old-tags}
new-tags (assoc :new-tags new-tags)
(seq error) (assoc :error error))])
m)) [])
(into {}) (print))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment