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

#!/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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The One-a-Day Agile Team

S3 Antivirus Scanning with Lambda and ClamAV by Dennis Webb — AWS Cloud Expert and Slack Comedian

Docerina - Ballerina API Documentation

MapReduce in C from Scratch Using Threads: Map

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

SageMaker : Bring your own Algorithms

How to Integrate Your CRM & GL Systems

CS 373 Spring 2021: Kent Hansen

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SaurabhG

SaurabhG

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

More from Medium

Packer Building AWS AMI Images

How to use AWS Application Migration Service ( Onpremise to AWS Cloud Mumbai)

Backup & Archive with AWS

Mount S3 on EC2 Instance