 sqs = aws
   .load('sqs', key, secret)
   .setApiVersion('2011-10-01')
+ec2 = aws
+  .load('ec2', key, secret)
+  .setApiVersion('2012-05-01')
+
+getRegionInstances = (region, msg) ->
+  ec2.setRegion(region).request 'DescribeInstances', (error, reservations) ->
+    if error?
+      msg.send "Failed to describe instances for region #{region} - error #{error}"
+      return
+
+    ec2.setRegion(region).request 'DescribeInstanceStatus', (error, allStatuses) ->
+      statuses = if error? then [] else allStatuses.instanceStatusSet.item
+
+      instances = _.flatten [reservations?.reservationSet?.item ? []]
+      instances = _.pluck instances, 'instancesSet'
+      instances = _.flatten _.pluck instances, 'item'
+
+      msg.send "Found #{instances.length} instances for region #{region}..."
+
+      for instance in instances
+        do (instance) ->
+          status = _.find statuses, (s) ->
+            instance.instanceId == s.instanceId
+
+          suffix = ''
+          state = instance.instanceState.name
+          excl = String.fromCharCode 0x203C
+          dexcl = excl + excl
+
+          switch state
+            when 'pending' then prefix = String.fromCharCode 0x25B2
+            when 'running' then prefix = String.fromCharCode 0x25BA
+            when 'shutting-down' then prefix = String.fromCharCode 0x25BC
+            when 'terminated' then prefix = String.fromCharCode 0x25AA
+            when 'stopping' then prefix = String.fromCharCode 0x25A1
+            when 'stopped' then prefix = String.fromCharCode 0x25A0
+            else prefix = dexcl
+
+          if status?
+            bad = _.filter [status.systemStatus, status.instanceStatus],
+            (s) -> s.status != 'ok'
+
+            if bad.length > 0
+              prefix = dexcl
+              badStrings = _.map bad, (b) ->
+                b.details.item.name + ' ' + b.details.item.status
+              concat = (memo, s) -> memo + s
+              suffix = _.reduce badStrings, concat, ''
+
+            iEvents = _.flatten [status.eventsSet?.item ? []]
+            if not _.isEmpty iEvents then prefix = dexcl
+            desc = (memo, e) -> "#{memo} #{dexcl}#{e.code} : #{e.description}"
+            suffix += _.reduce iEvents, desc, ''
+
+          id = instance.instanceId ? 'N/A'
+          type = instance.instanceType
+          dnsName = if _.isEmpty instance.dnsName then 'N/A' \
+            else instance.dnsName
+          launchTime = moment(instance.launchTime)
+            .format 'ddd, L LT'
+          arch = instance.architecture
+          devType = instance.rootDeviceType
+
+          tags = _.flatten [instance.tagSet.item]
+          name = (_.find tags, (t) -> t.key == 'Name').value
+
+          msg.send "#{prefix} [#{state}] - #{name} / #{type} [#{devType} #{arch}] / #{dnsName} / #{region} / #{id} - started #{launchTime} #{suffix}"
 
 getRegionQueues = (region, msg) ->
   sqs.setRegion(region).request 'ListQueues', {}, (error, queues) ->

# Queries for the status of AWS services
#
# hubot sqs status - Returns the status of SQS queues.
#

# Environment variables:
#   HUBOT_AWS_ACCESS_KEY_ID     - The Amazon access key id
#   HUBOT_AWS_SECRET_ACCESS_KEY - The Amazon secret key for the given id
#   HUBOT_AWS_SQS_REGIONS - Comma separated list of regions to query
#
# package.json needs to have "aws2js":"0.6.12" and "moment":"1.6.2" and "underscore":"1.3.3"
#

# https://console.aws.amazon.com/iam/home?#
#
# SQS - requires ListQueues, GetQueueAttributes and ReceiveMessage
# EC2 - requires EC2:Describe*, elasticloadbalancing:Describe*, cloudwatch:ListMetrics, 
# cloudwatch:GetMetricStatistics, cloudwatch:Describe*, autoscaling:Describe*

key = process.env.HUBOT_AWS_ACCESS_KEY_ID
secret = process.env.HUBOT_AWS_SECRET_ACCESS_KEY
