Created by
Craig Phillips
| // AWS SDK object instance
var AWS = require('aws-sdk');
// The names of the EC2 instances that we wish to stop.
// Servers need to have a "Tag" with the name of "Name" and the value is as
// included in the list below.
var instanceNames = ['YourServerName1', 'YourServerName2', 'YourServerName3'];
// node.js event handler declaration
exports.handler = function(event, context) {
// AWS EC2 object instance
var ec2 = new AWS.EC2({region: 'eu-west-1'});
var instanceIds = { InstanceIds: []};
var params = {
Filters: [
{
Name: 'tag:Name',
Values: instanceNames
}
]
};
ec2.describeInstances(params, function (err, data) {
if (err) return console.error(err.message);
var result = data;
for(var i = 0; i < result.Reservations.length; ++i) {
for(var j = 0; j < result.Reservations[i].Instances.length; ++j) {
instanceIds.InstanceIds.push(result.Reservations[i].Instances[j].InstanceId);
}
}
console.log(JSON.stringify(instanceIds));
// call AWS API to start list of instances
ec2.startInstances(instanceIds, function (err, data) {
if (err) {
// log failed with stack trace
console.log(err, err.stack);
} else {
// successful response
console.log(JSON.stringify(data));
}
// report back
context.done(err,data);
});
});
};
|
| // AWS SDK object instance
var AWS = require('aws-sdk');
// The names of the EC2 instances that we wish to stop.
// Servers need to have a "Tag" with the name of "Name" and the value is as
// included in the list below.
var instanceNames = ['YourServerName1', 'YourServerName2', 'YourServerName3'];
// node.js event handler declaration
exports.handler = function(event, context) {
// AWS EC2 object instance
var ec2 = new AWS.EC2({region: 'eu-west-1'});
var instanceIds = { InstanceIds: []};
var params = {
Filters: [
{
Name: 'tag:Name',
Values: instanceNames
}
]
};
ec2.describeInstances(params, function (err, data) {
if (err) return console.error(err.message);
var result = data;
for(var i = 0; i < result.Reservations.length; ++i) {
for(var j = 0; j < result.Reservations[i].Instances.length; ++j) {
instanceIds.InstanceIds.push(result.Reservations[i].Instances[j].InstanceId);
}
}
console.log(JSON.stringify(instanceIds));
// call AWS API to stop list of instances
ec2.stopInstances(instanceIds, function (err, data) {
if (err) {
// log failed with stack trace
console.log(err, err.stack);
} else {
// successful response
console.log(JSON.stringify(data));
}
// report back
context.done(err,data);
});
});
};
|