This repository was archived by the owner on Feb 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (38 loc) · 1.3 KB
/
index.js
File metadata and controls
43 lines (38 loc) · 1.3 KB
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
var AWS = require('aws-sdk');
var scan = require('./lib/scan');
var adjust = require('./lib/adjust');
var cfn = require('./lib/cfn');
module.exports = {
envCheck: envCheck,
spotswap: spotswap,
cfn: cfn
};
function envCheck() {
[
'INSTANCE_ID',
'AWS_REGION'
].forEach(function(key) {
if (!process.env[key]) throw new Error('Env var ' + key + ' is required');
});
if (!process.env.SpotGroup && !process.env.SpotFleet) throw new Error('One of SpotGroup or SpotFleet required.');
if (process.env.SpotGroup && process.env.SpotFleet) throw new Error('May only have one of SpotGroup or SpotFleet.');
}
/**
* Execute spotswap behavior:
* 1. List instances in spot ASG or Fleet with SpotTermination tag
* 2. Read current state of on-demand ASG configuration
* 3. Increment DesiredCapacity of on-demand group
* by the # of instances marked for termination
* 4. Remove tags from tagged instances
*
* @param {object} event - environment information sent from another lambda
* @param {object} context - lambda context object
* @param {Function} callback
*/
function spotswap(event, context, callback) {
AWS.config.update({ region: process.env.AWS_REGION });
scan()
.then(instances => instances.length ? adjust.up(instances) : adjust.down())
.then(() => callback())
.catch(callback);
}