简体   繁体   中英

AWS cli: how to start all machines found by tag

I can list all machines:
aws ec2 describe-instances --filters "Name=tag:Env,Values=my_super_tag" --query 'Reservations[].Instances[].[InstanceId]' --output text
And then I wish to start all found machines - is the aws cli expression what allow that?

The workaround can be applying next aws cli command for received output (machines ids) but here I got the problem too:

 $ aws ec2 describe-instances --filters "Name=tag:Env,Values=my_super_tag" --query 'Reservations[].Instances[].[InstanceId]' --output text\
  | xargs -L1 aws ec2 start-instances --instance-ids
' does not existd (InvalidInstanceID.NotFound) when calling the StartInstances operation: The instance ID 'i-12345677890
xargs: aws: exited with status 255; aborting

Strange because with echo
aws ec2 describe-instances --filters "Name=tag:Env,Values=spt1" --query 'Reservations[].Instances[].[InstanceId]' --output text | xargs -L 1 echo aws ec2 start-instances --instance-ids
I get output (executing one of below line works as intended)

aws ec2 start-instances --instance-ids i-2123456789
aws ec2 start-instances --instance-ids i-3123456789
aws ec2 start-instances --instance-ids i-4123456789
aws ec2 start-instances --instance-ids i-5123456789

You can embed one command within another, eg:

aws ec2 start-instances --instance-ids `ANOTHER-COMMAND`

So, try this:

aws ec2 start-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:Env,Values=my_super_tag" --query 'Reservations[].Instances[].InstanceId' --output text`

@John Rotenstein answer do the job, but due to AWS limits and handling already started instances (my question about this link ), it's good to add to query

"Name=instance-state-name,Values=stopping,stopped"

So full query then will look like

aws ec2 start-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:Env,Values=my-super-tag" "Name=instance-state-name,Values=stopping,stopped" --query 'Reservations[].Instances[].InstanceId' --outpu t text`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM