cosbor11
cosbor11

Reputation: 16034

How to use a cli variable in AWSCLI query

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?

Upvotes: 14

Views: 10272

Answers (1)

cosbor11
cosbor11

Reputation: 16034

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) 

Upvotes: 21

Related Questions