     (do ~@body) 
     (throw+ {:type ::aws:missing-id :message "Instance id not found"}))) 
 
+(defn image-id [machine]
+  (hypervisor :aws :ostemplates (machine :os) :ami))
+
 (defn handle-volumes 
    "attached and waits for ebs volumes" 
-   [{:keys [image-id volumes]} endpoint instance-id]
-  (when (= (image-desc endpoint image-id :root-device-type) "ebs")
+   [{:keys [aws machine] :as spec} endpoint instance-id]
+  (when (= (image-desc endpoint (image-id machine) :root-device-type) "ebs")
     (wait-for-attach endpoint instance-id [10 :minute]))
   (let [zone (instance-desc endpoint instance-id :placement :availability-zone)]
-    (doseq [{:keys [device size]} volumes]
+    (doseq [{:keys [device size]} (aws :volumes)]
       (let [{:keys [volumeId]} (with-ctx ebs/create-volume size zone)]
         (wait-for {:timeout [10 :minute]} #(= "available" (with-ctx ebs/state volumeId))
            {:type ::aws:ebs-volume-availability :message "Failed to wait for ebs volume to become available"})

        {:type ::aws:ebs-volume-availability :message "Failed to wait for ebs volume to become available"})
     (with-ctx ebs/delete-volume (ebs :volume-id))))

(defconstrainedrecord Instance [endpoint spec user]
  "An Ec2 instance"
  [(provider-validation spec) (-> endpoint nil? not)]
  Vm
  (create [this] 
    (let [{:keys [aws]} spec instance-id (-> (with-ctx ec2/run-instances (dissoc aws :volumes)) :instances first :id)]
       (s/partial-system (spec :system-id) {:aws {:instance-id instance-id}})
       (debug "created" instance-id)
       (handle-volumes aws endpoint instance-id)    
       (when-let [ip (get-in spec [:machine :ip])] 
         (debug (<<  "Associating existing ip ~{ip} to instance-id"))
          (with-ctx eip/assoc-pub-ip instance-id ip))


(import-logging)

(defn creds [] (dissoc (hypervisor :aws) :ostemplates))

(defn wait-for-status [instance req-stat timeout]
  "Waiting for ec2 machine status timeout is in mili"
