Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete leftover Datomic resources

Delete Leftover Datomic Cloud Resources

If you delete a Datomic Cloud system, it will leave some resources in AWS. To entirely delete all resources created by Datomic Cloud, you must follow these steps. Because we love automation, this script performs all these steps for you so you don't need to navigate the AWS Console UI.

Prerequisites

Usage

Simply replace your-datomic-system-name in the below bash command.

clojure -Sdeps '{:deps {delete {:git/url "https://gist.github.com/kennyjwilli/55007211070a260044c8e6abcb54dd5b" :sha "eb0262673a05bdb13d2bcd56d36cc9e90eca6e50"}}}' -m delete-datomic-system your-datomic-system-name
(ns delete-datomic-system
(:require
[clojure.string :as str]
[clojure.java.shell :as sh]
[clojure.data.json :as json]))
(defn json-cmd
([args] (json-cmd args nil))
([args opts]
(let [r (apply sh/sh (concat args (mapcat identity opts)))]
(if (= 0 (:exit r))
(json/read-str (:out r))
(println "Error:" (:err r))))))
(defn parse-arn
[arn-str]
(let [parts (drop 1 (str/split arn-str #":" 6))]
(zipmap [:partition :service :region :account-id :resource] parts)))
;; Delete docs
;; https://docs.datomic.com/cloud/operation/deleting.html#deleting-storage
(defn delete-s3-bucket
[s3-bucket]
(sh/sh "aws" "s3" "rb" (str "s3://" s3-bucket) "--force"))
(defn delete-ddb-table
[table-name]
(sh/sh "aws" "dynamodb" "delete-table" "--table-name" table-name))
(defn delete-efs
[fs-id]
(sh/sh "aws" "efs" "delete-file-system" "--file-system-id" fs-id))
(defn deregister-dynamo-scalable-target
[datomic-system]
(let [deregister (fn [dimension]
(sh/sh "aws" "application-autoscaling" "deregister-scalable-target"
"--service-namespace" "dynamodb"
"--scalable-dimension" dimension
"--resource-id" (format "table/datomic-%s" datomic-system)))]
(deregister "dynamodb:table:WriteCapacityUnits")
(deregister "dynamodb:table:ReadCapacityUnits")))
(defn get-datomic-resource-arns
[system-name]
(let [r (json-cmd ["aws" "resourcegroupstaggingapi" "get-resources" "--tag-filters"
(format "Key=datomic:system,Values=%s" system-name)])]
(when r
(map (comp parse-arn #(get % "ResourceARN")) (get r "ResourceTagMappingList")))))
(defn delete-leftover-datomic-resources-from-arns
[system-name parsed-arns]
(let [resources-by-service (group-by :service parsed-arns)]
;; delete s3 buckets
(doseq [{:keys [resource]} (get resources-by-service "s3")]
(println "Deleting S3 bucket" resource)
(delete-s3-bucket resource))
;; delete dynamo tables
(doseq [{:keys [resource]} (get resources-by-service "dynamodb")
:let [[_ table-name] (str/split resource #"\/" 2)]]
(println "Deleting ddb table" table-name)
(delete-ddb-table table-name))
;; delete efs
(doseq [{:keys [resource]} (get resources-by-service "elasticfilesystem")
:let [[_ fs-id] (str/split resource #"\/" 2)]]
(println "Deleting efs " fs-id)
(delete-efs fs-id))
(println "Deregistering dynamodb application-autoscaling")
(deregister-dynamo-scalable-target system-name)))
(defn delete-leftover-datomic-system-resources
[system-name]
(->> (get-datomic-resource-arns system-name)
(delete-leftover-datomic-resources-from-arns system-name)))
(defn system-exit!
"Exit the process."
[status message]
(when (and message (not (str/blank? message)))
(binding [*out* (if (= 0 status) *out* *err*)]
(println message)))
(System/exit status))
(defn -main
[& args]
(let [datomic-system-name (first args)]
(if datomic-system-name
(do (delete-leftover-datomic-system-resources datomic-system-name)
(shutdown-agents))
(system-exit! 1 "Missing datomic system name."))))
{:paths ["."]
:deps {org.clojure/data.json {:mvn/version "0.2.6"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.