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

前端 未结 1 1060
广开言路
广开言路 2021-01-01 20:14

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 \\
            


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 20:50

    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

    0 讨论(0)
提交回复
热议问题