Search AWS CLI output and save to variable


I’m very new to batch files, but I’m trying to use one to automate some AWS CLI instance-creation.

What I have is pretty simple so far — I have a command in my .bat file that will run the run-instances command:

This command takes a little bit to run, but will eventually (without the --dry-run) return json about the created instance(s). I’d like to search that json output and save the instance-id to a variable so that I can use it to tag my newly created instance with the ec2 create-tags command.

Any thoughts on how I could do that? My first attempt was to add > test.txt to the end of the above command and then search through the json and set the variable. However, the test.txt is created instantly before the CLI command has finished and returned its output.



The AWS Command-Line Interface (CLI) has a --query parameter that can be used to specify the output fields. Combined with --output text, it can provide a list of Instance IDs.

Here’s a script, assuming that only one instance is started per run-instances call (otherwise a loop would be required):

Leave a Reply