Fund the accrual, not the invoice
My bank automation splits every paycheck into per-bill envelopes: rent here, power there, each one getting topped up to whatever that bill actually is. Electric and water work because the utility publishes a balance I can read before payday.
AWS doesn't. The paycheck hits at 08:00 on the first; AWS gets around to issuing the invoice a couple of days later and charges the card on the third. By the time there is a bill to budget against, the money has already been spent on something else. The amount isn't trivial or stable either — it has swung between $24 and $105 in the last six months, so "set aside a fixed amount" is wrong in both directions.
The fix was realizing I was waiting for the wrong artifact. The invoice is a PDF rendered days late; the cost is known continuously:
ce.get_cost_and_usage(
TimePeriod={"Start": "2026-10-01", "End": "2026-12-01"},
Granularity="MONTHLY", Metrics=["UnblendedCost"])
On the first of the month that returns a closed-out total for last month that matches the invoice to the cent, plus the running month to the day. So payday stops asking "what do I owe?" and asks "what have I already run up?"
owed = unpaid_prior + month_to_date
target = max(MIN_DUE, ceil(max(owed * 1.10, owed + 5)))
Three things made it safe enough to leave alone:
Top up to a target, never add an amount. Every path computes target - balance and moves the difference. That one choice is what lets the estimate be sloppy: a 10% buffer that turns out to be too much isn't a leak, it's just a smaller transfer next month. If I had written transfer(estimate) I'd be reconciling an envelope that slowly fills with other people's money.
Subtract charges, don't set a "paid" flag. My first version asked "has the invoice been charged yet?" and answered it with "is there a debit on this envelope this month?". That's a boolean standing in for arithmetic, and it breaks on everything interesting: a one-off domain renewal on the second, a card verification hold, a partial charge, a borrow against the envelope that I pay back later. Subtracting instead — unpaid = max(0, prior_month - charged_so_far) — handles all of those without a single special case, and over-subtracting self-corrects within a day because the one-off shows up in month-to-date anyway.
Fall back to stale numbers, not to zero. If the cost API is unreachable at 08:00 on the first, the worst possible behaviour is "skip the bill this month". It now funds from yesterday's cached figures and flags the estimate as stale. Wrong by a day beats absent.
The daily safety net runs after 09:00, only until the charge clears, and only ever moves the shortfall — which in a normal month is nothing at all. It exists for the one case the payday estimate can't cover: a late usage record pushing the real invoice above the buffer, two days before the card gets hit.
What I'd do differently: I built the "is it paid" check as a boolean first and had to be talked out of it. The tell was that every failure mode I could think of needed its own if. When your predicate keeps growing exceptions, the thing you actually wanted was a subtraction.