EC2インスタンスタイプのスケールアップ・ダウンをbashスクリプトで作る

# 目的
ec2インスタンスタイプのスケールアップ・ダウンをbashスクリプトで作る

#　概要
任意のEC2インスタンスを一時的にスケールアップ（ダウン）しなきゃいけない運用があったと仮定。
なお、いつまで続く運用か定かでないので即席で作成

# 方法
- Ver1
  - 無限ループ使ってる。その際、api叩きすぎないよう気をつける。

```
#!/bin/bash

#
#change the instance-type on xxxx-xxxxxxxxx
#
set -x

#time-stamp
date

instance_type=`aws ec2 describe-instance-attribute --instance-id i-xxxxxxxx --attribute instanceType --output json --profile xxxxx.xxxxx | jq -r '.InstanceType[]'`

#dettach ec2
aws elb deregister-instances-from-load-balancer --load-balancer-name xxxx-xxxxxxxxx --instances i-xxxxxxxx --profile xxxxx.xxxxx

#connection-draining & stop instance
sleep 300;aws ec2 stop-instances --instance-ids i-xxxxxxxx --profile xxxxx.xxxxx

#status check
while :
do
  instance_state=`aws ec2 describe-instances --instance-ids i-xxxxxxxx  --output json --profile xxxxx.xxxxx | jq -r '.Reservations[].Instances[] | .State.Name'`
  if [ "$instance_state" = "stopped" ]; then
    break
  fi
sleep 10
done

#change the instance-type
case $instance_type in
	"r4.xlarge" ) aws ec2 modify-instance-attribute --instance-id i-xxxxxxxx --attribute instanceType --value m4.2xlarge --profile xxxxx.xxxxx ;;
	"m4.2xlarge" ) aws ec2 modify-instance-attribute --instance-id i-xxxxxxxx --attribute instanceType --value r4.xlarge --profile xxxxx.xxxxx ;;
esac

#start instance
aws ec2 start-instances --instance-ids i-xxxxxxxx --profile xxxxx.xxxxx

#attach ec2
aws elb register-instances-with-load-balancer --load-balancer-name xxxx-xxxxxxxxx --instances i-xxxxxxxx --profile xxxxx.xxxxx

#time-stamp
date
```

- Ver2
  - 実は無限ループ使う必要なかった

```
EC2()                                                                    EC2()



NAME
       ec2 -

DESCRIPTION
       Amazon  Elastic Compute Cloud (Amazon EC2) provides resizable computing
       capacity in the Amazon Web Services (AWS) cloud. Using Amazon EC2 elim-
       inates your need to invest in hardware up front, so you can develop and
       deploy applications faster.

AVAILABLE COMMANDS
(snip)
       o wait

```

```
#!/bin/bash

#
#change the instance-type on xxxxxxxx
#
set -x

#time-stamp
date

instance_type=`aws ec2 describe-instance-attribute --instance-id i-xxxxxxxxxxxxxxxxx --attribute instanceType --output json --profile xxxxxxxx.xxxxxxx | jq -r '.InstanceType[]'`

#dettach ec2
aws elb deregister-instances-from-load-balancer --load-balancer-name xxxxxxxxxxxxx --instances i-xxxxxxxxxxxxxxxxx --profile xxxxxxxx.xxxxxxx

#connection-draining & stop instance
sleep 300;aws ec2 stop-instances --instance-ids i-xxxxxxxxxxxxxxxxx --profile xxxxxxxx.xxxxxxx

#status check
aws ec2 wait instance-stopped --instance-ids i-xxxxxxxxxxxxxxxxx --profile xxxxxxxx.xxxxxxx

#change the instance-type
case $instance_type in
	"r4.xlarge" ) aws ec2 modify-instance-attribute --instance-id i-xxxxxxxxxxxxxxxxx --attribute instanceType --value m4.2xlarge --profile xxxxxxxx.xxxxxxx ;;
	"m4.2xlarge" ) aws ec2 modify-instance-attribute --instance-id i-xxxxxxxxxxxxxxxxx --attribute instanceType --value r4.xlarge --profile xxxxxxxx.xxxxxxx ;;
esac

#start instance
aws ec2 start-instances --instance-ids i-xxxxxxxxxxxxxxxxx --profile xxxxxxxx.xxxxxxx

#attach ec2
aws elb register-instances-with-load-balancer --load-balancer-name xxxxxxxxxxxxx --instances i-xxxxxxxxxxxxxxxxx --profile xxxxxxxx.xxxxxxx

#status check
aws ec2 wait instance-status-ok --instance-ids i-xxxxxxxxxxxxxxxxx --profile xxxxxxxx.xxxxxxx

#time-stamp
date
```

# 今後
- DataPipeLineで定期実行させるか検討