Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pipe the output of one AWS CLI command as the input to another?

Tags:

I'm attempting to call run-instances and pass the resulting instance IDs as the input to create-tags as a one-liner 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 attempting 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 does one have to resort to using variables (or some other way I'm not thinking about)?

Additional Background

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

Equivalent PowerShell example:

New-EC2Instance -ImageId ami-1234 |
    ForEach-Object Instances |
    ForEach-Object InstanceId |
    New-EC2Tag -Tag @{key='foo';value='bar'}
like image 256
Rafael Dowling Goodman Avatar asked Jul 01 '16 14:07

Rafael Dowling Goodman


People also ask

How do I filter AWS CLI output?

To additionally filter the output, you can use other command line tools such as head or tail . If you specify --output json , --output yaml , or --output yaml-stream the output is completely processed as a single, native structure before the --query filter is applied.

What are the different output formats available when working with the AWS CLI?

The AWS CLI supports the following output formats: json – The output is formatted as a JSON string. yaml – The output is formatted as a YAML string. yaml-stream – The output is streamed and formatted as a YAML string.

How do I change the default output format in AWS CLI?

Use the output option in the named profile in the 'config' file. It sets the default output format to JSON. Do check out AWS Training by Intellipaat and master AWS.

Can I run AWS CLI commands in Lambda?

In general, when you want to use AWS CLI in Lambda, it's best to call AWS APIs directly by using the appropriate SDK from your function's code. But there's one specific use case that is easier to do with the AWS CLI than API: aws s3 sync . This post will show how to enable AWS CLI in the Lambda execution environment.


1 Answers

It can be done by leveraging xargs {} to capture the instance IDs to feed it into the --resources parameter of 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"'

Note that unlike the example in the original question, it's important to wrap the "InstanceId" portion of the --query parameter value in brackets so that if one calls run-instances with --count greater than one, the multiple instance IDs that get returned will be outputted as separate lines instead of being tab-delimited. See http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html#controlling-output-format

like image 196
Frederic Henri Avatar answered Sep 21 '22 14:09

Frederic Henri