Starting and stopping ec2 instances

I’ve been using EC2 instances to train my models for a while now. Having to use AWS web console to start and stop instances has been a nuisance for me. And since I’m not using the elastic IP because I’m too poor to afford such luxuries, I decided to spend some time to write a bash script to start\stop my ec2 instances by using the instance name. In addition to that, I can also get the ipv4 public IP address for my instances, thus, allowing me to ssh into the machine!
Setting up aws cli
First thing you need to do is to setup aws-cli. Installation is quite easy, you can follow the steps in their official website. Once you install the aws-cli, configure your aws cli with the aws access token and secret key.
aws configure
You will be asked to enter the aws keys and secret keys.
Starting ec2
Let’s begin by writing a bash script to start an instance. Create a file called start_instance.sh
.
#start_instance.sh
#!/bin/bash
INSTANCE_NAME=$1
#if instance id is none exit
if [ -z "$INSTANCE_NAME" ]; then
echo "Instance name is required"
exit 1
fi
echo "Starting instance with name: $INSTANCE_NAME"
echo "Getting instance-id using instance name"
INSTANCE_ID=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$INSTANCE_NAME" --query "Reservations[*].Instances[*].InstanceId" --output text)
# get ec2 instance-id using instance name
echo "Instance ID: $INSTANCE_ID"
echo "Starting instance with instance-id: $INSTANCE_ID"
aws ec2 start-instances --instance-ids $INSTANCE_ID
aws ec2 wait instance-running --instance-ids $INSTANCE_ID
IP_ADDRESS=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[*].Instances[*].PublicIpAddress" --output text)
echo "Instance started with IP Address: $IP_ADDRESS"
This script takes the instance name as input, and starts the ec2 instance and returns the public ip address of the newly started ec2 isntance.
bash start_instance.sh instance_name
If you want to run this script from anywhere. Just move this script to /usr/local/bin
.
mv start_instance.sh /usr/local/bin/start_instance
sudo chmod +x /usr/local/bin/start_instance
start_instance instance_name
Make sure that the path /usr/local/bin/
is in the path. You can check it by:
echo $PATH
If it is not in the path, just add it:
export PATH=$PATH:/usr/local/bin
Bonus 1: Login script
The above script will only return the IP address of the instance that was started. We can use this script to create another script that will also automate login (by just using the instance name!). Create a new script called ec2login.sh
#!/bin/bash
# Define your default PEM file and user
PEM_FILE="~/Downloads/prod-i8ml-key.pem"
USER="ubuntu" # Change this if your instance uses a different user (like ubuntu)
if [ "$#" -ne 1 ]; then
echo "Usage: ec2login <public-ip-or-dns>"
exit 1
fi
echo "Starting instance $1..."
#PUBLIC_IP=$(start_instance $1)
PUBLIC_IP=$(start_instance $1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')
echo "Logging into $PUBLIC_IP"
# Modify SSH config to avoid passing PEM file every time
CONFIG_FILE="$HOME/.ssh/config"
if ! grep -q "$PUBLIC_IP" "$CONFIG_FILE"; then
echo "Adding $PUBLIC_IP to SSH config..."
echo -e "\nHost $PUBLIC_IP" >>"$CONFIG_FILE"
echo " User $USER" >>"$CONFIG_FILE"
echo " IdentityFile $PEM_FILE" >>"$CONFIG_FILE"
echo " StrictHostKeyChecking no" >>"$CONFIG_FILE"
echo " UserKnownHostsFile=/dev/null" >>"$CONFIG_FILE"
fi
# Login to EC2 instance
ssh $PUBLIC_IP
This script uses the start_instance
script to login to ec2 instance just by name. If the instance is not running, this script will start the instance. There’s two things that you should change here: the location of the pem file and the ec2 user. This script then updates the ssh config file so that the keys are automatically added and you can ssh into the ec2 instance.
Similarly with the start_instance.sh
script, move this to /usr/local/bin
. And now you will be able to login to any ec2 instance just by using the instance.
mv ec2login.sh /usr/local/bin/ec2login
sudo chmod +x /usr/local/bin/ec2login
ec2login instance_name
BONUS 2: Stopping the instance
You can also create a bash script to stop the instance:
#!/bin/bash
INSTANCE_NAME=$1
#if instance id is none exit
if [ -z "$INSTANCE_NAME" ]; then
echo "Instance name is required"
exit 1
fi
echo "Starting instance with name: $INSTANCE_NAME"
echo "Getting instance-id using instance name"
INSTANCE_ID=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$INSTANCE_NAME" --query "Reservations[*].Instances[*].InstanceId" --output text)
# get ec2 instance-id using instance name
echo "Instance ID: $INSTANCE_ID"
#stop instance
aws ec2 stop-instances --instance-ids $INSTANCE_ID
Subscribe to my newsletter
Read articles from Siddhi Kiran Bajracharya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Siddhi Kiran Bajracharya
Siddhi Kiran Bajracharya
Hi there! I'm a machine learning, python guy. Reach out to me for collaboration and stuff! :D