Last active
July 20, 2017 09:23
-
-
Save kenzo0107/b841bfad242e87da3625e5d980845529 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Action | |
| # stop and start instance. | |
| # | |
| # Precondition | |
| # - Install aws cli | |
| # - aws configured and create ~/.aws/config. | |
| # - Install jq | |
| # | |
| # Ex | |
| # sh stop_and_start_ec2_instance.sh "<profile>" "<instance id>" | |
| # | |
| # Authored by @kenzo0107 | |
| set -eu | |
| function stop_and_start_a_instance_by_instance_id() { | |
| local _account=$1 | |
| local _instance_id=$2 | |
| local _ret | |
| local _code | |
| # check status. | |
| _ret=$(aws --profile $_account ec2 describe-instance-status --instance-ids $_instance_id --filter "Name=instance-state-name,Values=running" | jq '.InstanceStatuses[] | {InstanceId, InstanceState: .InstanceState.Name, SystemStatus: .SystemStatus.Status, InstanceStatus: .InstanceStatus.Status}') | |
| echo $_ret | |
| if [ -z "${_ret}" ]; then | |
| echo "Instance: $_instance_id is not running. No action." | |
| return | |
| fi | |
| echo ">>> Instance: ${_instance_id} is running. Next, stop and start the instance." | |
| # stop. | |
| _ret=$(aws --profile $_account ec2 stop-instances --instance-ids $_instance_id) | |
| _code=$? | |
| if [ $_code -ne 0 ]; then | |
| echo "code: $code, msg: 'failed stopping instance.'" | |
| exit 1 | |
| fi | |
| echo $_ret | |
| # wait stopping. | |
| _ret=$(aws --profile $_account ec2 wait instance-stopped --instance-ids $_instance_id) | |
| _code=$? | |
| if [ $_code -ne 0 ]; then | |
| echo "code: $code, msg: 'failed waiting for stopping instance.'" | |
| exit 1 | |
| fi | |
| echo $_ret | |
| # start. | |
| _ret=$(aws --profile $_account ec2 start-instances --instance-ids $_instance_id) | |
| _code=$? | |
| if [ $_code -ne 0 ]; then | |
| echo "code: $code, msg: 'failed starting instance.'" | |
| exit 1 | |
| fi | |
| echo $_ret | |
| # wait starting. | |
| _ret=$(aws --profile $_account ec2 wait instance-status-ok --instance-ids $_instance_id) | |
| _code=$? | |
| if [ $_code -ne 0 ]; then | |
| echo "code: $code, msg: 'failed waiting for starting instance.'" | |
| exit 1 | |
| fi | |
| echo $_ret | |
| # check status. | |
| r=$(aws --profile $_account ec2 describe-instance-status --instance-ids $_instance_id --filter "Name=instance-state-name,Values=running" | jq '.InstanceStatuses[] | {InstanceId, InstanceState: .InstanceState.Name, SystemStatus: .SystemStatus.Status, InstanceStatus: .InstanceStatus.Status}') | |
| echo $r | |
| } | |
| if [ -z $2 ]; then | |
| echo 'Not enough args. Please set args - ex) sh stop_and_start_ec2_instance.sh <profile> <instance>' | |
| exit 1 | |
| fi | |
| profile="$1" | |
| instance_id="$2" | |
| stop_and_start_a_instance_by_instance_id "${profile}" "${instance_id}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment