Last active
July 8, 2019 05:36
-
-
Save cubenuri/8762884fdc7752e9abeb5463e0275728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
AWS.config.update({region: process.env.AWS_REGION}); | |
// Create EC2 service object | |
const ec2 = new AWS.EC2({apiVersion: '2016-11-15'}); | |
const params = { | |
InstanceIds: [ 'i-xxxxxxx' ], | |
DryRun: false | |
}; | |
exports.handler = async (event) => { | |
console.log('event.body', event.body); | |
const path = event.path; | |
console.log('event.path', event.path); | |
const buildResponse = (res) => ({ | |
statusCode: 200, | |
body: typeof res === 'string' ? res : JSON.stringify(res), | |
}); | |
switch (event.httpMethod) { | |
case 'GET': | |
return buildResponse('Success GET'); | |
case 'POST': | |
if (path === '/ec2-slack-ctrl') { | |
return buildResponse({ | |
attachments: [ | |
{ title: 'EC2 Controller', callback_id: 'ctl_ec2', text: '호스트별 SELECT & 동작 선택', | |
actions: [ | |
{ | |
"name": "ec2test", | |
"text": "EC2 - TEST", | |
"type": "select", | |
"options": [ | |
{ | |
"text": "Start", | |
"value": "Start" | |
}, | |
{ | |
"text": "Stop", | |
"value": "Stop" | |
} | |
] | |
}, | |
] | |
}] | |
}); | |
} | |
if (path === '/ec2-slack-ctrl/interactive') | |
{ | |
const body = JSON.parse(decodeURIComponent(event.body).replace('payload=', '')); | |
console.log("응답 결과 :" + JSON.stringify(body)); | |
if (body.callback_id === 'ctl_ec2') | |
{ | |
const hostId = body.actions[0].name; | |
const action = body.actions[0].selected_options[0].value; | |
if(action =="Stop") | |
{ | |
await ec2.stopInstances(params, function(err, data) { | |
if (err) { | |
console.log("Error", err); | |
} else if (data) { | |
console.log(data); | |
console.log("Success", data.StoppingInstances); | |
} | |
}).promise(); | |
}else if(action =="Start") | |
{ | |
await ec2.startInstances(params, function(err, data) { | |
if (err) { | |
console.log("Error", err); | |
} else if (data) { | |
console.log(data); | |
console.log("Success", data.StartingInstances); | |
} | |
}).promise(); | |
} | |
return buildResponse(`* 응답 결과: :computer:${hostId}* *${action}* `); | |
} | |
} | |
} return { statusCode: 400, body: 'unknown httpMethod', }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment