Last active
December 12, 2019 19:20
-
-
Save frankitox/440c9d758ecd428e8fcd46163c588995 to your computer and use it in GitHub Desktop.
Tag all your S3 buckets without loosing previous tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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