Spend Protection

Spend protection watches what each resource of a stage spends. When one resource spends far above its own normal, you get an alert, usually 10 to 20 minutes after the spending started. When the cause can be stopped without losing work, spend protection can stop it: for example, it disables the queue trigger of a function that nothing else invokes, so that new messages wait in the queue.

It does not put a bound on your bill. It sees only the resources listed below, its estimate leaves some charges out, and an alert comes minutes after the spending started. Use it next to budgets, not instead of them.

Turn it on

stackConfig:
  spendProtection:
    alarm:
      timesNormal: 5
      minimumUsdPerHour: 1
    stop: ask
  • alarm.timesNormal (default 5): how many times its normal a resource must spend before it alerts.
  • alarm.minimumUsdPerHour (default 1): below this, in US dollars an hour, nothing alerts.
  • alarm: false turns spend protection off for the stage.
  • stop: ask, automatic or never. See What gets stopped.

Every deploy prints how many resources spend protection watches and what the monitoring costs a month.

The alert goes to the Stacktape Console (alarm history and incidents) and to the Console channels of your organization-wide alarms, like other alarm notifications. It says what the resource spends an hour, how that compares with its normal and what it would cost a day, and links to the stage's Costs tab, where a waiting stop can be approved. When a stop that waits for a person is proposed after the first alert, for example once the 30 minutes after a deploy are over, one follow-up goes to the same places. A stack that is not connected to the Console gets no message; stacktape spend list still shows everything.

How it decides

Every five minutes a small function in your stack (the stage check) reads CloudWatch metrics and turns them into dollars an hour, with the list prices that the deploy wrote into the stage's settings.

  • Normal. A resource's normal is a busy five minutes of the last 7 days: the 99th percentile of its five-minute estimates. Earlier alerts are left out, so a runaway does not become the new normal.
  • History. A resource needs 24 hours of history before Stacktape knows its normal, counted from when spend protection first saw it. A replaced resource, or a function with another memory size or a service with another task size, starts over. Until then it alerts only above minimumUsdPerHour, and nothing is stopped.
  • Two windows. It alerts when a resource spends more than timesNormal times its normal, and more than minimumUsdPerHour, in two complete five-minute windows in a row. A five-minute spike does not alert.
  • After a deploy. For 30 minutes after a deploy finishes, and after spend protection starts watching a resource, nothing is stopped. A new version can change what is normal. Alerts still go out, and if the spending is still high when the 30 minutes are over, a stop is decided then.
  • Late data. It decides only on windows that ended at least three minutes ago, and reads the last 30 minutes every time. High windows whose data arrived late still raise an alert; if the spending was already back under the level by then, the alert says so and nothing is stopped. A metric that could not be read counts as unknown, never as no spending.

The alert says what is spending, about how much an hour, against which normal, since when, and the last deploy before it. A second message says when the spending is back under the level. A message that cannot be delivered is sent again on every check until it is.

What it watches, and what it cannot see

ResourceEstimateNot counted
FunctionDuration × configured memory × the GB-second price, plus invocations × the request priceRounding to the next millisecond, ephemeral storage above 512 MB, provisioned concurrency (its execution is priced at the on-demand rate), the initialization phase, free tier and volume discounts, data transfer. A version still running with an older memory size is priced at the current one
Container service (Fargate)Running tasks per minute × the task's vCPU and memory prices. Approximate: CloudWatch has no exact running-task count without Container Insights, so the task count comes from utilization samples. The alert says soStorage above 20 GB, public IPv4 addresses, load balancers, data transfer, the Spot discount (Spot tasks are priced at the on-demand rate)
Aurora Serverless v2Average capacity units of each instance × the ACU-hour priceStorage, I/O, the I/O-Optimized rate, backups, RDS Proxy
DynamoDB table, on-demandConsumed write and read units × their pricesGlobal secondary indexes, storage, backups, streams, global table replication
HTTP APIRequests × the request priceRequests counted once per 512 KB, the lower rate above 300 million a month, data transfer
Log ingestion (functions and containers)Incoming bytes × the ingestion priceStorage, Logs Insights, the Infrequent Access class, subscription filters
NAT gatewayBytes through the gateway × the processing price, plus bytes to the internet × the data transfer priceThe hourly charge, the free 100 GB of data transfer, traffic to AWS services in the same region (counted as internet traffic, an overestimate)
CDN (CloudFront)Bytes downloaded × the North America and Europe priceRequests, other edge locations (they cost more), the free 1 TB, edge functions

