Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 856
pbaranski Avatar asked Apr 24 '17 05:04

pbaranski


People also ask

Which CLI command is used to tag AWS resources?

By default, the AWS CLI uses SSL when communicating with AWS services. For each SSL connection, the AWS CLI will verify SSL certificates. This option overrides the default behavior of verifying SSL certificates.

How do I view all EC2 instances?

AWS has recently launched the Amazon EC2 Global View with initial support for Instances, VPCs, Subnets, Security Groups, and Volumes. To see all running instances go to EC2 or VPC console and click EC2 Global View in the top left corner.

What AWS CLI command is used to display all EC2 instances?

You can use the AWS CLI to list your instances and view information about them. You can list all your instances, or filter the results based on the instances that you're interested in. The following examples show how to use the aws ec2 describe-instances command. The following command lists all your instances.

How do I run an instance from AWS CLI?

Running the EC2 Instance For this login to your AWS Console and choose any AMI of your type. Copy the image id and replace it here in place of <ami-id>. Also use your key pair name, security group id, and subnet id at the correct place in the above command. Also, make a note of the InstanceId.


2 Answers

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`
like image 200
John Rotenstein Avatar answered Sep 28 '22 06:09

John Rotenstein


@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`
like image 45
pbaranski Avatar answered Sep 28 '22 06:09

pbaranski