查询标签tag里key为Name的不同value值的实例ID及运行状态
query.bat
set monitor_list=EC2client1,EC2client2,EC2client3 aws ec2 describe-instances --filters "Name=tag-value,Values=%monitor_list%" --query Reservations[*].[Instances[*].[Tags[?Key==`Name`].[Value],State.[Name]]] --output table pause
启动现在为Stopped状态的在monitor_list中的EC2
start.bat
set monitor_list=EC2client1,client2,client3 aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped" "Name=tag-value,Values=%monitor_list%" --query Reservations[*].Instances[].join('',[InstanceId]) --output text > startlist.txt for /f "tokens=1" %%i in (startlist.txt) do aws ec2 start-instances --instance-ids %%i pause
停止现在为running状态的在monitor_list中的EC2
stop.bat
set monitor_list=client1,client2,client3,client4 aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag-value,Values=%monitor_list%" --query Reservations[*].Instances[].join('',[InstanceId]) --output text > stop.txt for /f "tokens=1" %%i in (stop.txt) do aws ec2 stop-instances --instance-ids %%i pause