Spend protection cannot see anything else: durable workflows (durable-workflow resources), provisioned capacity (it bills the same however much it is used), EC2 instances and container services on EC2, load balancers, storage, other data transfer, Bedrock and other services billed per token or request, and third-party charges.

Amounts are estimates from metrics and list prices. Your bill can differ: credits, savings plans and free tiers are not known.

How late an alert can be

Measured on AWS in eu-west-1 on 1 December 2026, in four runs of the spend protection canary: a queue-triggered function suddenly spending about $0.23 an hour against a normal of $0.005, on a stage marked non-production.

From the start of the spending toRun 1Run 2Run 3Run 4
The alert (function with 24 hours of history)12.7 min16.7 min11.8 min12.5 min
The trigger disabled by spend protection13.1 min17.4 min12.4 min12.7 min
The alert (function without history, absolute rule only)14.9 min14.9 min9.9 min9.9 min

The runs cleared the record of the deploy that created the stage, so the 30 minutes after a deploy did not apply. The time depends on where the spending starts within a five-minute window and on when the next check runs. Expect between about 10 and 20 minutes. A function whose invocations run for many minutes is later: Lambda reports an invocation's duration when it ends.

How long after a minute ends CloudWatch stops changing that minute's data (runs 2 to 4, 13 to 18 minutes of traffic each):

DataMedianSlowest
Function duration and invocations65–76 s91 s
Log ingestion28–36 s47 s
HTTP API requests76–85 s93 s
Queue messages sent45–63 s77 s

The check waits three minutes after a window ends before it uses it. Container services, Aurora, DynamoDB, NAT gateways and CloudFront were not measured; CloudFront publishes its metrics in us-east-1 and can be later.

What gets stopped

A stop happens only for causes that can be stopped without losing work. Each kind of resource has one way to stop it:

WayUsed forWhat it does to work in flight
Disable the queue triggerA function whose only way in is one SQS queue of the stack: no other trigger, HTTP route, function URL, schedule, topic, bucket notification, and no other resource that invokes it (connectTo, a destination, a state machine)Invocations already running finish. New messages wait in the queue. Messages the poller already received become visible again after the queue's visibility timeout.
Set reserved concurrency to 0Any other functionEvery way in is throttled. Synchronous callers, HTTP routes included, get an error. Queue and stream triggers wait. New asynchronous events go straight to the function's failure destination or dead-letter queue, and are lost without one. Never offered for a function with provisioned concurrency, nor for a function that answers public requests (HTTP API, load balancer, WebSocket, function URL) on a stage not known to be non-production.

Databases, NAT gateways, CDNs, log groups and HTTP APIs are never stopped: their spending rarely comes from one cause that can be stopped safely. They only alert.

Container services only alert too. A service never runs more tasks than the scaling.maxInstances in your config, so that number already limits what it can spend. The alert names it; to spend less, lower it and deploy.

Who decides:

  • Disabling a queue trigger happens without a person on a stage known not to be production (stackConfig.stageType: non-production, the type chosen in the Console, or a name such as dev or staging), or anywhere with stop: automatic. Right before it acts, spend protection asks AWS again whether anything else invokes the function (another trigger, or a permission in its resource policy). If anything does, the stop waits for a person.
  • Setting concurrency to 0 always waits for a person, for 6 hours.
  • stop: ask: every stop waits for a person. stop: never: only alerts.

Production asks by default because public traffic can cross the same level as a malfunction, and a launch or a scheduled batch looks like a runaway.

A person approves by answering the stop's own workflow run, with stacktape spend approve or in the Console. That needs the IAM permission lambda:SendDurableExecutionCallbackSuccess on the stage's spend-stop workflow. Starting the workflow by hand approves nothing. An approval names the exact stop it was shown: the release, the resource as it was then, the stage type and the stop setting. If any of that changed since, nothing is stopped.

Every stop ends by itself after at most 24 hours. A disabled queue trigger ends earlier: before the oldest waiting message would reach the queue's retention period, with a margin of 2 hours or a quarter of the retention, whichever is larger. SQS leaves messages that were received three times or more out of its age metric, so spend protection takes the oldest message to be at least as old as the time since the spending started plus the oldest age the metric showed just before. When messages wait and CloudWatch has no age of the oldest one from the last five minutes, the trigger is not disabled. Neither is it when the queue cannot keep its messages for at least an hour, nor when the queue is another queue's dead-letter queue: its messages expire counted from when they were first sent to the other queue. The end of a disabled trigger is scheduled in your account when the trigger is disabled, with a one-time EventBridge Scheduler entry, so it ends on time even if the stage check, the workflow or a deploy fails.

When a stop ends, spend protection restores exactly what was there, item by item: the trigger's state or the function's reserved concurrency (none, if it had none). A value that someone changed meanwhile is left as it is. If AWS refuses to set the old reserved concurrency back, because the account must keep a minimum of unreserved concurrency, the reserved concurrency is removed instead and the stop's note says so. An item that cannot be restored yet is tried again; the stop is recorded as in effect until every item is done.

After a stop ends, the work it held back is processed first: spend protection does not stop the same resource again until two complete five-minute windows after the end were under the alert level.

Commands

stacktape spend list --stage staging --region eu-west-1
stacktape spend approve <stop-id> --stage staging --region eu-west-1
stacktape spend reject <stop-id> --stage staging --region eu-west-1
stacktape spend undo <stop-id> --stage staging --region eu-west-1
  • spend list shows the alerts and stops of the last 7 days, with what each stop holds back and a projection of what was not spent. A projection is the rate when the stop started times the hours it lasted; it is not a measurement.
  • spend approve carries out a proposed stop. On a stage not known to be non-production it asks for confirmation; --yes confirms.
  • spend reject drops a proposed stop. The alert stays.
  • spend undo ends a stop now and restores exactly what was there.

Approve, reject and undo answer the stop's own workflow run; none of them starts a run. With --agent, each command returns JSON. The MCP server offers spend list as read-only and the others as changing commands. The Console shows the same alerts and stops, with Approve, Refuse and Undo buttons.

Deploys, rollbacks and deletes

Stops and proposals belong to the release they were made for. Before a deploy, a rollback or a stacktape dev update, the CLI ends every stop in effect and restores exactly what was there, drops every proposed stop, and closes open alerts with a message. If the spending goes on with the new release, spend protection alerts again, and decides a stop after the 30 minutes that follow a deploy. A rollback never carries a stop into a release whose settings would not have allowed it. A delete removes the resources together with their stop. A resource that CloudFormation keeps when it deletes the stack (DeletionPolicy: Retain) is the exception: its stop ends and what was there is restored before the delete, because nothing would end it afterwards.

A deploy never waits on spend protection. When AWS does not let the CLI restore an item in time, for example a throttled API or a queue trigger that is still changing its state, the deploy, rollback or dev update prints a warning and goes on. The stop stays recorded as in effect, and the stage check restores what is left within minutes after the command. The CLI stops instead before a delete, before a deploy that turns spend protection off or sets stop: never, and before a deploy after which spend protection no longer stops that resource the same way. In those cases the stage check could not restore it afterwards. To go on anyway in an urgent fix, see STP_STAGE_FORCE.

A deploy, rollback or delete that was interrupted (a closed terminal, a killed CI job) keeps the stage locked until the next deploy, rollback or delete of that stage. Until then spend protection still reads the spending and alerts, but stops nothing: the alert says that nothing acts in the stage until someone runs the command. The stage check also sends one alert that the stage is paused. A disabled queue trigger still ends at its end time, because its schedule runs in AWS. A stop that set a function's concurrency to 0, which a person approved, ends at the next deploy, rollback or delete. The stage check alerts about such a stop as soon as the stage is paused, with its end time and the command that ends the pause. Run the deploy again soon.

What the monitoring costs

The check itself bills in your AWS account. CloudWatch charges for each metric it reads ($0.01 per 1,000), so the cost grows with the number of resources: a function reads three metrics (duration, invocations, log ingestion), a container service two, a DynamoDB table two, an HTTP API one. At list prices, before free tiers:

StackMetrics read every five minutesMetric readsStack metricCheck functionA month
5 functions15$1.40$0.39$0.05about $1.84
20 functions60$5.62$0.39$0.05about $6.05
100 functions300$28.08$0.39$0.05about $28.52

Each metric is read every five minutes for the last 30 minutes, and once an hour for the last 7 days; CloudWatch charges per metric, not per data point. The check function itself ran about 1.0 s per tick at 256 MB in our measurement (1.6 s at most). Reading 300 metrics took 0.2 s per tick and 2 s for the hourly history, so a large stack adds little compute.

Spend protection creates no CloudWatch alarm. It writes one metric for the stack, EstimatedSpendUsdPerHour in the Stacktape/Cost namespace, and one more for a resource only while it spends above its alert level. A stack with a function whose queue trigger can be disabled also gets an EventBridge Scheduler schedule group and a role for the end schedules; a schedule exists only while a stop is in effect, and its cost is negligible.