Skip to main content

Command Palette

Search for a command to run...

Started my Project on Energy-Efficient DevOps: Auto-Suspending Idle AWS Resources using ML

Published
3 min read

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.pkl to Node-1 using scp.

  • Ensured it’s readable by the predict_and_suspend.py script.

Wrote & Deployed Prediction Script

A Python script (predict_and_suspend.py) that:

  • Reads the latest CPU usage from cpu_log.csv

  • Loads the trained ML model (model.pkl)

  • Predicts if the system is idle (CPU < threshold)

  • If idle, stops the instance using boto3 and 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/credentials manually.

Scheduled the Script Using Cron

  • Used crontab -e to schedule predict_and_suspend.py every 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

CommandPurpose
scp -i key.pem model.pkl ubuntu@<IP>Upload model to EC2
nano predict_and_suspend.pyCreate automation script
sudo apt install awscliEnsure boto3 dependencies
crontab -eAdd script to cron scheduler
requests.get("....")Fetch instance ID from metadata
ec2.stop_instances(...)Stop EC2 from within itself

Problems Faced + Solutions

  1. Instance Metadata Access Failed

Cause: Metadata service was blocked or IMDSv2 required
Solution: Enabled IMDSv2 via EC2 settings → Instance Settings → Modify metadata options

  1. boto3: NoCredentialsError

Cause: No IAM Role attached to instance
Solution: Created IAM Role with EC2 permissions and attached to instance

  1. Instance ID '' is malformed

Cause: Couldn't fetch instance ID from metadata
Solution: Enabled metadata access + confirmed that request returns correct value

  1. 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.py working

  • Cron 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.

More from this blog

Sahil

12 posts