In my last post I talked about distributing your committed RI spend over time. The goal being to avoid buying too many 1 year RIs (front loading your spend), and missing out on the savings of committing to 3 years, but not buying too many 3 year RIs (back loading your spend) and risking having a bill you have to foot if your organization has major changes.
Our solution for balancing this is a powershell snippet that graphs our RI commitment over time.
Our solution for balancing this is a powershell snippet that graphs our RI commitment over time.
# Get RI entries from AWS console
$ri_entries = Get-EC2ReservedInstance -filter @(@{Name="state";Value="active"});
# Array to hold the relevant RI data
$ri_data = @();
# Calculate monthly cost for RIs
foreach ($ri_entry in $ri_entries) {
$ri = @{};
$hourly = $ri_entry.RecurringCharges.Amount;
$monthly = $hourly * 24 * 30 * $ri_entry.InstanceCount;
$ri.monthly = $monthly;
$ri.End = $ri_entry.End;
$ri_data += $ri;
}
# Three years into the future (maximum duration of RIs as of 1.22.2019)
$three_years_out = (get-date).addyears(3);
# Our current date iterator
$current = (get-date);
# Array to hold the commit by month
$monthly_commit = @();
# CSV file name to save output
$csv_name = "ri_commitment-$((get-date).tostring('ddMMyyyy')).csv";
# Remove the CSV if it already exists
if(test-path $csv_name) {
remove-item -force $csv_name;
}
# Insert CSV headers
"date,commitment" | out-file $csv_name -append -encoding ascii;
# Iterate from today to three years in the future
while($current -lt $three_years_out) {
# Find the sum of the RIs that are active on this date
# all RI data -> RIs that have expirations after current -> select the monthly measure -> get the sum -> select the sum
$commit = ($ri_data | ? {$_.End -gt $current} | % {$_.monthly} | measure -sum).sum;
# Build a row of the CSV
$output = "$($current),$($commit)";
# Print the output to standard out for quick review
write-host $output;
# Write out to the CSV for deeper analysis
$output | out-file $csv_name -append -encoding ascii;
# Increment to the next month and repeat
$current = $current.addmonths(1);
}
Ok, short's not the right word. It's a little lengthy, but at the end it kicks out a CSV in your working directory with months and your RI commit for them.
From there it's easy to create a graph that shows your RI spend commit over time.
That gives you an idea of how much spend you've committed to, and for how long.
No comments:
Post a Comment