AWS Resource Start and Stop Scheduling for RDS, EKS and EC2
Save money and reduce your carbon footprint by streamlining your resources management effortlessly!
This article was originally written to cover the RDS script, which was the only one available at the time. Now, there are also scripts for managing EKS and EC2 start and stop times, working in a similar way so his article is still 100% relevant. For more details, please check the README.md file in the GitHub repository.
Introduction
Managing AWS RDS instances is crucial for many applications, but keeping them running 24/7 can rack up unnecessary costs — especially for development and testing environments. What if you could automate the start and stop times of your RDS instances to align with your team’s working hours? In this article, we’ll explore a straightforward solution using a Bash script and AWS Systems Manager to schedule your RDS instances, ensuring they’re running only when needed. Plus, we’ll dive into how this approach can save you money and reduce your carbon footprint.
Why leave the lights on when nobody’s home?
💸 The Problem: Unnecessary Costs and Environmental Impact
Running RDS instances continuously can lead to significant costs, particularly when those instances are idle during off-hours. Manually starting and stopping instances is not only tedious but also prone to human error. Missing a stop time could mean your instance runs all weekend, incurring costs without providing any benefit.
But it’s not just about the money. Continuous operation of cloud resources consumes energy, contributing to your organization’s carbon footprint. By optimizing resource usage, you not only save on costs but also contribute to environmental sustainability.
⏲️ The Solution: Automate with AWS Systems Manager and a Bash Script
By leveraging AWS Systems Manager State Manager and a custom Bash script, we can automate the scheduling of your RDS instances. The script sets up scheduled tasks to start and stop your database at specified times, such as during business hours from Monday to Friday. This automation reduces costs and frees you from the hassle of manual management.
Implementation: Step-by-Step Guide to Automation
Let’s dive into how you can set this up.
Prerequisites
Before you begin, ensure you have the following:
- AWS CLI installed and configured with a profile that has the necessary permissions.
- jq installed for JSON processing.
- Permissions to create IAM roles and policies, and to use AWS Systems Manager.
Step 1: Understand the Scheduling Requirements
We want the RDS instance to:
- 🟢 Start at 8 AM UTC from Monday to Friday.
- 🔴 Stop at 6 PM UTC from Monday to Friday.
- Remain stopped outside of these hours.
(you can edit the time range)
Step 2: Prepare the Script
We’ve developed a Bash script named schedule_rds_start_stop.sh
that automates the entire process.
Script Features
- Creates an IAM Policy: Grants permissions to manage the RDS instance.
- Creates an IAM Role: Allows AWS Systems Manager to assume the role and execute actions.
- Schedules Start and Stop Associations: Sets up individual tasks for each weekday.
Step 3: Download the Script
You can download the script from this GitHub repository:
Step 4: Run the Script
Script Parameters
The script accepts the following parameters:
-p
or--profile
: (Required) AWS CLI profile to use.-r
or--region
: (Required) AWS region where your RDS instance is located.-i
or--instance-id
: (Required) RDS instance identifier to manage.-D
or--delete
: (Optional) Deletes the scheduled tasks and IAM resources.-h
or--help
: Displays the help message.
Scheduling the RDS Instance
Run the script with the necessary parameters:
./schedule_rds_start_stop.sh -p your-aws-profile -r your-aws-region -i your-rds-instance-id
Example:
./schedule_rds_start_stop.sh -p default -r us-east-1 -i my-dev-database
Deleting the Schedule and Resources
If you need to remove the schedules and delete the IAM resources created by the script, use the -D
or --delete
flag:
./schedule_rds_start_stop.sh -D -p your-aws-profile -r your-aws-region -i your-rds-instance-id
Example:
./schedule_rds_start_stop.sh -D -p default -r us-east-1 -i my-dev-database
Step 5: Verify the Setup
After running the script:
- Check AWS Systems Manager:
- Navigate to State Manager in the AWS console.
- Verify that start and stop associations are created for each weekday. - Check IAM Roles and Policies:
- Ensure that the IAM role (RDSStartStopRole
) and policy (RDSStartStopPolicy
) are created.
- Confirm that the role has the correct trust relationship and policy attachment.
💵 Calculating Your Savings: Money and Carbon Footprint
Let’s break down how much you can save by automating your RDS instance’s operating hours.
Scenario Details
- Instance Type:
db.m5.large
(a commonly used instance for testing workloads) - Cost per Hour: Approximately $0.203 per hour in the eu-central-1 region. (Note: Pricing may vary; check AWS RDS pricing for your region and instance type)
- Operating Hours Without Automation: 24 hours/day × 7 days/week = 168 hours/week
- Operating Hours With Automation: 10 active hours x 5 days = 50 hours/week
Calculating Cost
- Cost Without Automation: 168 hours/week × $0.203/hour = $34.10/week
- Cost With Automation: 50 hours/week × $0.203/hour = $10.15/week
- Weekly Savings: $34.10 — $10.15 = $23.95/week
- Annual Savings: $23.95/week × 52 weeks/year = $1,245.40/year
That’s a savings of over $1,200 per year for just one RDS instance!
If you have multiple instances or use even larger instance types, the savings multiply accordingly.
🏭 Reducing Your Carbon Footprint
While precise calculations are challenging without exact power consumption data, it’s clear that reducing the operating hours of your RDS instances lowers energy usage. This not only cuts costs but also contributes to a smaller carbon footprint. By automating your RDS start and stop times, you’re taking a meaningful step toward more sustainable cloud computing practices.
Deep Dive: How the Script Works
Editing the script you can set the defaults you like and you can change the “working hours” to the range that more fit your company. Then, you just need to run the script once for every db instance you desire.
You can even run the script itself programmatically because it’s non-interactive once it has all the needed parameters.
IAM Policy and Role Creation
The script first creates an IAM policy with permissions to start, stop, and describe the RDS instance.
It then creates an IAM role that AWS Systems Manager can assume to perform these actions.
Scheduling with AWS Systems Manager
AWS Systems Manager doesn’t support cron expressions with day ranges like MON-FRI
. To work around this, the script creates individual start and stop associations for each weekday (Monday through Friday), resulting in a total of 10 associations.
- Start Associations: Trigger at 8 AM UTC on each weekday.
- Stop Associations: Trigger at 6 PM UTC on each weekday.
Idempotency and Cleanup
The script is idempotent — it can be run multiple times without creating duplicate resources: if a resource with the same name is found, then it will be updated.
When run with the -D
flag, it cleanly deletes all created associations, IAM roles, and policies restoring the account resources to the previous state.
Benefits: Why You Should Implement This
Maximize efficiency and minimize costs.
- Cost Savings: Only pay for the RDS instance when it’s in use.
- Environmental Impact: Reduce energy consumption and lower your carbon footprint.
- Automation: Reduce manual intervention and potential for human error.
- Simplicity: No need for complex AWS Lambda functions or additional services.
🌈 Conclusion
Automating the start and stop times of your AWS RDS instances can lead to significant cost savings and operational efficiency. With this simple Bash script, you can ensure your databases are running only when needed, freeing up your time to focus on more critical tasks. Plus, you’re contributing to a greener planet by reducing unnecessary energy consumption.
Spread the Word!
If you found this guide helpful, please share it on social media platforms like X (formerly Twitter), Reddit, and LinkedIn. Your support helps others discover ways to optimize their AWS environments and promotes sustainable practices!
Additional Resources
- AWS Systems Manager Documentation: AWS Systems Manager
- AWS RDS Pricing: Amazon RDS Pricing
- AWS Sustainability Initiatives: Sustainability in the Cloud
- AWS Article (manual): Schedule Amazon RDS stop and start using AWS Systems Manager