-
Notifications
You must be signed in to change notification settings - Fork 12
/
launch.js
46 lines (38 loc) · 1.26 KB
/
launch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Require EC2.
var ec2 = require("ec2")
, fs = require("fs")
, path = require("path")
, configuration = path.resolve(process.env.HOME, ".aws")
;
// Read in the configuration above.
var configuration = JSON.parse(fs.readFileSync(configuration, "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", MinCount: 1, MaxCount: 1
}, running);
var reservationId, instanceId;
function running (error, response) {
if (error) throw error;
reservationId = response.reservationId
instanceId = response.instancesSet[0].instanceId;
describe();
}
function describe () {
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(describe, 2500);
}
function ready () {
console.log("Instance created with id: " + instanceId);
}