A Clojure library for accessing Amazon EC2, based on the official AWS Java SDK and borrowing heavily from James Reeves's clj-aws-s3 library.
This is a very early development version. Currently the library only supports functions to list/describe EC2 reservations, instances and machine images (AMIs), and to start and stop EBS-backed instances. See TODO for future plans.
Add the following dependency to your project.clj
file:
[clj-aws-ec2 "0.5.0"]
(require '[aws.sdk.ec2 :as ec2])
(def cred {:access-key "...", :secret-key "..."})
(ec2/describe-instances cred)
(ec2/describe-instances cred (ec2/instance-id-filter "i-deadcafe"))
(ec2/describe-instances cred (ec2/instance-filter (ec2/aws-filter "instance-state-name" "running")
(ec2/aws-filter "tag-key" "role")))
(ec2/describe-images cred (ec2/image-owner-filter "self"))
(ec2/describe-images cred (ec2/image-id-filter "ami-3c47a355"))
(ec2/describe-images cred (ec2/image-filter (ec2/aws-filter "tag-key" "latest")
(ec2/aws-filter "tag-key" "webserver")))
(ec2/start-instances cred "i-beefcafe")
(ec2/stop-instances cred "i-beefcafe" "i-deadbabe")
(ec2/run-instances cred { :min-count 1
:max-count 1
:image-id "ami-9465dbfd"
:instance-type "t1.micro"
:key-name "my-key" })
(ec2/terminate-instances cred "i-beefcafe" "i-deadbabe")
(ec2/create-image cred { :instance-id "i-deadbabe"
:name "web-snapshot"
:description "Snapshot of web server" })
(ec2/deregister-image cred "ami-07f9756e")
(ec2/describe-tags cred (ec2/tag-filter-by-resource-id "id-babecafe"))
(ec2/describe-tags cred (ec2/tag-filter-by-resource-type "image"))
(ec2/create-tags cred ["id-deadcafe", "ami-9465dbfd"] {:name "web server" :owner "ops"})
(ec2/delete-tags cred ["id-deadcafe"] {:owner nil})
(ec2/describe-volumes cred)
(ec2/describe-volumes cred (ec2/volume-id-filter "vol-98765432"))
(ec2/describe-volumes cred (ec2/volume-snapshot-filter "snap-abcd1234"))
(ec2/describe-volumes cred (ec2/volume-status-filter :available)))
Refer to the
API docs
for a more detailed example of the parameters available to
run-instances
.
To use a region other than us-east-1
you can specify an API endpoint
in the credentials map:
(def cred {:access-key "...", :secret-key "...", :endpoint "ec2.ap-southeast-2.amazonaws.com"})
Refer to Regions and Endpoints for a list of current EC2 endpoints.
You can catch exceptions and extract details of the error condition:
(try
(ec2/start-instances cred "i-beefcafe")
(catch Exception e (ec2/decode-exception e)))
ec2/decode-exception
provides a map with the following keys:
:error-code
:error-type
:service-name
:status-code
- instance-block-device now has private-ip-address (thanks @jonpither)
- use default credential provider chain if available (thanks @christianblunden)
- upgrade AWS SDK to 1.8.9.1 (thanks @christianblunden)
- introduce describe-volumes
- instance-filter and image-filter can now accept multiple filter specs
- describe-instances now returns public-dns and block-device-mappings (thanks Ronen Narkis)
- Bump version number to be more "semantic" (http://semver.org/)
- Make the describe-tags API more consistent with other describe-* operations
- Simplify decode-exception
- FIX: Don't map hyphens<->underscores in tag names. Always return tag names as strings (not keywords).
- Introduced describe, create and delete tags
- FIX: correctly handle numeric attributes where AWS is expecting an Integer (not Long)
- Introduced create/deregister images
- FIX: run-instances now correctly handles its arguments
- FIX: run-instances now gracefully handles missing parameters
- Introduced run-instances and terminate-instances
- Added support for regions
- Introduced function to decode AWS exceptions
- Introduced stop/start instances
- Introduce describe-images
- Initial release. Can only describe-instances.
Copyright (C) 2012-2013 Michael Rowe
Distributed under the Eclipse Public License, the same as Clojure.