-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (124 loc) · 3.83 KB
/
index.js
File metadata and controls
139 lines (124 loc) · 3.83 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
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
132
133
134
135
136
137
138
139
exports.handler = (event, context, callback) => {
var wxp_dynamo_data = null;
var wxp_ec2_data = null;
var ec2_public_dns = '';
//get the user name from api gateway
var cognito_username = event.requestContext.authorizer.claims['cognito:username'];
var email = event.requestContext.authorizer.claims['email'];
//FOR TESTING PURPOSES, REMOVE IN PRODUCTION
// var cognito_username = 'ede97585-21c7-46f1-a5e2-8cc460bfca99';
// var email = "vo.bee92@gmail.com";
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region (May not be necessary)
AWS.config.update({region: 'us-east-1'});
// Create the DynamoDB service object
var dynamo = new AWS.DynamoDB(); //TODO SHOULD DEFINE API VERSION!
//Create EC2 Service object
var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
//create return object and response for debugging purposes.
var obj = {
'cognito_username': cognito_username,
'email': email,
};
var response = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*"
},
"body": obj
};
function init(){
getDynamoData();
}
// Call DynamoDB to read the item from the table
function getDynamoData(){
var params = {
TableName: 'WxPress',
Key: {
'UserId' : {S: cognito_username},
}
};
dynamo.getItem(params, function(err, data) {
if (err) {
response.body.error = err;
send();
} else {
wxp_dynamo_data = data.Item;
response.body.wxpress_data = wxp_dynamo_data;
getEc2Data();
}
});
}
// fetch data of ec2 instance
function getEc2Data(){
var ec2Params = {
InstanceIds: [wxp_dynamo_data.instanceId.S]
};
ec2.describeInstances(ec2Params, function(err, data) {
if (err) {
response.body.err = err;
send();
} else {
wxp_ec2_data = data.Reservations[0].Instances[0];
response.body.ec2 = wxp_ec2_data;
handleInstance();
}
});
}
//Handler for instance state codes
function handleInstance(){
var instanceCode = wxp_ec2_data.State.Code;
switch(instanceCode){
case 80: //Stopped
//start it!
startEc2();
break;
case 16: //running
ec2_public_dns = wxp_ec2_data.PublicDnsName;
response.body.public_dns = ec2_public_dns;
send();
break;
default:
response.body.err = "EC2 State Code not handled.";
send();
break;
}
}
//Initiate start of the ec2 instance
function startEc2(){
var params = {
'InstanceIds': [wxp_ec2_data.InstanceId],
'DryRun': false
}
ec2.startInstances(params, function(err, data) {
if (err) {
response.body.err = err;
send();
} else if (data) {
waitForEc2();
}
});
}
//Wait for the ec2 instance to start
function waitForEc2(){
var params = {
'InstanceIds': [wxp_ec2_data.InstanceId]
};
ec2.waitFor('instanceRunning', params, function(err, data) {
if (err) {
response.body.err = err;
send();
} else {
getEc2Data();
}
});
}
//send response to endpoint
function send(){
response.body = JSON.stringify(response.body);
callback(null, response);
}
//start!
init();
};