AWS EC2 : Automation bash script to launch/start/stop Ec2 Instance(Ubuntu Instances)

SaurabhG
2 min readAug 27, 2021

--

Hi Folks…

Being part of DevOps / Development teams we often face requirement to launch VM/EC2 instance to execute some script/ Jenkins pipeline(if Jenkins hosted on that instance) and after if task has been accomplished we need to shut down the instance to prevent occurring cost.

Here I am sharing a bash script to start/stop AWS EC2 instance. User/ Develop need to pass Instance Id as parameter. Here I used some bash scripting and AWS CLI commands to perform automated launch/stop EC2 instance.

You can clone the same script form github URL.

https://github.com/SaurabhG-20/AWS-Instance-start-stop-atuoamtion

Feel free to share the article/ or copy the block of code. Happy learning.

Steps :

1. Create file with extension .sh in folder where your Instance key have been stored.
2. Replace the File path in script with path of yours instance key file.
3. Keep your instance Id ready for the input.

At the end of script execution user will get command for launching EC2 instance.
P.S. This script is intended to launch Ubuntu OS based instance.

Script

#!/bin/bashecho "   -------------------------------- "echo "   Automation Script for instance start/stop"echo "   -------------------------------- "read -p "   Enter Instance Id : " instance_idecho "   "if [ -z "$instance_id" ]thenecho "   Exiting from script. Please enter instance Id."exit -1elseecho "  --> Fetching Instance $instance_id status."fiinstance_state=$(aws ec2 describe-instance-status --instance-id $instance_id --query 'InstanceStatuses[*].InstanceState.Name' --output text)size=${#instance_state}if [ -z "$instance_state" ]thenecho "  --> Instance $instance_id is not in running state. Starting the instance"# on first execution start-instances command returns "pending" state.# we can run small while loop to check if instance started successfully or notinstance_start_invoke=$(aws ec2 start-instances --instance-ids $instance_id --query 'StartingInstances[*].CurrentState.Name' --output text)echo "  --> start instance command execution result : $instance_start_invoke"if [ "$instance_start_invoke" = "pending" ]thenfetch_instance_start=$instance_start_invokewhile [ "$fetch_instance_start" = "pending" ]dofetch_instance_start=$(aws ec2 start-instances --instance-ids $instance_id --query 'StartingInstances[*].CurrentState.Name' --output text)echo "  --> Instance state : $fetch_instance_start"sleep 5doneecho "  --> -------------------------------------------"echo "  --> Instance state : $fetch_instance_start"echo "  --> Checking Instance Health status"fetch_instance_health="initializing"while [ "$fetch_instance_health" = "initializing" ]dofetch_instance_health=$(aws ec2 describe-instance-status --instance-id $instance_id --query 'InstanceStatuses[*].InstanceStatus.Status' --output text)echo "  --> Instance health check : $fetch_instance_health"sleep 10doneecho "  --> -------------------------------------------"echo "  --> Instance health : $fetch_instance_health"echo "Fetching Instance Ip"instance_ip=$(aws ec2 describe-instances --instance-id $instance_id --query "Reservations[*].Instances[*].PublicIpAddress" --output=text)echo "  --> Launching Instance $instance_id with instance's public IP $instance_ip"file_path=<Paste file path of your instance key file>echo "Launch Instance using command : ssh -i $file_path ubuntu@$instance_ip"fielseif [ "$instance_state" = "running" ]theninstnace_stop_invoke=$(aws ec2 stop-instances --instance-ids $instance_id --query 'StoppingInstances[*].CurrentState.Name' --output text)echo "  --> Instance state : $instnace_stop_invoke"echo "  --> Instance will be stopped after sometime."fifi

#EC2 #Automation #AWS #Ubuntu

--

--

SaurabhG
SaurabhG

Written by SaurabhG

I am an enthusiastic learner. Always want to challenge my last learning & keep hunting for new learning. about.me/saurabh.gangrade