Skip to content

Instantly share code, notes, and snippets.

@hotakasaito
Last active January 23, 2017 06:55
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 hotakasaito/ac4aad2be578535eed81e22a483141f4 to your computer and use it in GitHub Desktop.
Save hotakasaito/ac4aad2be578535eed81e22a483141f4 to your computer and use it in GitHub Desktop.
Amazon EC2 Run Commandでコマンド発行から結果出力まで1アクションで実施 ref: http://qiita.com/hotakasaito/items/1650f983558555da70a1
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