Is there a way to pass the output of an AWS CLI command as input to another?

I am trying to call run instances and pass the resulting instance identifiers as input to create tags as a single-line image as follows:

aws ec2 run-instances \ --image-id ami-1234 \ --output text \ --query Instances[*].InstanceId | \ aws ec2 create-tags \ --tags 'Key="foo",Value="bar"' 

When I try this, I get the following:

 usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters] To see help text, you can run: aws help aws <command> help aws <command> <subcommand> help aws: error: argument --resources is required 

Is something like this possible or need to resort to using variables (or in some other way that I don't think about)?

Additional background

The motivation for asking this question is that something similar is possible with AWS Tools for Windows PowerShell; I was hoping to do the same with the CLI AWS.

An equivalent PowerShell example:

 New-EC2Instance -ImageId ami-1234 | ForEach-Object Instances | ForEach-Object InstanceId | New-EC2Tag -Tag @{key='foo';value='bar'} 
+5
source share
1 answer

This can be done using xargs {} to grab the instance ids to pass it to the --resources parameter of the create tags.

 aws ec2 run-instances \ --image-id ami-1234 \ --output text \ --query Instances[*].[InstanceId] | \ xargs -I {} aws ec2 create-tags \ --resources {} \ --tags 'Key="foo",Value="bar"' 

Please note that unlike the example in the original question, it is important to wrap the “InstanceId” part of the -query parameter value in brackets so that when run instances are called with -count more than one, the multiple instance Identifiers that are returned will be displayed as separate lines, and not divided into tabs. See http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html#controlling-output-format

+9
source

All Articles