Last active
January 23, 2017 06:55
-
-
Save hotakasaito/ac4aad2be578535eed81e22a483141f4 to your computer and use it in GitHub Desktop.
Amazon EC2 Run Commandでコマンド発行から結果出力まで1アクションで実施 ref: http://qiita.com/hotakasaito/items/1650f983558555da70a1
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
AWS = require('aws-sdk') | |
AWS.config.region = 'ap-northeast-1' | |
ssm = new AWS.SSM() | |
Promise = require('bluebird') | |
retry = require('bluebird-retry') | |
Promise.promisifyAll(ssm) | |
checkStatus = (id) -> | |
params = | |
CommandId: id | |
Details: true | |
ssm.listCommandInvocationsAsync(params).then (res, err) -> | |
throw err if err | |
if res.CommandInvocations[0].Status == 'Success' | |
return res | |
else | |
throw new Error "#{res.CommandInvocations[0].Status}" | |
params = | |
DocumentName: 'AWS-RunShellScript' | |
InstanceIds: ['i-***'] | |
Parameters: | |
commands: ['hostname'] | |
ssm.sendCommandAsync(params).then (sendRes, sendErr) -> | |
if sendErr | |
console.log sendErr | |
throw sendErr | |
else | |
console.log sendRes | |
sendRes | |
.then (data) -> | |
retry(checkStatus, { args: [data.Command.CommandId], max_tries: 5, interval: 1000 }) | |
.then (data) -> | |
console.log "SUCCESS:", JSON.stringify(data, null, 2) | |
.caught (err) -> | |
console.log "ERROR:", JSON.stringify(err, null, 2) | |
.catch (err) -> | |
console.log "ERROR:", JSON.stringify(err, null, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment