Here's a simple shell script that you can use to start and stop an EC2 instance using the AWS Command Line Interface (CLI):
#!/bin/bash
# Replace with your AWS region and EC2 instance ID
REGION="us-east-1"
INSTANCE_ID="your-instance-id"
# Function to start the EC2 instance
start_instance() {
aws ec2 start-instances --region $REGION --instance-ids $INSTANCE_ID
echo "Starting EC2 instance..."
}
# Function to stop the EC2 instance
stop_instance() {
aws ec2 stop-instances --region $REGION --instance-ids $INSTANCE_ID
echo "Stopping EC2 instance..."
}
# Check the command line arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 [start|stop]"
exit 1
fi
# Determine the action based on the argument
case "$1" in
start)
start_instance
;;
stop)
stop_instance
;;
*)
echo "Usage: $0 [start|stop]"
exit 1
;;
esac
exit 0
Replace "your-region"
with your desired AWS region (e.g., us-east-1
) and "your-instance-id"
with the actual ID of the EC2 instance you want to start and stop.
Save this script to a file (e.g., manage_ec2.sh
), make it executable (chmod +x manage_ec2.sh
), and then you can use it as follows:
# To start the EC2 instance
sh ec2_start_stop.sh start
# To stop the EC2 instance
sh ec2_start_stop.sh stop
Let's break down how command line arguments are used in the script:
The script expects exactly one command line argument: either "start" or "stop".
if [ "$#" -ne 1 ]; then
echo "Usage: $0 [start|stop]"
exit 1
fi
$#
represents the number of command line arguments passed to the script.The script checks if the number of arguments is not equal to 1. If this condition is true, it means the user didn't provide the correct number of arguments.
In that case, an error message is printed along with the correct usage of the script (i.e., specifying either "start" or "stop" as an argument), and the script exits with an error code.
Based on the provided argument, the script determines whether to start or stop the EC2 instance:
case "$1" in
start)
start_instance
;;
stop)
stop_instance
;;
*)
echo "Usage: $0 [start|stop]"
exit 1
;;
esac
The
case
statement checks the value of the first command line argument ($1
) against different cases: "start", "stop", and a wildcard (*
), which matches any other value.If the argument is "start", the
start_instance
function is called.If the argument is "stop", the
stop_instance
function is called.If the argument is neither "start" nor "stop", an error message is printed along with the correct usage of the script, and the script exits with an error code.
Make the script executable:
chmod +x ec2_start_stop.sh
Set Up a Cron Job:
Open the cron configuration for editing:
crontab -e
Add the following lines to the cron configuration to schedule the start and stop actions. Modify the timing as needed.
# Schedule to start the EC2 instance at 3 PM daily
00 15 * * * /home/ubuntu/ec2_start_stop.sh start
# Schedule to stop the EC2 instance at 2 AM daily
00 02 * * * /home/ubuntu/ec2_start_stop.sh stop
Replace /path/to/ec2_start_stop.sh
with the actual path to your bash script.
Save and exit the editor. The cron job will now automatically start and stop your EC2 instance at the specified times.
Refer here for Crobtab