Started my Project on Energy-Efficient DevOps: Auto-Suspending Idle AWS Resources using ML
Week 2– Automation Phase (Auto-Suspend Script Deployment)
Objective:
To automate EC2 instance suspension when the machine learning model predicts an idle state, using boto3, instance metadata, and cron jobs.
What I Did Today:
Transferred Trained Model to EC2
Uploaded
model.pklto Node-1 usingscp.Ensured it’s readable by the
predict_and_suspend.pyscript.
Wrote & Deployed Prediction Script
A Python script (predict_and_suspend.py) that:
Reads the latest CPU usage from
cpu_log.csvLoads the trained ML model (
model.pkl)Predicts if the system is idle (CPU < threshold)
If idle, stops the instance using
boto3and the EC2 metadata endpoint to fetch the instance ID
Solved IAM Permissions
Attached an IAM Role with AmazonEC2FullAccess to allow the instance to stop itself using
boto3.Verified no need to store or configure
~/.aws/credentialsmanually.
Scheduled the Script Using Cron
Used
crontab -eto schedulepredict_and_suspend.pyevery 15 minutes.Verified that the script shuts down the instance when idle is predicted.
Fixed Metadata Access Error
Instance metadata was initially blocked.
Enabled it via EC2 > Actions > Modify metadata options
Set IMDSv2 to “Required” and allowed ip access.
All Commands Used Today and Their Purpose
| Command | Purpose |
scp -i key.pem model.pkl ubuntu@<IP> | Upload model to EC2 |
nano predict_and_suspend.py | Create automation script |
sudo apt install awscli | Ensure boto3 dependencies |
crontab -e | Add script to cron scheduler |
requests.get("....") | Fetch instance ID from metadata |
ec2.stop_instances(...) | Stop EC2 from within itself |
Problems Faced + Solutions
Instance Metadata Access Failed
Cause: Metadata service was blocked or IMDSv2 required
Solution: Enabled IMDSv2 via EC2 settings → Instance Settings → Modify metadata options
boto3: NoCredentialsError
Cause: No IAM Role attached to instance
Solution: Created IAM Role with EC2 permissions and attached to instance
Instance ID '' is malformed
Cause: Couldn't fetch instance ID from metadata
Solution: Enabled metadata access + confirmed that request returns correct value
DeprecationWarning: datetime.utcnow()
Cause: Python is moving to timezone-aware datetime
Solution: Replaced with datetime.datetime.now(datetime.UTC) (resolved warning)
Setup Status
predict_and_suspend.pyworkingCron job running every 15 mins
Instances now self-terminate if idle
IAM Role + metadata access properly configured
Screenshots:

Tags
#AWS #DevOps #MachineLearning #CloudComputing #Python #EnergyEfficiency #Boto3 #CronJobs #Automation #BuildInPublic
Want to Follow Along?
I’ll be sharing weekly progress — issues, logs, architecture, and ML models.
If you've solved similar problems (like automated cloud optimization), I’d love to hear your insight.