AWS Logo
Menu

Stop EC2 instances and RDS/Aurora databases overnight by tagging them with cron schedules

Stopping resources overnight is a common AWS cost-saving practice. Tag-based solutions often come with a big system for managing schedules. I put cron schedules in the tags.

Published Apr 14, 2025
Some might measure productivity by lines of code written πŸ“œ, but I value making code shorter and easier to understand. I recently updated Lights Off, an open-source tool for shutting off EC2 instances and RDS/Aurora databases to cut AWS costs. πŸ’°
Try it if you have dev/test environments that sit idle overnight or on weekends! Creating a stack from the CloudFormation template takes a few clicks. Later, if you like it, you can deploy it to multiple regions and/or accounts as a StackSet. You can wrap a StackSet in Terraform, too.
Tag 🏷️ instances and databases with schedules like:
This minimal example turns an instance or database on at 07:30🌞 and off at 17:00🌜, every day. You can of course vary the times by day of week or day of month πŸ“…. You get the flexibility of cron, with field labels for clarity.
  • The field labels come from strftime(). The date and time values are ISO 8601-standard. Schedule expressions…
  • Converting to/from UTC is annoying. The example works as-is, if you happen to be in Great Britain πŸ‡¬πŸ‡§ in winter ❄️. Don't want to worry about time zones and daylight saving time? Pre-calculated, year-round schedules for North America, Europe and India are in the ReadMe.
  • More and more EC2 configurations support hibernation. Apply the sched-hibernate tag instead of sched-stop.
Lights Off stops and starts tagged EC2 instances and RDS/Aurora databases, but if you're an advanced user, you can also make it delete and recreate arbitrary kinds of infrastructure in your own CloudFormation stacks, based on schedules in stack tags. πŸ’‘
As always, I demonstrate least-privilege security, with Identity and Access Management policies that are easy to read and verify. Components are thoughtfully named and thoroughly documented. You can use this CloudFormation template to learn about serverless architecture, Lambda, EventBridge, Simple Queue Service (SQS), AWS Backup, and IAM.
New Features
πŸ’Ύ Whether you're installing in one region and AWS account or many, there's no need to copy Lambda source code to S3 first. The CloudFormation template is now self-contained. Thank you, AWS, for raising the inline limit!
πŸ›… The sched-backup tag now triggers AWS Backup to create EC2 instance images (AMIs), EBS volume snapshots, and RDS/Aurora database snapshots. You can manage different kinds of backups in one place and take advantage of lifecycles (transition to cheaper storage, delete), but without having to write AWS Backup plans. I've been using AWS since 2013, and AWS Backup, of all things, has become one of my favorite services. It's so comprehensive and well-implemented.
I wrote the first version of this back in 2017. In hopes of retiring it, I regularly check whether someone else has done it better. The closest alternative I've found on GitHub is AWS's own Instance Scheduler. Excluding blanks, comments and tests, it has over 9,500 lines of Python code. Lights Off has under 600 lines of Python; in this update, I managed to cut βœ‚οΈ 100 lines.
Let me know what you think. How can I make Lights Off more useful for you?
Β 

Comments