EC2インスタンスの自動起動停止

  • 0
    いいね
  • 0
    コメント

    自動起動

    var AWS = require('aws-sdk'); 
    AWS.config.region = 'ap-northeast-1';
    
    function ec2Start(cb){
        var ec2 = new AWS.EC2();
        var params = {
            InstanceIds: [
                "i-0da881bbe333d6fbb"
            ]
        };
    
        ec2.startInstances(params, function(err, data) {
            if (!!err) {
                console.log(err, err.stack);
            } else {
                console.log(data);
                cb();
            }
        });
    }
    
    exports.handler = (event, context, callback) => {
        // TODO implement
        // callback(null, 'Hello from Lambda');
        ec2Start(function() {
            context.done(null, 'Started Instance');
        });
    };
    

    自動停止

    var AWS = require('aws-sdk'); 
    AWS.config.region = 'ap-northeast-1';
    
    function ec2Stop(cb){
        var ec2 = new AWS.EC2();
        var params = {
            InstanceIds: [
                "i-0da881bbe333d6fbb"
            ]
        };
    
        ec2.stopInstances(params, function(err, data) {
            if (!!err) {
                console.log(err, err.stack);
            } else {
                console.log(data);
                cb();
            }
        });
    }
    
    exports.handler = (event, context, callback) => {
        // TODO implement
        // callback(null, 'Hello from Lambda');
        ec2Stop(function() {
            context.done(null, 'Stoped Instance');
        });
    };
    

    ロール

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "logs:CreateLogGroup",
                    "logs:CreateLogStream",
                    "logs:PutLogEvents",
                    "ec2:StopInstances",
                    "ec2:StartInstances"
                ],
                "Resource": [
                    "arn:aws:logs:*:*:*",
                    "arn:aws:ec2:*"
                ]
            }
        ]
    }