 
 (defn creds [] (get-in config [:hypervisor :aws]))
 
-(def ids (synched-map :aws-keys))
+(defm ids [] (synched-map :aws-keys))
 
-(defn wait-for-status [instance req-stat  {:keys [timeout] :or {timeout [5 :minute]}}]
-  "Waiting for ec2 machine status timeout is in mili"
+(defn wait-for [{:keys [timeout] :or {timeout [5 :minute]}} pred err]
+  "A general wait for pred function"
   (let [wait (+ (curr-time) (parse-time-unit timeout))]
     (loop []
       (if (> wait (curr-time))
-        (if (= req-stat (.status instance)) 
+        (if (pred) 
           true
           (do (Thread/sleep 2000) (recur))) 
-        (throw+ {:type ::aws:status-failed :message "Failed to wait for status" :status req-stat})))))
+        (throw+ err)))))
+
+(defn wait-for-status [instance req-stat timeout]
+  "Waiting for ec2 machine status timeout is in mili"
+  (wait-for timeout #(= req-stat (.status instance))
+    {:type ::aws:status-failed :message "Failed to wait for status" :status req-stat}))
 
 (defmacro ec2 [f & args]
- `(~f (assoc (creds) :endpoint ~'endpoint) ~@args))
+  `(~f (assoc (creds) :endpoint ~'endpoint) ~@args))
 
 (defn instance-id [uuid]
-   (if-let [id (@ids uuid)]
-     id
-     (throw+ 
-       {:type ::aws:missing-id :message 
-        "Instance id not found, make sure no to address deleted instances" :uuid uuid})
-     ))
+  (if-let [id (@(ids) uuid)]
+    id
+    (throw+ 
+      {:type ::aws:missing-id :message 
+       "Instance id not found, make sure no to address deleted instances" :uuid uuid})
+    ))
+
+(defn image-desc [ami & ks]
+  (-> (ec2/describe-images (creds) (ec2/image-id-filter ami))
+      first (apply ks)))
+
+(defn instance-desc [endpoint uuid & ks]
+  (-> (ec2 describe-instances (instance-id-filter (instance-id uuid)))
+      first :instances first (get-in ks)))
+
+(defn wait-for-attach [endpoint uuid timeout]
+  (wait-for timeout 
+    #(= "attached" (instance-desc endpoint uuid :block-device-mappings 0 :ebs :status)) 
+    {:type ::aws:ebs-attach-failed :message "Failed to wait for ebs root device attach"}))
 
 (defconstrainedrecord Instance [endpoint conf uuid]
   "An Ec2 instance, uuid used for instance-id tracking"

        (ec2 stop-instances  (instance-id uuid))
        (wait-for-status this "stopped" {:timeout [2 :minute]}))
  (status [this] 
          (-> (ec2 describe-instances (instance-id-filter (instance-id uuid)))
              first :instances first :state :name
              ))) 

(defmethod translate :aws [{:keys [aws machine]}] 
  [(aws :endpoint) (dissoc aws :endpoint) (UUID/randomUUID)])

  (:require [aws.sdk.ec2 :as ec2])
  (:import (java.util UUID))
  (:use 
    [flatland.useful.utils :only (defm)]
    [minderbinder.time :only (parse-time-unit)]
    [slingshot.slingshot :only  [throw+ try+]]
    [aws.sdk.ec2 :only 
