require 'yaml' require 'terminal-table' cmd = 'aws ce get-cost-and-usage --time-period Start=2023-12-01,End=2023-12-31 --granularity MONTHLY --metrics "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE' # Parse the YAML content text = system(cmd) data = YAML.load(text) # Extracting the relevant information services = data['ResultsByTime'][0]['Groups'].map do |group| service_name = group['Keys'][0] unblended_cost = "#{group['Metrics']['UnblendedCost']['Amount']} #{group['Metrics']['UnblendedCost']['Unit']}" usage_quantity = "#{group['Metrics']['UsageQuantity']['Amount']} #{group['Metrics']['UsageQuantity']['Unit']}" [service_name, unblended_cost, usage_quantity] end # Create a table table = Terminal::Table.new headings: ['Service', 'Unblended Cost', 'Usage Quantity'], rows: services # Output the table puts table