14

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?

1 Answer 1

21

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) 
Sign up to request clarification or add additional context in comments.

1 Comment

This works inline in the bash terminal but it doesnt' work in a bash script if just declaring the variable above it. The variable must be exported to work in a bash script file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.