How to stop dev EC2 and RDS outside working hours (and why RDS starts again after 7 days)
Development and test environments usually run all week but are used only during working hours. For on-demand EC2 instances and RDS databases you pay for compute while they run, so stopping them at night and at weekends is one of the simplest ways to cut a non-production bill.
Do it yourself
- Tag what may be stopped, for example
schedule=office-hours, so a script never touches production by mistake. Better still, use an IAM condition on that tag so the stop role cannot act on anything else. - Stop and start with the CLI:
aws ec2 stop-instances --instance-ids i-0abc123 aws ec2 start-instances --instance-ids i-0abc123 aws rds stop-db-instance --db-instance-identifier dev-db aws rds start-db-instance --db-instance-identifier dev-db aws rds stop-db-cluster --db-cluster-identifier dev-aurora - Auto Scaling groups: do not stop their instances. The group sees them as unhealthy and launches replacements.
Save the current sizes and scale to zero instead, then restore the saved sizes in the morning:
aws autoscaling update-auto-scaling-group --auto-scaling-group-name dev-asg \ --min-size 0 --max-size 0 --desired-capacity 0 - Run it on a schedule. EventBridge Scheduler supports cron expressions with a time zone and can call AWS APIs
such as
StopInstancesdirectly, or trigger a Lambda function that finds tagged resources and stops them.
Why RDS starts again after 7 days
Amazon RDS lets you stop a DB instance or an Aurora cluster for up to 7 days at a time. After that, RDS starts it again automatically so it does not fall behind on required maintenance. A database you stopped on Friday for a two-week holiday will be running, and billing, again a week later. If you do nothing, it stays running.
The fix is to notice the automatic start and stop it again. RDS publishes an event when it starts a database for this
reason. You can subscribe to RDS events, or catch them with an EventBridge rule, and call
stop-db-instance or stop-db-cluster again. A simpler alternative is to have your nightly stop job
run every night, including weekends and holidays, so a database that started itself is stopped again within a day.
Other gotchas
- Stopping does not stop every charge. EBS volumes, RDS storage, backups and Elastic IP addresses are still billed.
- A stopped EC2 instance without an Elastic IP gets a new public IP address when it starts, and instance store data is lost on stop.
- The hardest part is people, not scripts. When a developer needs a box at 9 pm and has to ask ops, the schedule tends to get switched off and never comes back.
How Dev Environment Off-Hours Scheduler for AWS automates it
You deploy one CloudFormation stack that creates an IAM role. It can only start and stop resources that carry the tag you choose, enforced by IAM conditions. We run schedules with a time zone, weekday hours, overnight periods and a holiday list, for EC2 instances, RDS instances, Aurora clusters and Auto Scaling groups (sizes saved and restored). When AWS starts a database after 7 days, we stop it again and log it. Developers wake a schedule themselves for 2 hours, 4 hours or until the next morning with a link or a Slack command, and it stops again by itself. Every schedule starts in dry run, and every action is logged.