Tuesday, January 13, 2015

Using AWS CLI to stop/start instances from the command line

Step 1:- 

Get the instance id's of the running instances

$aws ec2 describe-instances --filters Name=tag-key,Values="Name" --profile apix --output table --query "Reservations[*].Instances[*].InstanceId"

-------------------
|DescribeInstances|
+-----------------+
|  i-6e1b573e     |
|  ....               |
|  i-06ad8956     |
+-----------------+

The above will return an instance id(s)

Step 2:-

With the instance id's we can issue stop/start commands

$aws ec2 stop-instances --instance-ids=<your instance id(s)> --profile <your profile>


{
    "StoppingInstances": [
        {
            "InstanceId": "<instance-id>",
            "CurrentState": {
                "Code": 80,
                "Name": "stopped"
            },
            "PreviousState": {
                "Code": 80,
                "Name": "running"
            }
        }
    ]
}

Step 3 :-

We can start the instances using start-instances command, 

$aws ec2 start-instances --instance-ids=<your instance id(s)> --profile <your profile>


{
    "StartingInstances": [
        {
            "InstanceId": "<instance-id>",
            "CurrentState": {
                "Code": 0,
                "Name": "pending"
            },
            "PreviousState": {
                "Code": 80,
                "Name": "stopped"
            }
        }
    ]
}



No comments:

Post a Comment