-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathserverstatus.ts
104 lines (73 loc) · 3.02 KB
/
serverstatus.ts
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
/*
This code was created from sample code provided for the AWS SDK for JavaScript version 3 (v3),
which is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at
https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/using-lambda-function-prep.html.
Purpose:
This function queries the server status of an ecs container, and if it's running returns the public ip
Inputs (into code):
- REGION
- SERVICE_ARN - service arn or name of the ecs service
- CLUSTER_ARN - cluster arn or name of the ecs cluster
*/
"use strict";
const { ECSClient, ListServicesCommand, DescribeServicesCommand, ListTasksCommand, DescribeTasksCommand } = require('@aws-sdk/client-ecs');
const { EC2Client, DescribeNetworkInterfacesCommand } = require('@aws-sdk/client-ec2');
//Set the AWS Region
const REGION = process.env.REGION;
const SERVICE_ARN = process.env.SERVICE_ARN;
const CLUSTER_ARN = process.env.CLUSTER_ARN;
const client = new ECSClient({ region: REGION });
const ec2Client = new EC2Client({ region: REGION });
exports.handler = async (event, context, callback) => {
var statusResults = await getIPFunction();
console.log("status results: " + JSON.stringify(statusResults));
let response = {
statusCode: 200,
headers: {
},
body: JSON.stringify(statusResults)
};
callback(null, response);
};
async function getIPFunction() {
// Define the object that will hold the data values returned
let statusResults = {
running: false,
ip: "",
};
try {
var ListTasksParams = {
servicesName: SERVICE_ARN,
cluster: CLUSTER_ARN,
desiredStatus: "RUNNING"
};
const listTasksCommand = new ListTasksCommand(ListTasksParams);
const listTasks = await client.send(listTasksCommand);
console.log(listTasks);
if (listTasks.taskArns.length > 0) {
var describeTaskParams = {
cluster: CLUSTER_ARN,
tasks: listTasks.taskArns
};
const describeTaskCommand = new DescribeTasksCommand(describeTaskParams);
const describeTasks = await client.send(describeTaskCommand);
console.log(describeTasks);
var networkInterfaceId = describeTasks.tasks[0].attachments[0].details.find(x => x.name === "networkInterfaceId").value;
console.log("found network interfaceid " + networkInterfaceId);
var describeNetworkInterfacesParams = {
NetworkInterfaceIds: [networkInterfaceId]
};
const describeNetworkInterfacesCommand = new DescribeNetworkInterfacesCommand(ListTasksParams);
const networkInterfaces = await ec2Client.send(describeNetworkInterfacesCommand);
console.log(networkInterfaces);
var publicIp = networkInterfaces.NetworkInterfaces.find(x => x.Association != undefined).Association.PublicIp;
console.log("found public IP " + publicIp);
statusResults.running = true;
statusResults.ip = publicIp + ":2456";
}
} catch (error) {
console.log(error);
}
console.log(JSON.stringify(statusResults));
return statusResults;
}