forked from andalike/lambdaNode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2Stop_15July2020.js
62 lines (55 loc) · 1.59 KB
/
ec2Stop_15July2020.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
var sns = new AWS.SNS();
var ec2 = new AWS.EC2();
function one() {
sendIntialMessage();
}
function sendIntialMessage() {
var params = {
Message: 'Hi, The Instance will be stopped now. Please check the next message for the status', /* required */
Subject: 'Initial Status',
TopicArn: 'arn:aws:sns:us-east-1:016124700666:developers'
};
sns.publish(params, function (err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
stopInstance();
}
});
}
function stopInstance() {
var params = {
InstanceIds: [
"i-0003926a3395fa4c3"
]
};
ec2.stopInstances(params, function (err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
sendFinalMessage();
}
});
}
function sendFinalMessage() {
var params = {
Message: 'Hi, The Instance has been stopped successfuly, No action required', /* required */
Subject: 'Final status',
TopicArn: 'arn:aws:sns:us-east-1:016124700666:developers'
};
sns.publish(params, function (err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
});
}
module.exports.handler = one;