Introduction
If you’re running non-production environments such as staging, testing, or pre-production on AWS EC2, chances are your servers stay idle during off-hours.
Even when no one is using them, AWS continues charging for compute time — leading to unnecessary monthly costs.
A smart and simple way to reduce your AWS bill is to automatically stop your EC2 instances during off-hours (e.g., at night) and start them again in the morning.
In this guide, we’ll show how to do that using a cron job + AWS CLI, and we’ll estimate how much you can save.
Why Auto-Stop EC2 Makes Sense
Many teams run development or pre-prod environments 24/7 — even though they’re used only 8–12 hours a day.
For example:
- QA servers used only in office hours
- Internal demo or staging environments
- Pre-prod servers used during testing cycles
These idle hours silently consume compute cost.
By automating start/stop, you can:
- Reduce monthly EC2 compute cost by 30–50%
- Keep environments ready for use every morning
- Maintain all EBS data and configuration (since stopping doesn’t delete volumes)
- Apply the method without any extra AWS service cost
Real Example: Cost Comparison
Let’s take an example of a t2.medium EC2 instance in Singapore (ap-southeast-1) region.
| Plan | Runtime per Day | Monthly Hours | Approx. Monthly Cost |
| Always On (24×7) | 24 hrs | 720 hrs | $33.41 |
| Scheduled (08:00–22:00) | 14 hrs | 420 hrs | $19.49 |
That’s a ~42% cost reduction just by stopping the instance overnight.
If your previous month’s bill was $51.81, you’d bring it down to around $37.89 — without changing instance type or performance.
Why Not Use Spot Instances Instead?
AWS Spot Instances are a great way to cut costs (up to 90%), but they come with limitations:
- Instances can be interrupted anytime when AWS needs capacity.
- Not ideal for long-running or stateful environments.
- You must design your system to handle instance termination gracefully.
For pre-prod or test servers that need consistent uptime during work hours, auto start/stop is a safer and more predictable alternative.
Implementation Using AWS CLI + Cron Job
You don’t need extra AWS services like Lambda or EventBridge (which add small costs).
You can run commands directly from your local server or management VM that already has AWS CLI configured.
Step 1: Configure AWS CLI
Make sure you’ve installed and configured AWS CLI with an IAM user that has permissions:
| aws configure |
IAM policy example (minimal):
| { “Version”: “2012-10-17”, “Statement”: [ { “Effect”: “Allow”, “Action”: [ “ec2:StartInstances”, “ec2:StopInstances” ], “Resource”: “EC2-ARN” } ] } |
Step 2: Create Start/Stop Scripts
start-ec2.sh
| #!/bin/bash aws ec2 start-instances –instance-ids i-0893e0c5ddd0ff2b5 –region ap-southeast-1 |
stop-ec2.sh
| #!/bin/bash aws ec2 stop-instances –instance-ids i-0893e0c5ddd0ff2b5 –region ap-southeast-1 |
Make them executable:
| chmod +x start-ec2.sh stop-ec2.sh |
Or
| #!/bin/bash # ===================================================== # Simple EC2 Start/Stop Script (for Preprod) # Instance: i-0893e0c5ddd0ff2b5 # Region: ap-southeast-1 # Author: Tom # ===================================================== INSTANCE_ID=”*****************” REGION=”***********” LOG_FILE=”/var/log/ec2-scheduler.log” ACTION=$1 # start or stop timestamp() { date ‘+%Y-%m-%d %H:%M:%S’ } case “$ACTION” in start) aws ec2 start-instances –instance-ids “$INSTANCE_ID” –region “$REGION” echo “$(timestamp) – Started EC2 $INSTANCE_ID” >> “$LOG_FILE” ;; stop) aws ec2 stop-instances –instance-ids “$INSTANCE_ID” –region “$REGION” echo “$(timestamp) – Stopped EC2 $INSTANCE_ID” >> “$LOG_FILE” ;; *) echo “Usage: $0 {start|stop}” ;; esac |
Step 3: Add Cron Jobs
Edit your crontab:
| crontab -e |
Add these lines:
| 0 8 * * * /path/to/start-ec2.sh >> /var/log/ec2_start.log 2>&1 0 22 * * * /path/to/stop-ec2.sh >> /var/log/ec2_stop.log 2>&1 |
This will start the EC2 instance at 8 AM and stop it at 10 PM daily also you can add weekend scheduled
Security & Best Practices
- Use a dedicated IAM user or role with minimal privileges.
- Rotate access keys regularly.
- Store AWS credentials securely in ~/.aws/credentials or via environment variables.
- Optionally add a Slack or email notification after each successful/failed job.
Summary
| Feature | Auto Stop/Start | Spot Instance |
| Cost Savings | 30–50% | Up to 90% |
| Reliability | High (Predictable uptime) | Low (Can be interrupted) |
| Best For | Pre-prod, staging, test | Batch jobs, CI/CD, temporary workloads |
Conclusion
Most companies keep their non-production servers running 24×7, even when unused at night.
With a simple cron job and AWS CLI, you can cut your EC2 cost by up to 40% — safely, automatically, and without losing any data.