Skip to content

Instantly share code, notes, and snippets.

@ariel-frischer
Last active February 23, 2025 06:55
Show Gist options
  • Save ariel-frischer/719800265ea5c95bea5cf624511746a4 to your computer and use it in GitHub Desktop.
Save ariel-frischer/719800265ea5c95bea5cf624511746a4 to your computer and use it in GitHub Desktop.
Pretty print aws usage per service for last 7 days
#!/usr/bin/env bash
set -euo pipefail
# Get cost and usage data from AWS Cost Explorer
result=$(aws ce get-cost-and-usage --no-cli-pager \
--time-period Start=$(date -d '7 days ago' '+%Y-%m-%d'),End=$(date '+%Y-%m-%d') \
--granularity DAILY \
--metrics "BlendedCost" "UsageQuantity" \
--group-by Type=DIMENSION,Key=SERVICE)
# Iterate over each day in the results
while IFS= read -r day; do
start=$(echo "$day" | jq -r '.TimePeriod.Start')
end=$(echo "$day" | jq -r '.TimePeriod.End')
echo "Date: $start to $end"
printf "%-40s | %-20s | %-25s\n" "SERVICE" "BLENDED COST(USD)" "USAGE"
printf -- '%.0s-' {1..90}; echo
total_cost=0
total_usage=0
usage_unit=""
# Process each group for the day
mapfile -t groups < <(echo "$day" | jq -c '.Groups[]')
for group in "${groups[@]}"; do
service=$(echo "$group" | jq -r '.Keys[0]')
cost=$(echo "$group" | jq -r '.Metrics.BlendedCost.Amount')
usage=$(echo "$group" | jq -r '.Metrics.UsageQuantity.Amount')
unit=$(echo "$group" | jq -r '.Metrics.UsageQuantity.Unit')
if [ -z "$usage_unit" ]; then
usage_unit="$unit"
fi
total_cost=$(awk "BEGIN {printf \"%.10f\", $total_cost + $cost}")
total_usage=$(awk "BEGIN {printf \"%.10f\", $total_usage + $usage}")
printf "%-40s | %-20s | %-25s\n" "$service" "$cost" "$usage ($unit)"
done
printf -- '%.0s-' {1..90}; echo
printf "%-40s | %-20s | %-25s\n" "TOTAL" "$total_cost" "$total_usage ($usage_unit)"
echo ""
done < <(echo "$result" | jq -c '.ResultsByTime[]')
@ariel-frischer
Copy link
Author

This is a simpler way check on billing, without having to log into AWS console daily (with MFA auth).

Output looks like this:

Date: 2025-02-15 to 2025-02-16
SERVICE                                  | BLENDED COST(USD)    | USAGE                    
------------------------------------------------------------------------------------------
AWS Secrets Manager                      | 0                    | 120.0178571429 (N/A)     
Amazon EC2 Container Registry (ECR)      | 0.0040467336         | 0.0404673312 (N/A)       
Amazon Route 53                          | 0.0001952            | 488 (N/A)                
Amazon Simple Storage Service            | 0.000022             | 32.0000065799 (N/A)      
------------------------------------------------------------------------------------------
TOTAL                                    | 0.0042639336         | 640.0583310540 (N/A)     

Date: 2025-02-16 to 2025-02-17
SERVICE                                  | BLENDED COST(USD)    | USAGE                    
------------------------------------------------------------------------------------------
AWS Secrets Manager                      | 0                    | 36.4285714296 (N/A)      
Amazon EC2 Container Registry (ECR)      | 0.0040467336         | 0.0404673312 (N/A)       
Amazon Route 53                          | 0.000112             | 280 (N/A)                
Amazon Simple Storage Service            | 0.0000106            | 15.0000032345 (N/A)      
------------------------------------------------------------------------------------------
TOTAL                                    | 0.0041693336         | 331.4690419953 (N/A)     

Date: 2025-02-17 to 2025-02-18
SERVICE                                  | BLENDED COST(USD)    | USAGE                    
------------------------------------------------------------------------------------------
AWS Secrets Manager                      | 0                    | 0.4107142867 (N/A)       
Amazon EC2 Container Registry (ECR)      | 0.0040467336         | 0.0404673312 (N/A)       
Amazon Route 53                          | 0.000076             | 190 (N/A)                
------------------------------------------------------------------------------------------
TOTAL                                    | 0.0041227336         | 190.4511816179 (N/A)     

Date: 2025-02-18 to 2025-02-19
SERVICE                                  | BLENDED COST(USD)    | USAGE                    
------------------------------------------------------------------------------------------
AWS Secrets Manager                      | 0                    | 0.4285714296 (N/A)       
Amazon EC2 Container Registry (ECR)      | 0.0040467336         | 0.0404673312 (N/A)       
Amazon Route 53                          | 0.0001376            | 344 (N/A)                
Amazon Simple Email Service              | 0                    | 1.000007323 (N/A)        
AmazonCloudWatch                         | 0                    | 0.0104166666 (N/A)       
------------------------------------------------------------------------------------------
TOTAL                                    | 0.0041843336         | 345.4794627504 (N/A)     

Date: 2025-02-19 to 2025-02-20
SERVICE                                  | BLENDED COST(USD)    | USAGE                    
------------------------------------------------------------------------------------------
AWS Secrets Manager                      | 0                    | 0.4285714296 (N/A)       
Amazon EC2 Container Registry (ECR)      | 0.0040467336         | 0.0404673312 (N/A)       
Amazon Route 53                          | 0.0001032            | 258 (N/A)                
------------------------------------------------------------------------------------------
TOTAL                                    | 0.0041499336         | 258.4690387608 (N/A)     

Date: 2025-02-20 to 2025-02-21
SERVICE                                  | BLENDED COST(USD)    | USAGE                    
------------------------------------------------------------------------------------------
AWS Secrets Manager                      | 0                    | 0.4285714296 (N/A)       
Amazon EC2 Container Registry (ECR)      | 0.0040467336         | 0.0404673312 (N/A)       
Amazon Route 53                          | 0.0001208            | 302 (N/A)                
------------------------------------------------------------------------------------------
TOTAL                                    | 0.0041675336         | 302.4690387608 (N/A)     

Date: 2025-02-21 to 2025-02-22
SERVICE                                  | BLENDED COST(USD)    | USAGE                    
------------------------------------------------------------------------------------------
AWS Secrets Manager                      | 0                    | 0.4285714296 (N/A)       
Amazon EC2 Container Registry (ECR)      | 0.0040467336         | 0.0404673312 (N/A)       
Amazon Route 53                          | 0.0001172            | 293 (N/A)                
------------------------------------------------------------------------------------------
TOTAL                                    | 0.0041639336         | 293.4690387608 (N/A)   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment