Reputation: 274080
How can I kill all my instances from the command line? Is there a command for this or must I script it?
Upvotes: 9
Views: 10606
Reputation: 56
The core of this wasn't mine, but I had to make modifications to make it delete all instances in all regions. This was run in powershell using the aws cli install:
foreach ($regionID in (aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text)){foreach ($id in (aws ec2 describe-instances --filters --query "Reservations[].Instances[].[InstanceId]" --output text --region $regionID)) { aws ec2 terminate-instances --instance-ids $id --region $regionID}}
This lists all the regions in AWS, then runs a foreach on the regions. In the regions foreach it lists all the instances in the region, then runs a foreach on the instances. In the instances foreach it terminates the instance.
This was needed after my amazon account got hacked and they setup 10,000 instances of ec2 that needed deleting.
Upvotes: 0
Reputation: 490
Here is an updated answer using boto3:
import boto3
def terminateRegion(region, ignore_termination_protection=True):
"""This function creates an instance in the specified region, then gets the stopped and running instances in that region, then sets the 'disableApiTermination' to "false", then terminates the instance."""
# Create the profile with the given region and the credentials from:
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html
s = boto3.session.Session(region_name=region)
ec2 = s.resource('ec2')
# Get all the instances from the specified region that are either stopped or running
instances = ec2.instances.filter(Filters=[{'Name':'instance-state-name', 'Values': ['stopped', 'running', 'pending']}])
for instance in instances:
# set 'disableApiTermination' to 'false' so we can terminate the instance.
if ignore_termination_protection:
instance.modify_attribute(Attribute='disableApiTermination', Value='false')
instance.terminate()
print("done with {0}".format(region))
if __name__ == "__main__":
# We get a list of regions that the account is associated with
ec2 = boto3.client('ec2')
regions = [r['RegionName'] for r in ec2.describe_regions()['Regions']]
# loop through the regions and terminate all the instances in each region
for region in regions:
terminateRegion(region)
print("done with everything")
Upvotes: 2
Reputation: 8657
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' ' ')
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
Upvotes: 14
Reputation: 24740
For completeness sake. Here's another way, being more in line with the repertoire of a programmer, by using regular expressions and the aws cli:
aws ec2 terminate-instances
--instance-ids
$(
aws ec2 describe-instances
| grep InstanceId
| awk {'print $2'}
| sed 's/[",]//g'
)
Upvotes: 1
Reputation: 1199
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
Upvotes: 5
Reputation: 104196
As far as I know there isn't an 'all' switch for the ec2-terminate-instances command. So you probably need to script it. It won't be that hard. You only need to generate a comma separated list of your instances.
This is a python script I am using:
import sys
import time
from boto.ec2.connection import EC2Connection
def main():
conn = EC2Connection('', '')
instances = conn.get_all_instances()
print instances
for reserv in instances:
for inst in reserv.instances:
if inst.state == u'running':
print "Terminating instance %s" % inst
inst.stop()
if __name__ == "__main__":
main()
It uses boto library. This is not necessary for the specific task (a simple shell script will be enough), but it may be handy in many occasions.
Finally are you aware of Elasticfox extension for Firefox? This is by far the easiest way to access EC2.
Upvotes: 4