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?

More than 3 years have passed since last update.

AWS CLI で EC2 の情報取得、開始、停止

Posted at

目的

インスタンスを起動しっぱなしにしていたのでコマンドから停止したい。

参考ページ

やってみた

停止中のインスタンスに対して実行する。
実行結果は stopped から pending になるよ書いてあるのね、なるほど

~ $ aws ec2 start-instances --instance-ids i-xxxxxxx
{
    "StartingInstances": [
        {
            "CurrentState": {
                "Code": 0,
                "Name": "pending"
            },
            "InstanceId": "i-xxxxxxx",
            "PreviousState": {
                "Code": 80,
                "Name": "stopped"
            }
        }
    ]
}

もう一回実行するとどうなる?
running から running の状態なのでこれ以上は変わらないらしい。

~ $ aws ec2 start-instances --instance-ids i-xxxxxxx
{
    "StartingInstances": [
        {
            "CurrentState": {
                "Code": 16,
                "Name": "running"
            },
            "InstanceId": "i-xxxxxxx",
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}

停止

running から stopping になると、なるほど

~ $ aws ec2 stop-instances --instance-ids i-xxxxxx
{
    "StoppingInstances": [
        {
            "CurrentState": {
                "Code": 64,
                "Name": "stopping"
            },
            "InstanceId": "i-xxxxxx",
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}

ステータス確認

あれ?ステータスがうまく出ない。
--include-all-instance オプションで全てのインスタンスを表示してやると出る。

なぜ?

~ $ aws ec2 describe-instance-status --instance-ids i-xxxxxx
{
    "InstanceStatuses": []
}

~ $ aws ec2 describe-instance-status --include-all-instance
{
    "InstanceStatuses": [
        {
            "AvailabilityZone": "ap-northeast-1a",
            "InstanceId": "i-xxxxxx",
            "InstanceState": {
                "Code": 80,
                "Name": "stopped"
            },
            "InstanceStatus": {
                "Status": "not-applicable"
            },
            "SystemStatus": {
                "Status": "not-applicable"
            }
        }
    ]
}

って describe-instance-status はEC2インスタンスを起動していないと表示されないらしい。
起動したら結果が表示されたい。

~ $ aws ec2 start-instances --instance-ids i-xxxxxxx

略

~ $ aws ec2 describe-instance-status --instance-id i-xxxxxx
{
    "InstanceStatuses": [
        {
            "AvailabilityZone": "ap-northeast-1a",
            "InstanceId": "i-xxxxxx",
            "InstanceState": {
                "Code": 16,
                "Name": "running"
            },
            "InstanceStatus": {
                "Details": [
                    {
                        "Name": "reachability",
                        "Status": "initializing"
                    }
                ],
                "Status": "initializing"
            },
            "SystemStatus": {
                "Details": [
                    {
                        "Name": "reachability",
                        "Status": "initializing"
                    }
                ],
                "Status": "initializing"
            }
        }
    ]
}
~ $ aws ec2 stop-instances --instance-ids i-xxxxxx
{
    "StoppingInstances": [
        {
            "CurrentState": {
                "Code": 64,
                "Name": "stopping"
            },
            "InstanceId": "i-xxxxxx",
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}
~ $ aws ec2 describe-instance-status --instance-id ${ec2_id}
{
    "InstanceStatuses": []
}

へー

今度全てのEC2を停止するコマンドをエイリアスに登録しよう。(破壊的

#おまけ

AWS LII ec2 に関するコマンドドキュメント

AWS CLI のコマンド補完はめっちゃ便利

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?