Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 831c140883
Fetching contributors…

Cannot retrieve contributors at this time

150 lines (97 sloc) 3.694 kb
# Node EC2
Evended Node.js bindings to the EC2 Query API.
## Synopsis
Node EC2 is a thin layer over the Amazon Query API.
Because `node-ec2` is a thin layer over the Amazon Query API, you can use the
[Amazon API
Reference](http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?query-apis.html)
to find your way around. Node EC2 calls translate directly to Amazon Query API.
Other than converting parameter maps to signed query strings and converting the
XML responses to JSON objects, there is no syntactic sugar. See Rationale.
Here's an example using
[RunInstances](http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-RunInstances.html).
```
{ "key": "EXAMPLE"
, "secret": "EXAMPLE"
}
```
```javascript
// Require EC2.
var ec2 = require("ec2");
// Read in the configuration above.
var configuration = JSON.parse(fs.readFileSync("configuration.json", "utf8"));
// Create an ec2 function that uses your configuration.
ec2 = ec2(configuration)
// Run an instance and wait for it to become ready.
ec2("RunInstances", {
ImageId: "ami-2d4aa444", KeyName: "launch_key"
}, running);
var reservationId, instanceId;
function running (error, response) {
if (error) throw error;
reservationId = response.reservationId
instanceId = response.instancesSet[0].instanceId;
ec2("DescribeInstances", starting);
}
function starting (error, response) {
if (error) throw error;
var reservation = response.reservationSet.filter(function (reservation) {
return reservation.reservationId == reservationId;
})[0];
var instance = reservation.instancesSet.filter(function (instance) {
return instance.instanceId == instanceId;
})[0];
if (instance.instanceState.name == "running") ready();
else setTimeout(starting, 2500);
if (instance.instanceState.name == "running") {
});
function ready () {
console.log("Instance created with id: " + instanceId);
}
```
## Installing
The easiest way to install is using npm.
```
npm install ec2
```
You can also checkout the source code for using `git`. It has only one
dependency, the wonderful little XML parser `node-xml`.
## Reference
Node EC2 exports a function you can use to build an EC2 function.
```javascript
var ec2 = require("ec2");
```
function: createClient
Creates an {{AmazonEC2Client}} object for use in
class: AmazonEC2Client
Communicates with the Amazon Query API.
function: call
parameter: name
The Amazon EC2 Query API action to perform.
parameter: parameters optional
Named parameters for the Amazon EC2 Query API action.
parameter: callback optional
A callback function that is called with JSON object containing the Amazon EC2
Query API repsonse when the Amazon EC2 Query API action completes.
Call an Amazon Query API action.
function: poll
parameter: name
The Amazon EC2 Query API action to perform.
parameter: parameters optional
Named parameters for the Amazon EC2 Query API action.
parameter: callback optional
A callback function that is called with JSON object containing the Amazon EC2
Query API repsonse when the Amazon EC2 Query API action completes. If the
callback function returns `false` the query is performed again.
Call an Amazon Query API action while a condition is false.
function: execute
Run the query.
event: error
parameter: error
The error response structure.
parameter: statusCode
The HTTP status code for the error response.
Called when there is any error, both network errors, and exceptions thrown by
callbacks, so that you have a chance to release instances if things go wrong.
event: end
Called when all queries have completed successfully.
Jump to Line
Something went wrong with that request. Please try again.