有没有一种方法可以将一个AWS CLI命令的输出作为输入传递给另一个AWS CLI命令? [英] Is there a way to pipe the output of one AWS CLI command as the input to another?

查看:118
本文介绍了有没有一种方法可以将一个AWS CLI命令的输出作为输入传递给另一个AWS CLI命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用运行实例,并将生成的实例ID作为输入传递给create-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)?

其他背景

问这个问题的动机是,使用适用于Windows PowerShell的AWS工具可以实现类似的功能.我希望通过AWS CLI完成同样的事情.

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.

等效的PowerShell示例:

Equivalent PowerShell example:

New-EC2Instance -ImageId ami-1234 |
    ForEach-Object Instances |
    ForEach-Object InstanceId |
    New-EC2Tag -Tag @{key='foo';value='bar'}

推荐答案

可以通过利用xargs {}捕获实例ID并将其输入到create-tags的--resources参数中来完成.

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"'

请注意,与原始问题中的示例不同,将--query参数值的"InstanceId"部分包装在方括号中非常重要,这样,如果一个调用了--count大于1的run-instances,则多个实例返回的ID将以单独的行输出,而不是用制表符分隔.请参阅 http://docs.aws. amazon.com/cli/latest/userguide/controlling-output.html#controlling-output-format

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

这篇关于有没有一种方法可以将一个AWS CLI命令的输出作为输入传递给另一个AWS CLI命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