How to Automatically Start and Stop AWS EC2 Instances Using Cron (Ubuntu 20.04)

If you’re running workloads that only need to be online at specific times—like a live stream, dev environment, or scheduled job—you’re probably wasting money keeping EC2 instances running 24/7.

In this guide, you’ll learn how to automatically start and stop AWS EC2 instances using cron and the AWS CLI on Ubuntu 20.04—a simple, reliable, and cost-effective solution.

🚀 Why Schedule EC2 Start/Stop?

Automating EC2 uptime is a great way to:

  • 💰 Reduce AWS costs
  • ⚙️ Automate recurring workloads
  • 🧠 Eliminate manual start/stop tasks
  • 🔁 Improve operational consistency

Common use cases:

  • Live streaming servers (e.g., weekly broadcasts)
  • Dev/test environments
  • Batch processing jobs
  • Training labs or demos

🧠 Example Use Case

In this setup:

  • EC2 instances start every Sunday at 6:30 PM
  • EC2 instances stop every Sunday at 9:30 PM

This ensures the infrastructure is available only when needed.

⚙️ Prerequisites

Before you begin, make sure you have:

  • Ubuntu 20.04 (or similar Linux server)
  • AWS account
  • IAM user with EC2 permissions
  • AWS CLI v2 installed

🔐 Step 1: Create an IAM User for Automation

Create a dedicated IAM user with programmatic access.

Attach a policy like:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:StartInstances",
        "ec2:StopInstances",
        "ec2:DescribeInstances"
      ],
      "Resource": "*"
    }
  ]
}

🔒 Tip: For production, restrict access to specific instance ARNs.

💻 Step 2: Install AWS CLI v2 on Ubuntu

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Verify installation:

aws --version

🔧 Step 3: Configure AWS CLI Credentials

aws configure

Example:

AWS Access Key ID:     AKIA************
AWS Secret Access Key: ****************************
Default region name:   us-east-1
Default output format: json

Test Access:

aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxxxxxx

📜 Step 4: Create EC2 Start/Stop Scripts

Create a scripts directory:

sudo mkdir -p /scripts

▶️ Start EC2 Instances Script

/scripts/start-ec2.sh

#!/bin/bash

export PATH=/usr/local/bin:/usr/bin:/bin
export AWS_DEFAULT_REGION=us-east-1

aws ec2 start-instances \
  --instance-ids i-xxxxxxxxxxxxxxxxx i-yyyyyyyyyyyyyyyyy

⏹ Stop EC2 Instances Script

/scripts/stop-ec2.sh

#!/bin/bash

export PATH=/usr/local/bin:/usr/bin:/bin
export AWS_DEFAULT_REGION=us-east-1

aws ec2 stop-instances \
  --instance-ids i-xxxxxxxxxxxxxxxxx i-yyyyyyyyyyyyyyyyy

Make Scripts Executable

chmod +x /scripts/start-ec2.sh
chmod +x /scripts/stop-ec2.sh

⏰ Step 5: Schedule EC2 Automation with Cron

Create a cron file:

sudo nano /etc/cron.d/ec2-schedule

Add:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# Start EC2 instances Sunday at 6:30 PM
30 18 * * 0 root /scripts/start-ec2.sh >> /var/log/ec2-start.log 2>&1

# Stop EC2 instances Sunday at 9:30 PM
30 21 * * 0 root /scripts/stop-ec2.sh >> /var/log/ec2-stop.log 2>&1

📌 Important Configuration Notes

✅ Cron File Requirements

  • Owned by root: chown root:root /etc/cron.d/ec2-schedule
  • Permissions: chmod 644 /etc/cron.d/ec2-schedule
  • Must end with a newline

🕒 Set Correct Timezone

Cron uses system time:

timedatectl

Set to Eastern Time (if needed):

sudo timedatectl set-timezone America/New_York

🧪 Testing Your EC2 Cron Automation

Run scripts manually:

/scripts/start-ec2.sh
/scripts/stop-ec2.sh

For cron testing, temporarily run every minute:

* * * * * root /scripts/start-ec2.sh >> /var/log/ec2-start.log 2>&1

Monitor logs:

tail -f /var/log/ec2-start.log
tail -f /var/log/syslog

💡 Cron vs AWS EventBridge Scheduler

You could use AWS-native scheduling (EventBridge + Lambda), but cron has advantages:

  • Simple and transparent
  • No extra AWS services
  • Easy to debug
  • Works in hybrid environments

🎯 Final Thoughts

Using cron with AWS CLI is one of the simplest ways to automate EC2 instance lifecycle management.

It’s ideal for:

  • Cost optimization
  • Scheduled infrastructure
  • Lightweight automation

If you’re running anything on a schedule, this approach can save both time and money with minimal complexity.

Leave a Reply

Your email address will not be published. Required fields are marked *