Sub topic 16
Controlling cost and cleaning up
This chapter assumes you can use the AWS CLI, know what an RDS instance, snapshot, and read replica are, and have followed the earlier chapters on provisioning, backups, Multi-AZ, and replicas. It covers what an RDS bill is made of, how to reduce it, and how to delete test resources safely.
What you pay for
An RDS bill has several separate line items. A database you are not using can still produce most of them.
| Cost component | Charged for | Keeps charging when idle? |
|---|---|---|
| Instance hours | Instance class, engine, and Region, per hour or second | Yes, until the instance is deleted. Stopping pauses it temporarily. |
| Provisioned storage | GB allocated, plus provisioned IOPS and throughput on some storage types | Yes, even while stopped |
| Multi-AZ | Roughly double the instance and storage cost, since a standby exists | Yes |
| Read replicas | Each replica is a full instance with its own storage | Yes |
| Backup storage | Automated backups and manual snapshots beyond the free allowance | Yes, until they expire or you delete them |
| Data transfer | Traffic leaving the Region or crossing Regions, such as cross-Region replicas | Only when traffic flows |
| Extras | Performance Insights beyond the free retention, Enhanced Monitoring logs, RDS Proxy | Yes |
Prices vary by Region and engine, so check the RDS pricing page for your Region instead of relying on remembered numbers.
The free allowance for backup storage equals the size of your provisioned database storage in that Region. Storage for manual snapshots is not covered by the automated-backup allowance, and it persists until you delete the snapshot.
Reduce the cost of what you run
Right-size the instance
Start from measured load, not guesses. Review CloudWatch metrics such as CPUUtilization, FreeableMemory, and DatabaseConnections over at least a few weeks. An instance that sits at 5 percent CPU with plenty of free memory is a candidate for a smaller class. Changing the class requires a short outage on a single-AZ instance, and on Multi-AZ the outage is roughly a failover. Do it in a maintenance window.
Burstable classes (the db.t4g family) suit development and light workloads. They accrue CPU credits when idle and spend them under load. If a burstable instance runs out of credits, performance is throttled, or in unlimited mode you pay for the surplus. Sustained high CPU belongs on a general-purpose class such as db.m or db.r.
Graviton-based classes (names with a g, like db.t4g and db.m7g) usually give better price-to-performance than the Intel or AMD equivalents, if your engine version supports them.
Pick the right storage
gp3 storage lets you set IOPS and throughput separately from size, and its baseline is included in the storage price. Provisioned IOPS (io1/io2) costs much more, so use it only when you have measured a need for sustained high IOPS. Storage can grow but cannot shrink. If you over-allocate, the only way down is to migrate to a new, smaller instance, for example with a dump and restore or a logical replication tool. Allocate modestly and use storage autoscaling (--max-allocated-storage) to grow on demand.
Match availability to the environment
Production usually needs Multi-AZ. Development and test environments usually do not, and turning it off roughly halves their cost. The same applies to read replicas: create one for a measured read-scaling need, not "just in case". Each idle replica costs as much as a standalone instance of the same class.
Commit for steady workloads
If an instance will run continuously for a year or more, Reserved Instances (and database Savings Plans, where offered for your engine) reduce the hourly rate in exchange for a one-year or three-year commitment. Reserved Instances apply to a class and engine within a Region, and in most cases they are size-flexible within an instance family. Buy them only after the instance size has settled, because a reservation you cannot use is still billed.
Stop what you do not use all day
You can stop a single instance for up to seven days. After that, RDS starts it again automatically. While stopped, you do not pay instance hours, but you still pay for storage and backup storage. Stopping is not available for every configuration. For example, Multi-AZ SQL Server instances and instances with read replicas have restrictions, so check the current documentation for your engine.
aws rds stop-db-instance --db-instance-identifier demo-pg
aws rds start-db-instance --db-instance-identifier demo-pgFor a development database used only in office hours, schedule these calls with Amazon EventBridge Scheduler or a small Lambda function. Because of the seven-day auto-restart, a scheduled stop must run again at least weekly.
Trim backup and monitoring spend
- Lower
--backup-retention-periodfor non-production instances. Each day of retention adds backup storage. - Delete manual snapshots you no longer need. They never expire on their own.
- Keep Performance Insights at the free retention period unless you need longer history.
- Set Enhanced Monitoring to a coarser interval, or turn it off, on instances where CloudWatch metrics are enough.
Find what is costing money
You cannot control cost you cannot see. Use these tools together:
- Cost Explorer, filtered by service "Relational Database Service" and grouped by usage type, shows whether instance hours, storage, or backup storage dominate.
- Cost allocation tags, such as
env=devandowner=alice, applied to instances and snapshots and activated in the Billing console, let you attribute spend to teams and spot orphans. - AWS Budgets sends an alert when actual or forecast spend crosses a threshold. Create one for the RDS service on every account where people experiment.
Tagging at creation is cheapest. Add --tags Key=env,Value=dev Key=owner,Value=alice to create-db-instance. You can add tags later with aws rds add-tags-to-resource.
Clean up safely
Deleting an RDS instance is irreversible, so the order and the options matter. The following diagram shows the decision.
Delete the instance
Many instances have deletion protection turned on. It blocks delete-db-instance until you turn it off, which is a deliberate guard against mistakes. Disable it only after you confirm the target identifier.
aws rds modify-db-instance \
--db-instance-identifier demo-pg-restored \
--no-deletion-protection \
--apply-immediatelyFor a throwaway instance such as the restored test copy from the backups chapter, skip the final snapshot:
aws rds delete-db-instance \
--db-instance-identifier demo-pg-restored \
--skip-final-snapshotIf you might need the data, name a final snapshot instead:
aws rds delete-db-instance \
--db-instance-identifier demo-pg \
--no-skip-final-snapshot \
--final-db-snapshot-identifier demo-pg-finalBy default, delete-db-instance also deletes automated backups, which is why the final snapshot matters. You can keep them with --no-delete-automated-backups, but they then still cost money until their retention period ends.
Remove what the instance leaves behind
Deleting the instance does not remove everything it was attached to. Walk through this list:
- Read replicas. Delete replicas first or promote them. You cannot delete a source instance while replicas depend on it in some configurations, and a replica you forget keeps billing.
- Manual snapshots. List them with
aws rds describe-db-snapshots --snapshot-type manualand delete the ones you do not need withaws rds delete-db-snapshot. - Retained automated backups. If you kept them, list them with
describe-db-instance-automated-backupsand remove them withdelete-db-instance-automated-backups. - Secrets Manager secret. The managed master secret is deleted along with the instance when you used
--manage-master-user-password. Secrets you created yourself must be removed separately, and they are billed per secret per month. - Custom parameter groups and DB subnet groups. These are free, but removing them keeps the account tidy. A parameter group cannot be deleted while an instance uses it.
- Security groups and KMS keys. Remove security groups you created for the test. A customer-managed KMS key costs money monthly, and deleting one is scheduled with a waiting period of 7 to 30 days. Do not delete a key that still encrypts snapshots you want to keep.
- CloudWatch log groups. Logs exported from RDS go to log groups that keep data until their retention expires. Set a retention period or delete the group.
A short script can list what is left across one Region:
import boto3
rds = boto3.client("rds")
for db in rds.describe_db_instances()["DBInstances"]:
print("instance", db["DBInstanceIdentifier"], db["DBInstanceStatus"])
for snap in rds.describe_db_snapshots(SnapshotType="manual")["DBSnapshots"]:
print("snapshot", snap["DBSnapshotIdentifier"], snap["AllocatedStorage"], "GB")
for backup in rds.describe_db_instance_automated_backups()["DBInstanceAutomatedBackups"]:
print("retained backup", backup["DBInstanceIdentifier"], backup["Status"])Run it in every Region you have used. RDS resources are Regional, so a forgotten instance in a Region you rarely visit is the most common source of surprise bills.
Common mistakes
- Assuming stop means free. A stopped instance still charges for storage and backups, and restarts itself after seven days.
- Leaving manual snapshots behind. They persist after the instance is gone and are billed monthly.
- Forgetting Multi-AZ in non-production. It doubles the cost for availability nobody needs in a test environment.
- Over-allocating storage. You cannot shrink it later.
- Buying reservations too early. Resize first, then commit.
- Cleaning up in one Region only. Check each Region where you worked.
Exercise
You finish a lab. You delete the instance demo-pg with --skip-final-snapshot, but next month's bill still shows RDS charges. Name three places the charges could come from, and say how you would find and remove each.
Show answer
Possible sources are (1) manual snapshots, which never expire: list them with aws rds describe-db-snapshots --snapshot-type manual and remove them with delete-db-snapshot; (2) retained automated backups, if the instance was deleted with --no-delete-automated-backups: list them with describe-db-instance-automated-backups and remove them with delete-db-instance-automated-backups; (3) a read replica that was promoted or never deleted, which is a full instance: find it with describe-db-instances and delete it. Other valid answers include resources in another Region, a customer-managed KMS key, a self-created Secrets Manager secret, or Performance Insights and log data in CloudWatch. Cost Explorer grouped by usage type shows which line item is responsible.
Exercise
A development database runs Multi-AZ on db.m6i.xlarge with io1 storage and a 35-day backup retention. It is used only on weekdays and averages 4 percent CPU. List four changes that would lower its cost and say which one carries a restriction or risk.
Show answer
Disable Multi-AZ, move to a smaller or burstable Graviton class such as db.t4g, switch from io1 to gp3, lower backup retention (for example to 1 to 7 days), and schedule stop and start for nights and weekends. Risks: changing the instance class causes downtime or a failover, so do it in a maintenance window. Stopping lasts at most seven days before RDS restarts the instance, so the schedule must repeat. Storage cannot be shrunk, so if the allocation is also too large, you would need to migrate to a new instance.