How to keep an RDS database stopped for more than 7 days
You stop a dev database on Friday, and a week later it's running again and billing by the hour. That's Amazon RDS working as designed. Here's why, and how to keep it off.
Why RDS starts your database again
You can stop an RDS DB instance or an Aurora cluster for up to seven days at a time. After that, AWS starts it automatically so it doesn't miss maintenance updates. Nothing tells you unless you've subscribed to RDS events, so the usual way people find out is the bill.
When it happens to a DB instance, RDS records event RDS-EVENT-0154 ("DB instance is being started due to it exceeding the maximum allowed time being stopped"). Aurora clusters log a matching cluster event.
What you pay while it's stopped, and after it restarts
- While stopped: no instance hours, but storage, provisioned IOPS and backups beyond the free allowance are still charged.
- After the automatic start: full instance hours until someone notices. A db.r6g.large left running for three weeks costs far more than the storage you were trying to save.
Some databases can't be stopped at all
- A read replica, or an instance that has a read replica.
- SQL Server with Multi-AZ.
For those, the options are deleting and restoring from a snapshot, or living with the cost.
Option 1: stop it again automatically
The simplest reliable approach is a small Lambda that runs every hour and stops any database you've marked to stay off. It doesn't need to catch the restart event; it just puts things back. You can't stop a database while it's still starting, so a periodic check is easier than reacting to the event and waiting.
Tag each database that should stay stopped with keep-stopped=true, then deploy this function:
import boto3
rds = boto3.client("rds")
def handler(event, context):
stopped = []
for page in rds.get_paginator("describe_db_instances").paginate():
for db in page["DBInstances"]:
tags = {t["Key"]: t["Value"] for t in db.get("TagList", [])}
if (tags.get("keep-stopped") == "true"
and db["DBInstanceStatus"] == "available"
and not db.get("DBClusterIdentifier")): # Aurora: stop the cluster instead
rds.stop_db_instance(DBInstanceIdentifier=db["DBInstanceIdentifier"])
stopped.append(db["DBInstanceIdentifier"])
return {"stopped": stopped}
Give its role rds:DescribeDBInstances and rds:StopDBInstance, and run it with an EventBridge Scheduler schedule of rate(1 hour). The worst case is about an hour of instance time each week.
For Aurora, do the same with describe_db_clusters and stop_db_cluster, checking the cluster's Status.
Two things to add before you rely on it:
- A CloudWatch alarm on the function's errors, so a broken function doesn't fail silently.
- A way to start the database again on purpose. Remove the tag first, or the function will stop it within the hour.
Option 2: snapshot it and delete it
If nobody will need the database for weeks, take a final snapshot, delete the instance, and restore from the snapshot when it's needed. You pay only for snapshot storage. The cost is time: a restore creates a new instance, which takes longer than a start and may come back with a new endpoint unless you reuse the identifier.
Option 3: Aurora Serverless v2 with automatic pause
Aurora Serverless v2 can scale to zero capacity units and pause after a period with no connections, from 5 minutes up to 24 hours. You aren't charged for instance capacity while it's paused, and it resumes when a connection arrives. It's a good fit for new dev databases on Aurora MySQL or PostgreSQL. Moving an existing RDS database to it is a migration, and the first connection after a pause waits for it to resume.
Option 4: a scheduler that already handles it
If you're scheduling databases anyway, check how your tool handles the seven-day start before relying on it. CostCapper treats it as expected rather than as someone turning the database on: it stops the database again if no schedule or request needs it, and records the event in its audit log.
Which to choose: parked for a week or two, use option 1. Parked for months, use option 2. A new low-traffic dev database, consider option 3. If you schedule several databases and app servers together, a scheduler saves you maintaining the pieces.