-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (32 loc) · 1006 Bytes
/
app.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
import 'dotenv/config';
import "@babel/polyfill";
import "cross-fetch/polyfill";
import Koa from 'koa';
import serverless from 'serverless-http';
import { EC2 } from '@aws-sdk/client-ec2';
const app = new Koa();
// response
const slackMessage = (message) => {
const slackUrl = process.env.SLACK_URL;
/*global fetch*/
fetch(slackUrl, {
method: "POST",
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({"text": message})
});
};
app.use(async ctx => {
const instances = JSON.parse(process.env.INSTANCES || "[]");
const promises = instances.map(async (instance) => {
slackMessage(`Shutting down ${instance.id} in ${instance.region}...`);
const ec2 = new EC2({region: instance.region, maxRetries: 15});
const result = await ec2.stopInstances({InstanceIds: [instance.id]});
console.log(result);
});
await Promise.all(promises);
ctx.body = {message: 'OK'};
});
app.listen(8080);
module.exports.handler = serverless(app);