・INCETANCE_IDは環境変数に追加

StartEC2Instance

const INSTANCE_ID = `${process.env.NODE_ENV}`;

var AWS = require('aws-sdk'); 
AWS.config.region = 'ap-northeast-1';

function ec2Start(cb){
    var ec2 = new AWS.EC2();
    var params = {
        InstanceIds: [
            INSTANCE_ID
        ]
    };

    ec2.startInstances(params, function(err, data) {
        if (!!err) {
            console.log(err, err.stack);
        } else {
            console.log(data);
            cb();
        }
    });
}
exports.handler = function(event, context) {
    console.log('start');
    ec2Start(function() {
        context.done(null, 'Started Instance');
    });
};

StopEC2Instance

const INSTANCE_ID = `${process.env.NODE_ENV}`;

var AWS = require('aws-sdk'); 



AWS.config.region = 'ap-northeast-1';

function ec2Stop(cb){

    var ec2 = new AWS.EC2();
    var params = {
        InstanceIds: [
            INSTANCE_ID
        ]
    };

    ec2.stopInstances(params, function(err, data) {
        if (!!err) {
            console.log(err, err.stack);
        } else {
            console.log(data);
            cb();
        }
    });
}
exports.handler = function(event, context) {
    console.log('start');
    ec2Stop(function() {
        context.done(null, 'Stoped Instance');
    });
};
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.