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'}
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With