
Automating Cloud Cost Optimization: Save Cost on RDS/EC2 on Night/Weekends
Managing cloud servers manually is slow, causes human mistakes, and wastes money when servers are left running empty overnight. To fix this, I built an automatic schedule using Amazon EventBridge and AWS Step Functions to turn our database (Amazon RDS) and application server (Amazon EC2) on and off only when needed.
Executive Summary
Managing cloud servers manually is slow, causes human mistakes, and wastes money when servers are left running empty overnight. To fix this, I built an automatic schedule using Amazon EventBridge and AWS Step Functions to turn our database (Amazon RDS) and application server (Amazon EC2) on and off only when needed.
Cloud databases take time to start up and often shift through hidden background setups like modifying or configuring. To handle this, I created a smart State Machine that doesn't panic or crash if the database isn't ready instantly. Instead, it uses an automated waiting loop to double-check the server's status safely. By slowing down how often it checks, the system stays within AWS's safety limits, stops extra API costs, and guarantees that our application server fires up only after the database is completely online and ready to accept connections. This provides a reliable, money-saving cloud environment that runs entirely on autopilot.
🎯 Project Objectives
- Turn cloud resources on and off automatically using a schedule without any manual clicking.
- Ensure the database (RDS) turns completely online before the application server (EC2) attempts to start.
- Build smart waiting loops to handle background database states like modifying or configuring without crashing.
- Optimize check intervals to avoid hitting AWS step log limits or incurring extra API costs.
- Stop paying for expensive database and compute resources when they are idle during off-peak hours.
Architecture Overview
🌐 High-Level Flow
⏰ EventBridge (Timer) → ⚙️ Step Functions (Orchestrator) → 🗄️ Amazon RDS (Database Starts First) → 🖥️ Amazon EC2 (App Server Starts Second)
🧩 Components Used
- Amazon EventBridge: The automated alarm clock that triggers our workflow based on a set time or cron schedule.
- AWS Step Functions: The smart coordinator (State Machine) that controls the exact step-by-step order of operations and handles errors.
- Amazon RDS (MySQL): The cloud database instance that contains our application's data layers.
- Amazon EC2: The virtual computer server that hosts and runs our main application backend code.
⚙️ Step-by-Step Implementation
1️⃣ State Machine Design & Casing Optimization
- Wrote the workflow script in Amazon States Language (ASL) using JSON.
- Fixed strict case-sensitivity parameters (DbInstances and DbInstanceStatus) to match the AWS response structure.
- Added a custom Catch block to the first step to prevent transient server state errors from crashing the flow.
- Crucial for letting the automation skip past expected state warnings safely.
2️⃣ Dynamic Waiting Loops & Timing Setup
- Configured an initial extended 120-second wait window to let the database handle its heavy boot tasks.
- Created a fast 20-second loop block to check the status frequently after the initial wait.
- Avoids hitting AWS execution history log limits (25,000 events) and saves API costs by stopping endless loops.
3️⃣ Target Resource Linkage
- Bound the exact database identifier (task4-db) to the RDS task blocks.
- Embedded the target application server instance ID into the final EC2 task block.
- Ensures the script commands point to the correct live server hardware.
4️⃣ EventBridge Scheduler Automation
- Created an automated time trigger in Amazon EventBridge matching local time parameters (Asia/Kolkata).
- Turned Off the "Flexible time window" setting to ensure the trigger fires exactly on time.
- Passed an empty JSON payload {} to ensure Step Functions accepts the automated launch command.
🔄 Automation Workflow
- Timer Fires: EventBridge hits the scheduled time and automatically sends a wake-up signal to AWS Step Functions.
- DB Boot Triggered: Step Functions triggers the RDS database to start up. If it is already online, the Catch block safely bypasses the warning.
- Resilient Status Check: The machine waits for 2 minutes and polls the database. If it reads a transition state like configuring or modifying, it sits in a short loop.
- App Server Starts: The moment the database status shifts to exactly "available", the choice state unlocks and sends a command to turn on the EC2 application server.
- Secure Safe Launch: The entire web environment wakes up completely on autopilot without any manual logins or human intervention.
✅ Key Benefits
- Zero Manual Effort: Automatically starts and stops environments without human intervention.
- Significant Cost Optimization: Saves money by shutting down expensive cloud infrastructure outside of work hours.
- Dependent Boot Sequencing: Guarantees data integrity by forcing the database to be fully ready before the app server boots.
- Resilient Infrastructure: Gracefully handles transient AWS states (modifying, configuring) without breaking.
- Reduced API Overhead: Long-wait intervals save execution logs and prevent extra AWS transaction costs.
🔐 Security & Best Practices
- IAM Role Isolation: Used a specific IAM role (task4_role) with minimum required permissions to trigger only the targeted RDS and EC2 instances.
- Exception Safeguards: Implemented explicit Catch blocks to handle runtime warnings without crashing the execution pipeline.
- Precise Clock Synchronization: Aligned EventBridge triggers to the specific local time zone (Asia/Kolkata) to prevent delayed or early firing.
- Strict Parameter Scope: Bound the automation to explicit resource IDs, ensuring other production infrastructure is never accidentally targeted.
🚀 Conclusion
This project demonstrates how serverless orchestration tools and event-driven schedulers can be combined to build a highly automated, cost-efficient infrastructure management pipeline.
By leveraging:
- Amazon EventBridge (Automated Scheduling)
- AWS Step Functions (Resilient State Machine Orchestration)
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article