-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
78 lines (69 loc) · 2.31 KB
/
index.mjs
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
import {
getContainerInstanceId,
updateContainerInstance,
drainContainerInstance,
completeLifecycleAction,
} from "./commands.mjs";
import { env } from "./env.mjs";
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
export const handler = async (event, context, callback) => {
console.log("LogAutoScalingEvent");
console.log("Received event:", JSON.stringify(event, null, 2));
const { region, account } = event;
const {
AutoScalingGroupName,
LifecycleHookName,
EC2InstanceId,
LifecycleActionToken,
NotificationMetadata,
} = event.detail;
var metadata;
if (
typeof NotificationMetadata === "string" ||
NotificationMetadata instanceof String
) {
metadata = JSON.parse(NotificationMetadata);
} else {
metadata = NotificationMetadata;
}
const { clusterName } = metadata;
const containerInstanceId = await getContainerInstanceId({
instanceId: EC2InstanceId,
clusterName,
region,
});
console.log(
`containerInstanceId: ${containerInstanceId} on cluster: ${clusterName}, ec2InstanceId: ${EC2InstanceId}`
);
var ret;
if (containerInstanceId == null || containerInstanceId == undefined) {
console.log(
`containerInstanceId not found on cluster: ${clusterName}, ec2InstanceId: ${EC2InstanceId}`
);
ret = await completeLifecycleAction({
region,
autoScalingGroupName: AutoScalingGroupName,
lifecycleHookName: LifecycleHookName,
ec2InstanceId: EC2InstanceId,
lifecycleActionToken: LifecycleActionToken,
});
} else if (containerInstanceId != null) {
// arn:aws:ecs:${region}:${accountId}:container-instance/ecsCluster/${id}
await drainContainerInstance({
clusterName,
containerInstanceId: containerInstanceId.split("/").slice(-1).pop(),
region,
});
console.log(`Waiting for draining ${env.afterDrainingWaitTimeSeconds}...`);
await sleep(env.afterDrainingWaitTimeSeconds * 1000);
console.log("draining waiting done");
ret = await completeLifecycleAction({
region,
autoScalingGroupName: AutoScalingGroupName,
lifecycleHookName: LifecycleHookName,
ec2InstanceId: EC2InstanceId,
lifecycleActionToken: LifecycleActionToken,
});
}
console.log(`response from completeLifecycleAction: ${JSON.stringify(ret)}`);
};