Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I kill all my EC2 instances from the command line?

How can I kill all my instances from the command line? Is there a command for this or must I script it?

like image 201
flybywire Avatar asked Mar 12 '09 12:03

flybywire


People also ask

How do you kill an AWS instance?

To terminate an instance using the consoleOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances. Select the instance, and choose Actions, Instance State, Terminate. Choose Yes, Terminate when prompted for confirmation.

How kill AWS process in Linux?

kill command The command for that is simply 'kill' followed by the process ID. In the below screenshot, I have mentioned the PID of firefox application which I had started earlier. The default command 'kill' sends signal to the process to be terminated.


2 Answers

This is an old question but thought I'd share a solution for AWS CLI:

aws ec2 terminate-instances --instance-ids $(aws ec2 describe-instances --filters  "Name=instance-state-name,Values=pending,running,stopped,stopping" --query "Reservations[].Instances[].[InstanceId]" --output text | tr '\n' ' ')

Related info:

If hackers have disabled accidental instance termination, first run this command:

aws ec2 describe-instances --filters  "Name=instance-state-name,Values=pending,running,stopped,stopping" --query "Reservations[].Instances[].[InstanceId]" --output text  |  xargs --delimiter '\n' --max-args=1 aws ec2   modify-instance-attribute  --no-disable-api-termination --instance-id
like image 190
Belmin Fernandez Avatar answered Oct 27 '22 06:10

Belmin Fernandez


AWS Console and Elasticfox make it pretty easy.

A command-line solution can be achieved in one-line using the EC2 API tools:

for i in `ec2din | grep running | cut -f2`; do ec2kill $i; done
like image 31
AdamK Avatar answered Oct 27 '22 07:10

AdamK