Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a cli variable in AWSCLI query

Tags:

bash

aws-cli

How do I use a variable with an AWS query??

There does not seem to documentation on the query syntax, mearly examples.

Im trying to do the following:

API_ID=$(aws apigateway get-rest-apis --query 'items[?name == `${API_NAME}`] | [0].{id: id}' --output text)

The problem is that ${API_NAME} is read literally. Any ideas?

like image 984
cosbor11 Avatar asked Feb 09 '16 20:02

cosbor11


People also ask

Which command is used when working with the AWS CLI?

The AWS Command Line Interface (AWS CLI) examples in this guide are formatted using the following conventions: Prompt – The command prompt uses the Linux prompt and is displayed as ( $ ). For commands that are Windows specific, C:\> is used as the prompt. Do not include the prompt when you type commands.

What AWS CLI output format is most readable by a person?

The table format produces human-readable representations of complex AWS CLI output in a tabular form. You can combine the --query option with the table format to display a set of elements preselected from the raw output.

What command can be used to have the output in JSON format?

json/array. This option produces raw JSON wrapped in a JSON array. You can select these output formats by starting MySQL Shell with the --result-format= value command line option, or setting the MySQL Shell configuration option resultFormat .


1 Answers

I figured this out after sometime...

AWS uses JMESPath, as the spec for their --query option. When passing jmespath filter expression as a string:

You can use double quotes (") instead and wrap the variable in single quotes ('). This will not prevent the variable from being replaced.

So it worked when I changed it to:

API_ID=$(aws apigateway get-rest-apis --query "items[?name == '${API_NAME}'] | [0].{id: id}" --output text) 
like image 188
cosbor11 Avatar answered Sep 28 '22 07:09

cosbor11