Last active
August 29, 2015 14:13
-
-
Save horike37/06a81d6475f86e2dac91 to your computer and use it in GitHub Desktop.
Ruby on RailsからLambdaをキックしてOpsWorks管理のEC2を起動させる ref: http://qiita.com/horike37/items/9afe446ffc8478e46ee3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ACCESS_KEY = '<ACCESS_KEY>'; | |
const SECRET_KEY = '<SECRET_KEY>'; | |
var AWS = require('aws-sdk'); | |
AWS.config.update({accessKeyId: ACCESS_KEY, secretAccessKey: SECRET_KEY}); | |
exports.handler = function(event, context) { | |
var opsworks = new AWS.OpsWorks(); | |
opsworks.describeInstances( {InstanceIds : [event.instance_id]},function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} else { | |
if ( data.Instances[0].Status == 'stopped' ) { | |
opsworks.startInstance({InstanceId : event.instance_id}, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} else { | |
context.done(null, 'Update Online Success'); | |
} | |
}); | |
} else if ( data.Instances[0].Status == 'online' ) { | |
context.done(null, 'Now Online'); | |
} else { | |
context.done(null, 'Other Status'); | |
} | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
class ItemsController < ApplicationController | |
def index | |
Aws.config[:region] = "us-east-1" | |
lambda = Aws::Lambda::Client.new; | |
if params[:instance_status_toggle] | |
param = { instance_id: "<インスタンスID>"}.to_json # lambdaに渡す引数をjsonで生成 | |
resp = lambda.invoke_async(function_name: "start-incetance", invoke_args: param) # lambdaのstart-incetance関数を実行 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment