www.awsgood.com
EstimatedCharges 告警,以及定时缩容到零加每周孤儿资源清扫。我大概已经有过二十多次这种对话。有人打开 Cost Explorer,筛选 EC2,看到 40% 的跳升,然后怪上周二加的自动扩缩组。
那个组没花任何钱。它只是让 40 个实例同时存在了。
EC2 按秒计费,最低 60 秒,所以短时突发仍然便宜。一个 m6i.large 根据区域和类型不同,每小时大约一毛钱。保持 200 个这样的实例一天,在算上 EBS 和 NAT 之前你大约要花 480 美元——如果因为没人注意到而保持一周,那就是一辆二手车的钱。
| 账单项目 | 谁计费 | 是否由该组驱动? |
|---|---|---|
| EC2 实例秒数 | EC2 | 是——通常占金额的 80% 以上 |
| EBS 卷 GB-月 + 快照 | EBS | 是,每个实例一个根卷 |
| ALB/NLB 小时数 + LCU | ELB | 部分——LCU 跟踪的是流量,不是节点数 |
| CloudWatch 告警 | CloudWatch | 是,每个扩缩容策略一到两个 |
| 详细监控 | CloudWatch | 是,按实例计费,而且很少有人看 |
| 数据传输 + NAT 处理 | VPC | 是——这是人们最容易漏掉的一项 |
NAT 那一项最隐蔽。一个 worker 组扩到 30 个节点,并且每次启动都拉取容器镜像或访问外部 API,会把 30 美元的 NAT 网关变成 300 美元的处理费,而实例花费才翻了一倍。
策略、扩缩容活动和启动模板版本成本为零。删掉十几个旧的启动版本不会让账单变动一分钱。
在第一天就设置 MaxSize,而不是等事故之后。我的默认值是预期峰值的 2 倍,提高它要有意为之,并写下原因。
然后打开账单开关。在 Billing 控制台中,必须先开启 “Receive Billing Alerts”,CloudWatch 中才会存在 EstimatedCharges 指标。在此之前,put-metric-alarm 会成功,而告警会永远停在 INSUFFICIENT_DATA——仪表盘一片绿色,防护为零。
# 必须是 us-east-1;账单指标只存在于这个区域
aws cloudwatch put-metric-alarm
--alarm-name "acct-monthly-150usd"
--namespace AWS/Billing --metric-name EstimatedCharges
--dimensions Name=Currency,Value=USD
--statistic Maximum --period 21600 --evaluation-periods 1
--threshold 150 --comparison-operator GreaterThanThreshold
--alarm-actions arn:aws:sns:us-east-1:111122223333:finops
--region us-east-1
Six-hour periods are deliberate. The metric refreshes a few times a day, and 5-minute periods just produce flapping `INSUFFICIENT_DATA` noise that everyone learns to ignore.
The second layer should be an actual budget, not just a friendly email:
```bash
aws budgets create-budget --account-id 111122223333
--budget '{"BudgetName":"acct-monthly","BudgetLimit":{"Amount":"400","Unit":"USD"},"TimeUnit":"MONTHLY","BudgetType":"COST"}'
--notifications-with-subscribers '[{"Notification":{"NotificationType":"ACTUAL","ComparisonOperator":"GREATER_THAN","Threshold":80,"ThresholdType":"PERCENTAGE"},"Subscribers":[{"SubscriptionType":"EMAIL","Address":"ops@example.com"}]}]'
A budget action that attaches a deny on `ec2:RunInstances` past the threshold is blunt. It has also saved me twice, and explaining a stuck deploy beats explaining a four-figure weekend.
## Layer 2: stop the group from chasing load it can't afford
Scale-out is fast and scale-in is slow. That asymmetry is the whole cost problem.
One worker group of mine went from 4 instances to 40 between 01:00 and 03:00. A consumer had wedged on a poison message, the queue depth climbed, and the group did exactly what I configured it to do. Cross-checking `describe-scaling-activities` against Cost Explorer's daily EC2 line pinned the spike to the hour; `GroupTotalInstances` in CloudWatch showed 40 nodes held until 08:40. About $380 for one night of a queue that never drained.
The fix was three config changes: MaxSize 16, `ScaleInCooldown` at 600 seconds, and a step adjustment that stops scaling out once messages-per-instance drops below a sane ratio. I verified it by replaying the same metric range through the new thresholds before trusting it — the policy would have flattened at 16 and stayed there.
| Knob | Default worth changing | Why |
|---|---|---|
| MaxSize | 2× expected peak | A hard ceiling beats any alarm |
| ScaleInCooldown | 300s → 600s for batch | Stops out/in flap on spiky queues |
| Detailed monitoring | Off | 1-minute metrics nobody reads |
| Mixed instances policy | capacity-optimized | Spot at 60-70% off for stateless work |
For stateless web and worker tiers, a mixed instances policy with `capacity-optimized` and a spot base around 60-70% is the single biggest lever you have. If you've ever seen `InsufficientInstanceCapacity` at 2am, that's one subnet running dry — spreading the group across three AZs and four instance types fixes it more reliably than any retry logic.
## Layer 3: scheduled off-hours and the weekly orphan sweep
Non-prod groups should be at zero most of their life.
```bash
aws autoscaling put-scheduled-update-group-action
--auto-scaling-group-name web-dev
--scheduled-action-name nights-off
--recurrence "0 20 * * MON-FRI"
--min-size 0 --max-size 0 --desired-capacity 0
Recurrence is UTC. For a team in UTC+8, a 20:00 UTC shutdown is 04:00 local and the 06:00 UTC start is 14:00 — the fleet is dark through the entire local morning. Check the offset before you schedule anything.
Also set min and max to 0, both of them. A scheduled action that only zeroes desired capacity gets overridden on the next target-tracking evaluation, typically within a minute or two, and you'll swear the schedule never fired.
Monday sweep for what autoscaling leaves behind:
```bash
aws ec2 describe-volumes --filters Name=status,Values=available
--query 'Volumes[].[VolumeId,Size,CreateTime]' --output table
aws ec2 describe-addresses
--query 'Addresses[?AssociationId==null].[PublicIp,AllocationId]' --output table
aws autoscaling describe-auto-scaling-groups
--query 'AutoScalingGroups[?length(Instances)==`0`].[AutoScalingGroupName,MinSize,MaxSize]'
--output table
That last query finds groups sitting at zero instances but still attached to a load balancer. The group is free; the ALB it's wired to is not.
## So the bill jumped. What do I check first?
Daily EC2 cost grouped by usage type, then scaling activities. In that order, always.
```bash
aws ce get-cost-and-usage
--time-period Start=2025-06-01,End=2025-06-08
--granularity DAILY --metrics UnblendedCost
--filter '{"Dimensions":{"Key":"SERVICE","Values":["Amazon Elastic Compute Cloud - Compute"]}}'
--group-by Type=DIMENSION,Key=USAGE_TYPE
aws autoscaling describe-scaling-activities
--auto-scaling-group-name workers --max-items 20
--query 'Activities[].[StartTime,StatusCode,Description]' --output table
The sanity math takes ten seconds: instance-hours ÷ 24 = average fleet size. If that average exceeds your peak, something scaled out and never came back — or `DeleteOnTermination` is false and you're paying for volumes from instances that died in March.
Most SMB accounts I look at don't need a smarter policy. They need a ceiling, an alarm that actually leaves `INSUFFICIENT_DATA`, and someone to read the sweep output each Monday. When that Monday sweep needs to happen without you, it's part of what we run at awsgood.com.
Go set MaxSize on your busiest group right now.