8

如何从命令行杀死我的所有实例?有这个命令还是我必须编写脚本?

4

5 回答 5

11

这是一个老问题,但我想我会为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' ' ')

相关资料:

如果黑客禁用了意外实例终止,请先运行以下命令:

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
于 2016-08-06T00:10:48.767 回答
5

AWS 控制台Elasticfox让它变得非常简单。

使用 EC2 API 工具可以在一行中实现命令行解决方案:

for i in `ec2din | grep running | cut -f2`; do ec2kill $i; done
于 2009-03-12T22:36:11.803 回答
4

据我所知,ec2-terminate-instances 命令没有“全部”开关。所以你可能需要编写脚本。不会那么难。您只需要生成一个逗号分隔的实例列表。

这是我正在使用的 python 脚本:

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()

它使用boto库。这对于特定任务来说不是必需的(一个简单的 shell 脚本就足够了),但在许多场合它可能很方便。

最后,您知道 Firefox 的 Elasticfox 扩展吗?这是迄今为止访问 EC2 的最简单方法。

于 2009-03-12T12:29:16.047 回答
2

这是使用 boto3 的更新答案:

  1. 下载python 3
  2. 按照boto3的快速入门
  3. 将以下代码粘贴到一个文件中,并在没有空格的情况下调用它,我做了 delete_ec2_instances.py
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")
  1. 使用命令行,导航到上述文件并输入:python terminate_ec2_instances.py(或任何您的文件名称。
  2. 您应该看到该区域被删除时的名称,以及所有实例都已终止时的最终完成消息。
于 2019-09-03T04:56:52.740 回答
1

为了完整起见。这是另一种方式,通过使用正则表达式和 aws cli,更符合程序员的要求:

aws ec2 terminate-instances 
        --instance-ids 
         $(
          aws ec2 describe-instances 
            | grep InstanceId 
            | awk {'print $2'} 
            | sed 's/[",]//g'
          )
于 2017-09-10T14:53:14.750 回答