Skip to content

Instantly share code, notes, and snippets.

@yyano
Created March 27, 2019 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyano/d89c99c2e9dfdcdbd4a28701dd54432e to your computer and use it in GitHub Desktop.
Save yyano/d89c99c2e9dfdcdbd4a28701dd54432e to your computer and use it in GitHub Desktop.
AWS Lambda - node.js 6.10 でEC2インスタンスを起動したり止めたり

CloudWatch Event の 定数で EC2 インスタンスIDを指定する

{"ec2id":"i-0XXXXXXXXXXXXXXXXX"}
var AWS = require('aws-sdk');
const ec2 = new AWS.EC2();
exports.handler = (event, context, callback) => {
if(typeof(event.ec2id) !== "undefined") {
ec2.startInstances({InstanceIds: [event.ec2id]}).promise()
.then((r) => {
console.log("START EC2 instance", event.ec2id);
callback(null, 'START EC2 instance: ' + event.ec2id);
})
.catch((e) => {
callback('ERROR! start EC2: ' + event.ec2id, null);
});
} else {
callback('ERROR! not set EC2 id', null);
}
};
var AWS = require('aws-sdk');
const ec2 = new AWS.EC2();
exports.handler = (event, context, callback) => {
if(typeof(event.ec2id) !== "undefined") {
ec2.stopInstances({InstanceIds: [event.ec2id]}).promise()
.then((r) => {
console.log("STOP EC2 instance", event.ec2id);
callback(null, 'STOP EC2 instance: ' + event.ec2id);
})
.catch((e) => {
callback('ERROR! stop EC2: ' + event.ec2id, null);
});
} else {
callback('ERROR! not set EC2 id', null);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment