9
13

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 5 years have passed since last update.

EC2インスタンスをAWS CLIで起動する

Posted at

AWSCLIの初期設定

実行コマンド
aws configure

IAMであらかじめユーザーを作成しておく必要があります。
アクセスキーやシークレットアクセスキーは作成したユーザーのものを利用します。

起動

実行コマンド
aws ec2 start-instances --instance-ids "i-xxxxxxxxxxxxxxxxx"
処理結果
{
    "StartingInstances": [
        {
            "InstanceId": "i-xxxxxxxxxxxxxxxxx", 
            "CurrentState": {
                "Code": 0, 
                "Name": "pending"
            }, 
            "PreviousState": {
                "Code": 80, 
                "Name": "stopped"
            }
        }
    ]
}

状態表示(起動後)

実行コマンド
aws ec2 describe-instance-status --instance-ids "i-xxxxxxxxxxxxxxxxx"
処理結果
{
    "InstanceStatuses": [
        {
            "InstanceId": "i-xxxxxxxxxxxxxxxxx", 
            "InstanceState": {
                "Code": 16, 
                "Name": "running"
            }, 
            "AvailabilityZone": "ap-northeast-1a", 
            "SystemStatus": {
                "Status": "initializing", 
                "Details": [
                    {
                        "Status": "initializing", 
                        "Name": "reachability"
                    }
                ]
            }, 
            "InstanceStatus": {
                "Status": "initializing", 
                "Details": [
                    {
                        "Status": "initializing", 
                        "Name": "reachability"
                    }
                ]
            }
        }
    ]
}

起動完了待ち

; で区切ってコマンド指定することで、起動完了後に処理することができます。

実行コマンド
aws ec2 wait instance-running --instance-ids "i-xxxxxxxxxxxxxxxxx"; echo "next command"

停止

実行コマンド
aws ec2 stop-instances --instance-ids "i-xxxxxxxxxxxxxxxxx"
処理結果
{
    "StoppingInstances": [
        {
            "InstanceId": "i-xxxxxxxxxxxxxxxxx", 
            "CurrentState": {
                "Code": 64, 
                "Name": "stopping"
            }, 
            "PreviousState": {
                "Code": 16, 
                "Name": "running"
            }
        }
    ]
}

状態表示(停止中)

停止状態でdescribe-instance-statusすると、結果のjsonが空値で返ってきます。

実行コマンド
aws ec2 describe-instance-status --instance-ids "i-xxxxxxxxxxxxxxxxx"
処理結果
{
    "InstanceStatuses": []
}
9
13
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
9
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?