DevOps / Scripts bash Intermediate New

AWS EC2 Instance Scheduler

Stop and start EC2 instances by tag to save costs outside business hours.

bash
#!/bin/bash
ACTION=${1:-stop}
TAG_KEY=AutoSchedule
TAG_VALUE=true

IDS=$(aws ec2 describe-instances \
  --filters "Name=tag:$TAG_KEY,Values=$TAG_VALUE" "Name=instance-state-name,Values=running,stopped" \
  --query "Reservations[].Instances[].InstanceId" --output text)

if [ -z "$IDS" ]; then echo "No instances found."; exit 0; fi

aws ec2 ${ACTION}-instances --instance-ids $IDS
echo "$ACTION: $IDS"

Prerequisites

  • aws-cli v2
  • EC2 permissions

Pro Tips

  • → Schedule with EventBridge Rules: stop at 20:00, start at 08:00 weekdays.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
bash
Level
Intermediate
Read time
3 min
Published
Sep 2026
License
Commercial License

Newsletter

New modules every week, directly to your inbox.