-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathstart_and_stop_ec2_instances.js
131 lines (122 loc) · 3.5 KB
/
start_and_stop_ec2_instances.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
*
* This script starts and stops EC2 instances.
*/
const AWS = require('aws-sdk');
const TAGS = [{
key: 'autostartandstop',
value: 'true'
}];
const EC2_REGIONS = [
'eu-north-1',
'ap-south-1',
'eu-west-3',
'eu-west-2',
'eu-west-1',
'ap-northeast-2',
'ap-northeast-1',
'sa-east-1',
'ca-central-1',
'ap-southeast-1',
'ap-southeast-2',
'eu-central-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2'
];
// Get instance ids by region, state name, tags
async function getEc2InstanceIds(ec2Obj, stateName, tags) {
try {
const filters = [{
Name: "instance-state-name",
Values: [
stateName
]
}];
for (const tagObj of tags) {
filters.push({
Name: `tag:${tagObj.key}`,
Values: [
tagObj.value
]
})
}
const ec2Instances = await ec2Obj.describeInstances({
Filters: filters
}).promise();
const instanceIds = [];
for (const reservation of ec2Instances.Reservations) {
for (const instance of reservation.Instances) {
instanceIds.push(instance.InstanceId)
}
}
return instanceIds;
} catch (e) {
throw e;
}
}
function stopRunningEC2Instances(ec2Obj, instanceIds) {
return ec2Obj.stopInstances({
InstanceIds: instanceIds
}).promise();
}
function startStoppedEc2Instances(ec2Obj, instanceIds) {
return ec2Obj.startInstances({
InstanceIds: instanceIds
}).promise();
}
async function handleStoppingEc2Instances() {
try {
console.log('Stopping instances task started..')
for (const region of EC2_REGIONS) {
const ec2Obj = new AWS.EC2({
region
})
const ec2InstanceIds = await getEc2InstanceIds(ec2Obj, 'running', TAGS);
console.log(region, ': ', ec2InstanceIds);
if (!ec2InstanceIds || ec2InstanceIds.length === 0) {
console.log('No instance for region ', region);
continue;
}
await stopRunningEC2Instances(ec2Obj, ec2InstanceIds);
console.log('Stopping task completed. Instances: ', ec2InstanceIds, '& Region: ', region);
}
} catch (e) {
throw e;
}
}
async function handleStartingEc2Instances() {
try {
console.log('Starting instances task started..')
for (const region of EC2_REGIONS) {
const ec2Obj = new AWS.EC2({
region
})
const ec2InstanceIds = await getEc2InstanceIds(ec2Obj, 'stopped', TAGS);
console.log(region, ': ', ec2InstanceIds);
if (!ec2InstanceIds || ec2InstanceIds.length === 0) {
console.log('No instance for region ', region);
continue;
}
await startStoppedEc2Instances(ec2Obj, ec2InstanceIds);
console.log('Starting task completed. Instances: ', ec2InstanceIds, ' & Region ', region);
}
} catch (e) {
throw e;
}
}
exports.handler = async (event) => {
try {
console.log("Received event: ", JSON.stringify(event, null, 2));
if (event.action === 'start') {
await handleStartingEc2Instances();
}
if (event.action === 'stop') {
await handleStoppingEc2Instances();
}
return;
} catch (e) {
throw e;
}
};