LoginSignup
2
0

More than 1 year has passed since last update.

バッチ処理で EBS ボリュームタイプを gp2 から gp3 に変更する

Last updated at Posted at 2021-07-07

はじめに(要件)

今回の実施内容は、バッチ処理で EBS ボリュームタイプを gp2 から gp3 に変更します。

Case1:
(変更前)gp2のIOPS < 3000の場合:
(変更後)gp3のIOPSを3000、Throughputを125に設定します。

Case2:
(変更前)gp2のIOPS > 3000の場合:
(変更後)gp3のIOPSは変更前のgp2のIOPSと同じで、Throughputは125に設定します。

事前準備

①input.txt(EBSボリュームIDを格納する)
②gp.sh(EBSボリュームタイプをgp2からgp3に変更の実行ファイル)
⇒バッチファイルの中身は以下の通りです。

#!/bin/bash

# To get the exit IOPS of gp2, need to create a json file at first.
echo '[' >> gp2.json
cat input.txt | while read VOLUME_ID || [ -n "${VOLUME_ID}" ]; do  
    aws ec2 describe-volumes --volume-ids $VOLUME_ID --output json >> gp2.json
    echo ',' >> gp2.json
done < input.txt
head -n -1 gp2.json > temp.json ; mv temp.json gp2.json
sed '$ s/.$//' gp2.json
echo ']' >> gp2.json

# Get Volume length
VOLUME_ID_LENGTH=$(jq length gp2.json)
echo $VOLUME_ID_LENGTH

for ((i=0; i < $VOLUME_ID_LENGTH; i++)); do
    IOPS=$(jq ".[$i].Volumes[0].Iops" gp2.json | tr -d '"')
    # echo $IOPS
    VOLUME_ID=$(jq ".[$i].Volumes[0].VolumeId" gp2.json | tr -d '"') 
    # echo $VOLUME_ID

# Change gp2 to gp3.
  if  [ $IOPS -lt 3000 ]  ; then
        echo "****************************************************************************"
        echo "IOPS is: $IOPS, VOLUMEID is: $VOLUME_ID. IOPS is less then 3000"
        echo "****************************************************************************"
        echo "Start to change gp2 to gp3(IPOS < 3000)"
        echo -n "問題がなければ、'Y'を入力してください: "
        read Key

        #----
        if [ $Key = "Yes" ] || [ $Key = "yes" ] || [ $Key = "Y" ] || [ $Key = "y" ] || [ $Key = "YES" ]
        then
        VOLUME_ID=$(jq ".[$i].Volumes[0].VolumeId" gp2.json | tr -d '"') 
        aws ec2 modify-volume --volume-id $VOLUME_ID --volume-type gp3
        fi
  else      
        echo "****************************************************************************"
        echo "IOPS is: $IOPS,VOLUME_ID is $VOLUME_ID. IOPS is over 3000"
        echo "****************************************************************************"
        echo "Start to change gp2 to gp3(IPOS > 3000)"
        echo -n "問題がなければ、'Y'を入力してください: "
        read Key

        if [ $Key = "Yes" ] || [ $Key = "yes" ] || [ $Key = "Y" ] || [ $Key = "y" ] || [ $Key = "YES" ]
        then
        VOLUME_ID=$(jq ".[$i].Volumes[0].VolumeId" gp2.json | tr -d '"') 
        aws ec2 modify-volume --volume-id $VOLUME_ID --volume-type gp3 --iops $IOPS 
        fi
  fi
done
rm gp2.json


*2つのファイルは同一ディレクトリに格納する必要があります。

EBS ボリュームタイプをgp2からgp3に変更

STEP1

EBSのボリュームIDを「input.txt」に記入します。

Inked68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3435373839312f37373535383732312d396139332d363566362d346164392d32303233306337643537_LI.jpg

STEP2

バッチ実行します。

Jenny:/Desktop/gp3$ ./gp.sh

STEP3

コマンドを実行します。
出力内容を確認する。
問題がなければ、'Y'を入力してください: Y

1.jpg
2.jpg
3.jpg
4.jpg

STEP4

結果を確認します。
無事に変更しましたね~

Inked68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3435373839312f35663465343161632d346366382d343331342d623635332d36376262333565313831_LI.jpg

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0