Show the AWS Account Forecast Cost for the Current Month Broken Down by Service
In the world of cloud computing, keeping track of costs is paramount. AWS, with its plethora of services, can sometimes make cost management a challenging task. To address this, I’ve developed two scripts to query the forecasted cost for the current month, broken down by AWS service.
Sequential Script: Step-by-Step Explanation
#!/usr/bin/env bash
start_of_month=$(date -u "+%Y-%m-01")
current_day=$(date -u "+%Y-%m-%d")
end_of_month=$(date -u "+%Y-%m-%d" -d "$(date -u +'%Y-%m-01') +1 month -1 day")
current_costs=$(aws ce get-cost-and-usage --time-period Start=$start_of_month,End=$current_day --granularity MONTHLY --metrics "AmortizedCost" --group-by Type="DIMENSION",Key="SERVICE")
echo "$current_costs" | jq -c '.ResultsByTime[].Groups[]' | while IFS= read -r line; do
service=$(echo "$line" | jq -r '.Keys[0]')
filter_json=$(jq -n --arg service "$service" '{"Dimensions": {"Key": "SERVICE", "Values": [$service]}}')
forecast=$(aws ce get-cost-forecast --time-period Start="$current_day",End="$end_of_month" --granularity MONTHLY --metric "AMORTIZED_COST" --filter "$filter_json" 2>&1)
if [[ $forecast == *"Insufficient amount of historical data"* ]]; then
echo "$service, Insufficient historical data to generate forecast."
else
forecast_cost=$(echo "$forecast" | jq -r '.Total.Amount | tonumber')
forecast_cost_rounded=$(printf "%.2f" "$forecast_cost")
echo "$service, $forecast_cost_rounded"
fi
done
The sequential script operates in a straightforward manner. Here’s a breakdown:
- Date Variables: We define
start_of_month
,current_day
, andend_of_month
to establish our time frame. - Current Costs: The script retrieves current costs using the AWS Cost Explorer API, focusing on
AmortizedCost
. - Cost Processing: For each service, it calculates the forecasted cost from the current day to the end of the month.
- Output: It rounds off the forecasted cost to two decimal places and outputs the data in a readable format.
This script is ideal for environments where parallel processing might not be available or necessary.
Parallel Script: Enhanced Efficiency
#!/usr/bin/env bash
start_of_month=$(date -u "+%Y-%m-01")
current_day=$(date -u "+%Y-%m-%d")
end_of_month=$(date -u "+%Y-%m-%d" -d "$(date -u +'%Y-%m-01') +1 month -1 day")
function get_forecast(){
service="${1//\'/}"
current_day="$2"
end_of_month="$3"
filter_json=$(jq -n --arg service "$service" '{"Dimensions": {"Key": "SERVICE", "Values": [$service]}}')
forecast=$(aws ce get-cost-forecast --time-period Start="$current_day",End="$end_of_month" --granularity MONTHLY --metric "AMORTIZED_COST" --filter "$filter_json" 2>&1)
if [[ $forecast == *"Insufficient amount of historical data"* ]]; then
echo "$service, Insufficient historical data to generate forecast."
else
forecast_cost=$(echo "$forecast" | jq -r '.Total.Amount | tonumber')
forecast_cost_rounded=$(printf "%.2f" "$forecast_cost")
echo "$service, $forecast_cost_rounded"
fi
}
export -f get_forecast
aws ce get-cost-and-usage --time-period Start="$start_of_month",End="$current_day" --granularity MONTHLY --metrics "AmortizedCost" --group-by Type="DIMENSION",Key="SERVICE" | \
jq -r -c '.ResultsByTime[].Groups[] | .Keys[0] | @sh' | \
parallel --will-cite --jobs 5 --colsep ',' get_forecast {} "$current_day" "$end_of_month"
To speed up the process, especially in larger AWS environments, the parallel script comes into play:
- Function Definition:
get_forecast
is defined to process each service independently. - Parallel Execution: Using GNU Parallel, the script executes multiple instances of
get_forecast
simultaneously, significantly reducing the total processing time.
This method is particularly effective when dealing with multiple AWS services, as it leverages concurrent processing.
Sample Output
Both scripts produce a similar output, listing AWS services along with their current and forecasted costs. Here’s a snippet:
AWS Backup,1440.56
AWS Key Management Service,1122.98
Amazon Elastic Compute Cloud - Compute,54899.99
EC2 - Other,34999.56
Use Cases and Conclusions
These scripts are invaluable for several scenarios:
- Budget Planning: Businesses can use these forecasts for budgeting and financial planning.
- Cost Optimisation: Identifying high-cost services can lead to cost optimisation strategies.
- Automated Reporting: Integrate these scripts into automated systems for regular cost updates.
Automation and Multi-Account Management
A standout feature is the potential for automation and application across multiple AWS accounts. By automating these scripts, businesses can regularly monitor and forecast AWS costs without manual intervention, making it easier to manage costs across various departments or projects.
Final Thoughts
Understanding and forecasting AWS costs is crucial for efficient cloud management. These scripts, especially when automated, provide a comprehensive and time-efficient solution for managing AWS costs.