pbaranski
pbaranski

Reputation: 24952

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

Upvotes: 8

Views: 4630

Answers (2)

pbaranski
pbaranski

Reputation: 24952

@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`

Upvotes: 3

John Rotenstein
John Rotenstein

Reputation: 269101

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`

Upvotes: 11

Related Questions