0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[ワンライナー]特定のタグが無いVPCのインスタンスを全て停止する

Posted at

はじめに

以前、下記の記事を公開したのですが、こちらは特定のVPCのインスタンスを定期的に停止、起動する方法でした。

要望「むしろ止めちゃいけないもの以外全部止めたいです」

なるほど、では止め方を考えてみましょう

前提条件

  • Shell Script実行環境
  • IBM CloudのCLIを利用
  • jqコマンドを利用
  • 全てのリージョン、全てのVPCに対するアクセス権限がある

権限があればIBM Cloud Shellで実行も可能ですね。

ワンライナー

IBM Cloudに既にCLIでログインした状態からになります。
停止状態ではないインスタンスに dont-stop-me-now タグが付いていない場合は、インスタンスを停止するように指示しています。実は怖くて実行はしていません。

for region in us-south us-east br-sao ca-tor eu-gb eu-de eu-es jp-tok jp-osa au-syd ; do ibmcloud target -r $region 1>/dev/null 2>&1 ; ibmcloud is ins --json | jq '.[] | select(.status != "stopped")' | jq -cr '.id' | while read line ; do if [ -z `ibmcloud resource search $line --json | jq -cr '.items[].tags[]' | grep -x "dont-stop-me-now"` ];then ibmcloud is in-stop -f $line ; fi ; done ; done

同じだけど、ちゃんと書くと次になります

for region in us-south us-east br-sao ca-tor eu-gb eu-de eu-es jp-tok jp-osa au-syd
do 
  ibmcloud target -r $region 1>/dev/null 2>&1
  ibmcloud is ins --json | 
  jq '.[] | select(.status != "stopped")' | 
  jq -cr '.id' | 
  while read line
  do 
    if [ -z `ibmcloud resource search $line --json | jq -cr '.items[].tags[]' | grep -x "dont-stop-me-now"` ];
    then 
      ibmcloud is in-stop -f $line
    fi
  done
done

汎用操作ツールとの連携

先に紹介した記事では、環境変数に INSTANCE_ID を指定して、COMMAND02 でそのインスタンスIDのインスタンスを停止するようにしていましたが、こちらの INSTNACE_IDは指定せず、COMMAND02に 先ほど紹介したワンライナーのコマンドを指定すれば動くと思います。

さいごに

あまり多くないと思いますが、複数の利用者がいて、検証用環境だけどサーバー立ち上げっぱなしになっていて、費用発生してるけど、止めて良いの?他の人からすると思うかもしれません。
止めちゃいけないもの以外は止めちゃう、という方法も1つのCloud節約手段なのかなと思い、こちらの記事を書かせて頂きました。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?