Skip to content

Instantly share code, notes, and snippets.

@patrobinson
Created January 24, 2019 04:32
Show Gist options
  • Save patrobinson/7798669b62a2c3d98c271ccef6259b8b to your computer and use it in GitHub Desktop.
Save patrobinson/7798669b62a2c3d98c271ccef6259b8b to your computer and use it in GitHub Desktop.
Rough AWS inventory tool
require 'aws-sdk-configservice'
require 'json'
require 'net/http'
module Enumaws
def self.run
uri = URI('https://raw.githubusercontent.com/aws/aws-sdk-ruby/master/apis/config/2014-11-12/api-2.json')
res = Net::HTTP.get_response(uri)
config_api_schema = JSON.load(res.body)
resource_types = config_api_schema["shapes"]["ResourceType"]["enum"]
config_client = Aws::ConfigService::Client.new
results = {}
resource_types.each do |rt|
results[rt] = {}
resources = config_client.list_discovered_resources(resource_type: rt)[:resource_identifiers]
next if resources.empty?
resource_keys = resources.map do |r|
{
resource_type: r[:resource_type],
resource_id: r[:resource_id],
}
end
resource_configs = config_client.batch_get_resource_config(resource_keys: resource_keys)[:base_configuration_items]
resource_configs.each do |resource_config|
config = JSON.load(resource_config.configuration)
results[rt][resource_config.resource_name] = config
end
end
puts results.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment