如何通过 CLI 终止 AWS 中的多个 EC2 实例?

How to terminate multiple EC2 instances in AWS via CLI?

我正在寻找通过 AWS CLI 终止多个 EC2 实例。是的,可以通过执行以下命令终止 EC2 实例。

语法aws ec2 terminate-instances --instance-ids <intance id> --profile <profile name>

示例aws ec2 terminate-instances --instance-ids <i-...> --profile xxx

但是我有一大堆需要终止的实例,因此我正在寻找一种解决方案,通过提供实例 ID 列表来终止一批 EC2 实例。我尝试使用如下多个实例 ID,但它们不起作用。

如果有可能终止一批实例,请告诉我。

我会在 shell 脚本中创建一个循环来执行此操作,而不是 运行 所有实例 ID 一次通过。

假设您将每个实例 ID 放在文本文件中的单独一行中,您可以执行如下操作:

#!/usr/bin/env bash

while read ins_id; do
  aws ec2 terminate-instances --instance-ids $ins_id --profile <profile name> || echo "error terminating ${ins_id}"
done < instance_ids.txt

这没有经过测试,我对 shell 脚本编写不是很好,所以如果您尝试使用它,请先尝试一个或两个实例,看看会发生什么。

我可以按照 luk2302

推荐的以下命令来实现这一点

aws ec2 terminate-instances --instance-ids instance-id1 instance-id2 --profile xxx

同样根据 Alex Bailey 的建议,我们可以尝试使用 shell 脚本 (.sh) 或批处理 (.bat),这将使我们的工作更轻松。