Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slack の Slash Command から AWS の EC2 と RDS の起動と停止を実行する Lambda 関数
'use strict';
const AWS = require('aws-sdk');
// EC2 インスタンスを起動する
function startEC2Instance(region, instanceId) {
const ec2 = new AWS.EC2({ region: region });
const params = {
InstanceIds: [instanceId],
DryRun: false,
};
return new Promise((resolve, reject) => {
ec2.startInstances(params, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}
// RDS インスタンスを起動する
function startDBInstance(region, identifier) {
const rds = new AWS.RDS({ region: region });
const params = {
DBInstanceIdentifier: identifier,
};
return new Promise((resolve, reject) => {
rds.startDBInstance(params, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}
// EC2 インスタンスを停止する
function stopEC2Instance(region, instanceId) {
const ec2 = new AWS.EC2({ region: region });
const params = {
InstanceIds: [instanceId],
DryRun: false,
};
return new Promise((resolve, reject) => {
ec2.stopInstances(params, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}
// RDS インスタンスを停止する
function stopDBInstance(region, identifier) {
const rds = new AWS.RDS({ region: region });
const params = {
DBInstanceIdentifier: identifier,
};
return new Promise((resolve, reject) => {
rds.stopDBInstance(params, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}
// 関数指定してインスタンスを制御します。
function executeControl(ec2Function, dbFunction) {
const result = { EC2: null, DB: null };
const a = ec2Function(process.env.EC2_REGION, process.env.EC2_INSTANCE_ID)
.then(data => {
result.EC2 = { result: 'OK', data: data };
}).catch(err => {
result.EC2 = { result: 'NG', data: err };
});
const b = dbFunction(process.env.DB_REGION, process.env.DB_INSTANCE_ID)
.then(data => {
result.DB = { result: 'OK', data: data };
}).catch(err => {
result.DB = { result: 'NG', data: err };
});
return Promise.all([a, b]).then(() => result );
}
function getSuccessfulResponse(message, result) {
return {
"response_type": "in_channel",
"attachments": [
{
"color": "#32cd32",
"title": 'Success',
"text": message,
},
{
"title": 'Result',
"text": '```' + JSON.stringify(result, null, 2) + '```',
},
],
};
}
function getErrorResponse(message) {
return {
"response_type": "ephemeral",
"attachments": [
{
"color": "#ff0000",
"title": 'Error',
"text": message,
},
],
};
}
exports.handler = (event, context, callback) => {
if (!event.token || event.token !== process.env.SLASH_COMMAND_TOKEN)
callback(null, getErrorResponse('Invalid token'));
if (!event.text)
callback(null, getErrorResponse('Parameter missing'));
if (event.text.match(/start/)) {
executeControl(startEC2Instance, startDBInstance)
.then(result => { callback(null, getSuccessfulResponse('Starting...', result)); });
} else if (event.text.match(/stop/)) {
executeControl(stopEC2Instance, stopDBInstance)
.then(result => { callback(null, getSuccessfulResponse('Stopping...', result)); });
} else {
callback(null, getErrorResponse('Unknown parameters'));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment