Skip to content

Instantly share code, notes, and snippets.

@saggie
Created December 17, 2020 07:05
Show Gist options
  • Save saggie/ce5500584a25f9a5d158317d0c8b5e79 to your computer and use it in GitHub Desktop.
Save saggie/ce5500584a25f9a5d158317d0c8b5e79 to your computer and use it in GitHub Desktop.
特定の EC2 Instance を起動あるいは停止させる Lambda 関数 (Node.js 12.x)
/*
* 特定の EC2 Instance を起動あるいは停止させる関数
*/
const params = {
// 対象のEC2インスタンスIDのリスト
InstanceIds: [
'i-00000000000000000',
]
};
const aws = require('aws-sdk');
aws.config.update({region: 'ap-northeast-1'});
exports.handler = function(event, context) {
if (event.startOrStop === 'start') {
startInstances();
} else if (event.startOrStop === 'stop') {
stopInstances();
} else {
console.log('Do nothing. event: ', JSON.stringify(event))
}
return {
statusCode: 200,
body: JSON.stringify('Completed.'),
};
};
function startInstances() {
console.log('Starting instances...')
new aws.EC2().startInstances(params, putResultLog);
}
function stopInstances() {
console.log('Stopping instances...')
new aws.EC2().stopInstances(params, putResultLog);
}
function putResultLog(err, data) {
if (err) {
console.log("Error.", err);
} else if (data) {
console.log("Success.", JSON.stringify(data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment