0

I'm writing a hubot script to list all of my CloudFormation stacks in AWS by region. I've written the following, and it works, except for the order of the data output. The script is:

key = process.env.HUBOT_AWS_ACCESS_KEY_ID
secret = process.env.HUBOT_AWS_SECRET_ACCESS_KEY
defaultRegions = 'us-east-1,us-west-2,eu-west-1'

_ = require 'underscore'
moment = require 'moment'
AWS = require 'aws-sdk'

getRegionStacks = (region, logger, msg) ->
    msg.send "In region #{region}:"
    AWS.config.region = region
    cloudFormation = new AWS.CloudFormation()
    params = {
        StackStatusFilter: [ 'CREATE_COMPLETE' ]
    }
    cloudFormation.listStacks params, (err, data) ->
        if err
            logger.error "Received error #{JSON.stringify err}"
            logger.error err.stack
        else
            for stack in data.StackSummaries
                    msg.send stack.StackName

module.exports = (robot) ->
    robot.hear /List running Cloudformation stacks/i, (msg)->
        msg.send "The following CloudFormation stacks are running:"
        getRegionStacks region, robot.logger, msg for region in defaultRegions.split ','

I'm expecting the output to look like:

The following CloudFormation stacks are running:
    In region us-east-1:
    demo-container1
    In region us-west-2:
    demo-container2
    In region eu-west-1:
    demo-container3

However, it looks like the callbacks are all firing after the functions are complete and the output returned is this:

The following CloudFormation stacks are running:
    In region us-east-1:
    In region us-west-2:
    In region eu-west-1:
    demo-container1
    demo-container2
    demo-container3

I'm sure that I'm overlooking something simple. Does anyone have an idea of what I'm doing wrong?

ptierno
  • 9,534
  • 2
  • 23
  • 35
Lee Fox
  • 217
  • 4
  • 10

1 Answers1

0

Looks like your printing the region synchronously while the liststacks function is asynchronous. The following is untested but may work:

getRegionStacks = (region, logger, msg) ->
    AWS.config.region = region
    cloudFormation = new AWS.CloudFormation()
    params = {
        StackStatusFilter: [ 'CREATE_COMPLETE' ]
    }
    cloudFormation.listStacks params, (err, data) ->
        if err
            logger.error "Received error #{JSON.stringify err}"
            logger.error err.stack
        else
            msg.send "In region #{region}:"
            for stack in data.StackSummaries
                msg.send stack.StackName
ptierno
  • 9,534
  • 2
  • 23
  • 35