LoginSignup
1
1

More than 5 years have passed since last update.

aws-cliでスポットインスタンスの情報を取得するワンライナー2つ

Posted at

スポットインスタンスであんな事やこんな事を試してみたいのに

「ポチポチしてたら、日が暮れる!!!」

のでAWSが公式に出しているaws-cliを利用して色々やってみる事にしてみました。
(調べだしてからこの投稿を書くのに既に日が暮れているなんて、誰にも言えない・・・・)

参考にしたサイト

環境

  • CentOS 6.7
    • aws-cli
      • aws-cli/1.10.1 Python/2.7.10 Linux/2.6.32-573.el6.x86_64 botocore/1.3.23

事前準備に必要なもの

  • jq
    • インストール方法は適当にぐぐってみてください

稼働しているスポットインスタンスのインスタンスIDをまとめて取得する

jqで select(.InstanceLifecycle == "spot") としているところがミソ

path/to/hoge
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" | jq '.Reservations[].Instances[] | { instanceId: .InstanceId , InstanceLifecycle: .InstanceLifecycle} | select(.InstanceLifecycle == "spot")'

jqを使わずに上記と同じ事をする方法

こっちのほうがシンプル

path/to/hoge
aws ec2 describe-instances --filter "Name=instance-type,Values=t1.micro" "Name=instance-state-name,Values=running" "Name=instance-lifecycle,Values=spot"

稼働しているスポットインスタンスの内インスタンスタイプがt1.microのインスタンスIDをまとめて取得する

aws-cliの --filter"Name=instance-type,Values=t1.micro" をしているのがミソ

path/to/hoge
aws ec2 describe-instances --filter "Name=instance-type,Values=t1.micro" "Name=instance-state-name,Values=running" | jq '.Reservations[].Instances[] | { instanceId: .InstanceId , InstanceLifecycle: .InstanceLifecycle} | select(.InstanceLifecycle == "spot")'

最後に

最初はjqで「アチャー、こりゃー」ってしてたけども最終的には--filter使うのが一番シンプルでした・・・・

以上

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